本文整理汇总了TypeScript中tsutils.forEachComment函数的典型用法代码示例。如果您正苦于以下问题:TypeScript forEachComment函数的具体用法?TypeScript forEachComment怎么用?TypeScript forEachComment使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了forEachComment函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: if
return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<any>) => {
utils.forEachComment(ctx.sourceFile, (file, {pos, end}) => {
const commentText = file.substring(pos, end);
// TODO(crisbeto): remove this check once most of the pending
// PRs start using `breaking-change`.
if (commentText.indexOf(DELETION_TARGET) > -1) {
ctx.addFailure(pos, end, `${DELETION_TARGET} has been replaced with ${BREAKING_CHANGE}.`);
return;
}
const hasBreakingChange = commentText.indexOf(BREAKING_CHANGE) > -1;
if (!hasBreakingChange && commentText.indexOf('@deprecated') > -1) {
ctx.addFailure(pos, end, `@deprecated marker has to have a ${BREAKING_CHANGE}.`);
} if (hasBreakingChange) {
const version = commentText.match(/\d+\.\d+\.\d+/);
if (!version) {
ctx.addFailure(pos, end, `${BREAKING_CHANGE} must have a version.`);
} else if (this._hasExpired(packageVersion, version[0])) {
ctx.addFailure(pos, end, `Breaking change at ${version[0]} is due to be deleted. ` +
`Current version is ${packageVersion}.`);
}
}
});
});
示例2: getDisableMap
/**
* The map will have an array of TextRange for each disable of a rule in a file.
* (It will have no entry if the rule is never disabled, meaning all arrays are non-empty.)
*/
function getDisableMap(
sourceFile: ts.SourceFile,
failingRules: Set<string>,
): ReadonlyMap<string, ts.TextRange[]> {
const map = new Map<string, ts.TextRange[]>();
utils.forEachComment(sourceFile, (fullText, comment) => {
const commentText =
comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
? fullText.substring(comment.pos + 2, comment.end)
: fullText.substring(comment.pos + 2, comment.end - 2);
const parsed = parseComment(commentText);
if (parsed !== undefined) {
const { rulesList, isEnabled, modifier } = parsed;
const switchRange = getSwitchRange(modifier, comment, sourceFile);
if (switchRange !== undefined) {
const rulesToSwitch =
rulesList === "all"
? Array.from(failingRules)
: rulesList.filter(r => failingRules.has(r));
for (const ruleToSwitch of rulesToSwitch) {
switchRuleState(ruleToSwitch, isEnabled, switchRange.pos, switchRange.end);
}
}
}
});
return map;
function switchRuleState(
ruleName: string,
isEnable: boolean,
start: number,
end: number,
): void {
const disableRanges = map.get(ruleName);
if (isEnable) {
if (disableRanges !== undefined) {
const lastDisable = disableRanges[disableRanges.length - 1];
if (lastDisable.end === -1) {
lastDisable.end = start;
if (end !== -1) {
// Disable it again after the enable range is over.
disableRanges.push({ pos: end, end: -1 });
}
}
}
} else {
// disable
if (disableRanges === undefined) {
map.set(ruleName, [{ pos: start, end }]);
} else if (disableRanges[disableRanges.length - 1].end !== -1) {
disableRanges.push({ pos: start, end });
}
}
}
}
示例3: getEnableDisableRuleMap
public getEnableDisableRuleMap() {
utils.forEachComment(this.sourceFile, (fullText, comment) => {
const commentText = comment.kind === ts.SyntaxKind.SingleLineCommentTrivia
? fullText.substring(comment.pos + 2, comment.end)
: fullText.substring(comment.pos + 2, comment.end - 2);
return this.handleComment(commentText, comment);
});
return this.enableDisableRuleMap;
}
示例4: visitSourceFile
visitSourceFile(sourceFile: ts.SourceFile) {
utils.forEachComment(sourceFile, (text, commentRange) => {
const isTodoComment = text.substring(commentRange.pos, commentRange.end).includes('TODO:');
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && isTodoComment) {
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE);
}
});
super.visitSourceFile(sourceFile);
}
示例5: visitSourceFile
visitSourceFile(sourceFile: ts.SourceFile) {
utils.forEachComment(sourceFile, (fullText, commentRange) => {
const htmlIsEscaped =
this._parseForHtml(fullText.substring(commentRange.pos, commentRange.end));
if (commentRange.kind === ts.SyntaxKind.MultiLineCommentTrivia && !htmlIsEscaped) {
this.addFailureAt(commentRange.pos, commentRange.end - commentRange.pos, ERROR_MESSAGE);
}
});
super.visitSourceFile(sourceFile);
}
示例6: if
return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<any>) => {
utils.forEachComment(ctx.sourceFile, (file, {pos, end}) => {
const commentText = file.substring(pos, end);
const hasDeletionTarget = commentText.indexOf('@deletion-target') > -1;
if (!hasDeletionTarget && commentText.indexOf('@deprecated') > -1) {
ctx.addFailure(pos, end, '@deprecated marker has to have a @deletion-target.');
} if (hasDeletionTarget) {
const version = commentText.match(/\d+\.\d+\.\d+/);
if (!version) {
ctx.addFailure(pos, end, '@deletion-target must have a version.');
} else if (this._hasExpired(packageVersion, version[0])) {
ctx.addFailure(pos, end, `Deletion target at ${version[0]} is due to be deleted. ` +
`Current version is ${packageVersion}.`);
}
}
});
});
示例7: if
return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<any>) => {
utils.forEachComment(ctx.sourceFile, (file, {pos, end}) => {
const commentText = file.substring(pos, end);
// TODO(crisbeto): remove this check once most of the pending
// PRs start using `breaking-change`.
if (commentText.indexOf(DELETION_TARGET) > -1) {
ctx.addFailure(pos, end, `${DELETION_TARGET} has been replaced with ${BREAKING_CHANGE}.`);
return;
}
const hasBreakingChange = commentText.indexOf(BREAKING_CHANGE) > -1;
if (!hasBreakingChange && commentText.indexOf('@deprecated') > -1) {
ctx.addFailure(pos, end, `@deprecated marker has to have a ${BREAKING_CHANGE}.`);
} if (hasBreakingChange && !/\d+\.\d+\.\d+/.test(commentText)) {
ctx.addFailure(pos, end, `${BREAKING_CHANGE} must have a version.`);
}
});
});