test: remove custom internal tslint rules

This commit is contained in:
Charles Lyding 2020-09-14 16:12:51 -04:00 committed by Charles
parent 62128b4be8
commit c4473eeac8
8 changed files with 0 additions and 315 deletions

View File

@ -1,3 +0,0 @@
# TsLint Rules
This folder contains custom TsLint rules specific to this repository.

View File

@ -1,29 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import * as Lint from 'tslint';
import * as ts from 'typescript';
// An empty rule so that tslint does not error on rules '//' (which are comments).
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: '//',
type: 'typescript',
description: ``,
rationale: '',
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: false,
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return [];
}
}

View File

@ -1,57 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/*
* Taken from https://github.com/Sergiioo/tslint-defocus
* Copyright (c) 2016 Sergio Annecchiarico
* MIT - https://github.com/Sergiioo/tslint-defocus/blob/master/LICENSE
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'defocus',
description: "Bans the use of `fdescribe` and 'fit' Jasmine functions.",
rationale: 'It is all too easy to mistakenly commit a focussed Jasmine test suite or spec.',
options: null,
optionsDescription: 'Not configurable.',
type: 'functionality',
typescriptOnly: false,
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.CallExpression) {
const expression = (node as ts.CallExpression).expression;
const functionName = expression.getText();
bannedFunctions.forEach((banned) => {
if (banned === functionName) {
ctx.addFailureAtNode(expression, failureMessage(functionName));
}
});
}
return ts.forEachChild(node, cb);
});
}
const bannedFunctions: ReadonlyArray<string> = ['fdescribe', 'fit'];
const failureMessage = (functionName: string) => {
return `Calls to '${functionName}' are not allowed.`;
};

View File

@ -1,67 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'import-groups',
type: 'style',
description: `Ensure imports are grouped.`,
rationale: `Imports can be grouped or not depending on a project. A group is a sequence of
import statements separated by blank lines.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: false,
};
public static FAILURE_STRING = 'You need to keep imports grouped.';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}
class Walker extends Lint.RuleWalker {
walk(sourceFile: ts.SourceFile) {
super.walk(sourceFile);
const statements = sourceFile.statements;
const imports = statements.filter(s => s.kind == ts.SyntaxKind.ImportDeclaration);
const nonImports = statements.filter(s => s.kind != ts.SyntaxKind.ImportDeclaration);
for (let i = 1; i < imports.length; i++) {
const node = imports[i];
const previous = imports[i - 1];
if (previous && previous.kind == ts.SyntaxKind.ImportDeclaration) {
const nodeLine = sourceFile.getLineAndCharacterOfPosition(node.getStart());
const previousLine = sourceFile.getLineAndCharacterOfPosition(previous.getEnd());
if (previousLine.line < nodeLine.line - 1) {
if (nonImports.some(s => s.getStart() > previous.getEnd()
&& s.getStart() < node.getStart())) {
// Ignore imports with non-imports statements in between.
continue;
}
this.addFailureAt(
node.getStart(),
node.getWidth(),
Rule.FAILURE_STRING,
Lint.Replacement.deleteFromTo(previous.getEnd() + 1, node.getStart()),
);
}
}
}
}
}

View File

@ -1,80 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import * as path from 'path';
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'no-global-tslint-disable',
type: 'style',
description: `Ensure global tslint disable are only used for unit tests.`,
rationale: `Some projects want to disallow tslint disable and only use per-line ones.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: false,
};
public static FAILURE_STRING = 'tslint:disable is not allowed in this context.';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}
class Walker extends Lint.RuleWalker {
private _findComments(node: ts.Node): ts.CommentRange[] {
return ([] as ts.CommentRange[]).concat(
ts.getLeadingCommentRanges(node.getFullText(), 0) || [],
ts.getTrailingCommentRanges(node.getFullText(), 0) || [],
node.getChildren().reduce((acc, n) => {
return acc.concat(this._findComments(n));
}, [] as ts.CommentRange[]),
);
}
walk(sourceFile: ts.SourceFile) {
super.walk(sourceFile);
// Ignore spec files.
if (sourceFile.fileName.match(/_spec(_large)?.ts$/)) {
return;
}
// Ignore benchmark files.
if (sourceFile.fileName.match(/_benchmark.ts$/)) {
return;
}
// TODO(filipesilva): remove this once the files are cleaned up.
// Ignore Angular CLI files files.
if (sourceFile.fileName.includes('/angular-cli-files/')) {
return;
}
const scriptsPath = path.join(process.cwd(), 'scripts').replace(/\\/g, '/');
if (sourceFile.fileName.startsWith(scriptsPath)) {
return;
}
// Find all comment nodes.
const ranges = this._findComments(sourceFile);
ranges.forEach(range => {
const text = sourceFile.getFullText().substring(range.pos, range.end);
let i = text.indexOf('tslint:disable:');
while (i != -1) {
this.addFailureAt(range.pos + i + 1, range.pos + i + 15, Rule.FAILURE_STRING);
i = text.indexOf('tslint:disable:', i + 1);
}
});
}
}

View File

@ -1,61 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'single-eof-line',
type: 'style',
description: `Ensure the file ends with a single new line.`,
rationale: `This is similar to eofline, but ensure an exact count instead of just any new
line.`,
options: null,
optionsDescription: `Two integers indicating minimum and maximum number of new lines.`,
typescriptOnly: false,
};
public static FAILURE_STRING = 'You need to have a single blank line at end of file.';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const length = sourceFile.text.length;
if (length === 0) {
// Allow empty files.
return [];
}
const matchEof = /\r?\n((\r?\n)*)$/.exec(sourceFile.text);
if (!matchEof) {
const lines = sourceFile.getLineStarts();
const fix = Lint.Replacement.appendText(
length,
sourceFile.text[lines[1] - 2] === '\r' ? '\r\n' : '\n',
);
return [
new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix),
];
} else if (matchEof[1]) {
const lines = sourceFile.getLineStarts();
const fix = Lint.Replacement.replaceFromTo(
matchEof.index,
length,
sourceFile.text[lines[1] - 2] === '\r' ? '\r\n' : '\n',
);
return [
new Lint.RuleFailure(sourceFile, length, length, Rule.FAILURE_STRING, this.ruleName, fix),
];
}
return [];
}
}

View File

@ -1,11 +0,0 @@
{
// This tsconfig is to help only build the tslint rules instead of the whole project. Makes
// things faster.
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/etc/rules",
"baseUrl": ""
},
"exclude": [
]
}

View File

@ -6,7 +6,6 @@
"tslint-no-circular-imports"
],
"rulesDirectory": [
"dist/etc/rules",
"node_modules/tslint-sonarts/lib/rules"
],
"linterOptions": {
@ -17,12 +16,6 @@
]
},
"rules": {
// ==================================================================================================
// custom rules defined in etc/rules/**
"defocus": true,
"import-groups": true,
"no-global-tslint-disable": true,
"single-eof-line": true,
// ==================================================================================================
// tslint-sonarts rules. See https://github.com/SonarSource/SonarTS
// These rules are part of the bug detection section of tslint-sonarts