|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import * as Lint from 'tslint'; |
| 3 | +import * as utils from 'tsutils'; |
| 4 | + |
| 5 | +/** Doc tag that can be used to indicate a breaking change. */ |
| 6 | +const BREAKING_CHANGE = '@breaking-change'; |
| 7 | + |
| 8 | +/** Name of the old doc tag that was being used to indicate a breaking change. */ |
| 9 | +const DELETION_TARGET = '@deletion-target'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Rule that ensures that comments, indicating a deprecation |
| 13 | + * or a breaking change, have a valid version. |
| 14 | + */ |
| 15 | +export class Rule extends Lint.Rules.AbstractRule { |
| 16 | + apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 17 | + return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<any>) => { |
| 18 | + utils.forEachComment(ctx.sourceFile, (file, {pos, end}) => { |
| 19 | + const commentText = file.substring(pos, end); |
| 20 | + |
| 21 | + // TODO(crisbeto): remove this check once most of the pending |
| 22 | + // PRs start using `breaking-change`. |
| 23 | + if (commentText.indexOf(DELETION_TARGET) > -1) { |
| 24 | + ctx.addFailure(pos, end, `${DELETION_TARGET} has been replaced with ${BREAKING_CHANGE}.`); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const hasBreakingChange = commentText.indexOf(BREAKING_CHANGE) > -1; |
| 29 | + |
| 30 | + if (!hasBreakingChange && commentText.indexOf('@deprecated') > -1) { |
| 31 | + ctx.addFailure(pos, end, `@deprecated marker has to have a ${BREAKING_CHANGE}.`); |
| 32 | + } if (hasBreakingChange && !/\d+\.\d+\.\d+/.test(commentText)) { |
| 33 | + ctx.addFailure(pos, end, `${BREAKING_CHANGE} must have a version.`); |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + } |
| 38 | +} |
0 commit comments