Skip to content

build: add tslint rule to enforce deletion targets #9881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tools/tslint-rules/deletionTargetRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as utils from 'tsutils';
import * as path from 'path';

/**
* Rule ensuring that deletion targets have not expired.
* The current version is taken from the `package.json`.
*/
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const packageVersion = require(path.join(process.cwd(), 'package.json')).version;

return this.applyWithFunction(sourceFile, (ctx: Lint.WalkContext<any>) => {
utils.forEachComment(ctx.sourceFile, (file, {pos, end}) => {
const commentText = file.substring(pos, end);

if (commentText.indexOf('@deletion-target') === -1) {
return;
}

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}.`);
}
});
});
}

/**
* Checks whether a version has expired, based on the current version.
* @param currentVersion Current version of the package.
* @param deletionTarget Version that is being checked.
*/
private _hasExpired(currentVersion: string, deletionTarget: string) {
if (currentVersion === deletionTarget) {
return true;
}

const current = this._parseVersion(currentVersion);
const target = this._parseVersion(deletionTarget);

return target.major < current.major ||
(target.major === current.major && target.minor < current.minor) ||
(
target.major === current.major &&
target.minor === current.minor &&
target.patch < current.patch
);
}

/** Converts a version string into an object. */
private _parseVersion(version: string) {
const [major = 0, minor = 0, patch = 0] = version.split('.').map(segment => parseInt(segment));
return {major, minor, patch};
}
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"./tools/package-tools/rollup-globals.ts",
"src/+(lib|cdk|material-examples|material-experimental|cdk-experimental)/**/*.ts"
],
"deletion-target": true,
"no-unescaped-html-tag": true
}
}