Skip to content

Commit 001001c

Browse files
crisbetotinayuangao
authored andcommitted
build: custom tslint rules not executing in IDEs (#6817)
Fixes our custom tslint rules not executing IDEs, because they're written in TypeScript. This is the preferred option over switching them back to ES6, because some rules require access to TS files from other parts of the build (e.g. the Rollup globals rule).
1 parent 5527e22 commit 001001c

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

tools/gulp/tasks/lint.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {red} from 'chalk';
66

77
// These types lack of type definitions
88
const madge = require('madge');
9-
const resolveBin = require('resolve-bin');
109

1110
/** Glob that matches all SCSS or CSS files that should be linted. */
1211
const stylesGlob = '+(tools|src)/**/!(*.bundle).+(css|scss)';
@@ -28,10 +27,10 @@ task('stylelint', execNodeTask(
2827
));
2928

3029
/** Task to run TSLint against the e2e/ and src/ directories. */
31-
task('tslint', execTsLintTask());
30+
task('tslint', execNodeTask('tslint', tsLintBaseFlags));
3231

3332
/** Task that automatically fixes TSLint warnings. */
34-
task('tslint:fix', execTsLintTask('--fix'));
33+
task('tslint:fix', execNodeTask('tslint', [...tsLintBaseFlags, '--fix']));
3534

3635
/** Task that runs madge to detect circular dependencies. */
3736
task('madge', ['material:clean-build'], () => {
@@ -51,13 +50,3 @@ task('madge', ['material:clean-build'], () => {
5150
function formatMadgeCircularModules(circularModules: string[][]): string {
5251
return circularModules.map((modulePaths: string[]) => `\n - ${modulePaths.join(' > ')}`).join('');
5352
}
54-
55-
/** Creates a gulp task function that will run TSLint together with ts-node. */
56-
function execTsLintTask(...flags: string[]) {
57-
const tslintBinPath = resolveBin.sync('tslint');
58-
const tsNodeOptions = ['-O', '{"module": "commonjs"}'];
59-
60-
// TS-Node needs the module compiler option to be set to `commonjs` because the transpiled
61-
// TypeScript files will be running inside of NodeJS.
62-
return execNodeTask('ts-node', [...tsNodeOptions, tslintBinPath, ...tsLintBaseFlags, ...flags]);
63-
}

tools/tslint-rules/noViewEncapsulationRule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ class Walker extends Lint.RuleWalker {
3535
this._enabled = fileGlobs.some(p => minimatch(relativeFilePath, p));
3636
}
3737

38-
visitClassDeclaration(node) {
38+
visitClassDeclaration(node: ts.ClassDeclaration) {
3939
if (!this._enabled || !node.decorators) {
4040
return;
4141
}
4242

4343
node.decorators
44-
.map(decorator => decorator.expression)
44+
.map(decorator => decorator.expression as any)
4545
.filter(expression => expression.expression.getText() === 'Component')
4646
.filter(expression => expression.arguments.length && expression.arguments[0].properties)
4747
.forEach(expression => {
48-
const hasTurnedOffEncapsulation = expression.arguments[0].properties.some(prop => {
48+
const hasTurnedOffEncapsulation = expression.arguments[0].properties.some((prop: any) => {
4949
const value = prop.initializer.getText();
5050
return prop.name.getText() === 'encapsulation' && value.endsWith('.None');
5151
});

tools/tslint-rules/tsLoaderRule.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require('path');
2+
const Lint = require('tslint');
3+
4+
// Custom rule that registers all of the custom rules, written in TypeScript, with ts-node.
5+
// This is necessary, because `tslint` and IDEs won't execute any rules that aren't in a .js file.
6+
require('ts-node').register({
7+
project: path.join(__dirname, '../gulp/tsconfig.json')
8+
});
9+
10+
// Add a noop rule so tslint doesn't complain.
11+
exports.Rule = class Rule extends Lint.Rules.AbstractRule {
12+
apply() {}
13+
}

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"linebreak-style": [true, "LF"],
8686

8787
// Custom Rules
88-
88+
"ts-loader": true,
8989
"no-exposed-todo": true,
9090
"no-view-encapsulation": [
9191
true,

0 commit comments

Comments
 (0)