本文整理匯總了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.`);
}
});
});