Skip to content
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

build: custom lint rules not working locally #28187

Merged
merged 1 commit into from
Nov 27, 2023
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
8 changes: 8 additions & 0 deletions test/angular-test-init-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @license
* Copyright Google LLC 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
*/

import {TestBed} from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
Expand Down
9 changes: 2 additions & 7 deletions tools/tslint-rules/lightweightTokensRule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import ts from 'typescript';
import minimatch from 'minimatch';

import * as path from 'path';
import * as Lint from 'tslint';

/** Arguments this rule supports. */
Expand Down Expand Up @@ -43,10 +41,7 @@ export class Rule extends Lint.Rules.TypedRule {
* for which lightweight tokens are suitable (for optimized tree shaking).
*/
function checkSourceFileForLightweightTokens(ctx: Context, typeChecker: ts.TypeChecker): void {
// Relative path for the current TypeScript source file. This allows for
// relative globs being used in the rule options.
const relativeFilePath = path.relative(process.cwd(), ctx.sourceFile.fileName);
const [enabledFilesGlobs] = ctx.options;
const [disabledFileGlobs] = ctx.options;
const visitNode = (node: ts.Node) => {
if (ts.isClassDeclaration(node)) {
checkClassDeclarationForLightweightToken(node, ctx, typeChecker);
Expand All @@ -55,7 +50,7 @@ function checkSourceFileForLightweightTokens(ctx: Context, typeChecker: ts.TypeC
}
};

if (enabledFilesGlobs.some(g => minimatch(relativeFilePath, g))) {
if (!disabledFileGlobs.some(g => minimatch(ctx.sourceFile.fileName, g))) {
ts.forEachChild(ctx.sourceFile, visitNode);
}
}
Expand Down
11 changes: 4 additions & 7 deletions tools/tslint-rules/noCrossEntryPointRelativeImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ts from 'typescript';
import minimatch from 'minimatch';

import {existsSync} from 'fs';
import {dirname, join, normalize, relative, resolve} from 'path';
import {dirname, join, normalize, resolve} from 'path';
import * as Lint from 'tslint';

const BUILD_BAZEL_FILE = 'BUILD.bazel';
Expand All @@ -16,7 +16,7 @@ const BUILD_BAZEL_FILE = 'BUILD.bazel';
*/
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, checkSourceFile, this.getOptions().ruleArguments);
return this.applyWithFunction(sourceFile, checkSourceFile, this.getOptions().ruleArguments[0]);
}
}

Expand All @@ -25,10 +25,7 @@ export class Rule extends Lint.Rules.AbstractRule {
* with relative cross entry-point references.
*/
function checkSourceFile(ctx: Lint.WalkContext<string[]>) {
const filePath = ctx.sourceFile.fileName;
const relativeFilePath = relative(process.cwd(), filePath);

if (!ctx.options.every(o => minimatch(relativeFilePath, o))) {
if (ctx.options.some(o => minimatch(ctx.sourceFile.fileName, o))) {
return;
}

Expand All @@ -43,7 +40,7 @@ function checkSourceFile(ctx: Lint.WalkContext<string[]>) {
}

const modulePath = node.moduleSpecifier.text;
const basePath = dirname(filePath);
const basePath = dirname(ctx.sourceFile.fileName);
const currentPackage = findClosestBazelPackage(basePath);
const resolvedPackage = findClosestBazelPackage(resolve(basePath, modulePath));

Expand Down
6 changes: 2 additions & 4 deletions tools/tslint-rules/noLifecycleInvocationRule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import * as Lint from 'tslint';
import ts from 'typescript';
import minimatch from 'minimatch';
Expand Down Expand Up @@ -28,9 +27,8 @@ class Walker extends Lint.RuleWalker {

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
const fileGlobs = options.ruleArguments;
const relativeFilePath = path.relative(process.cwd(), sourceFile.fileName);
this._enabled = fileGlobs.some(p => minimatch(relativeFilePath, p));
const fileGlobs: string[] = options.ruleArguments[0];
this._enabled = !fileGlobs.some(p => minimatch(sourceFile.fileName, p));
}

override visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
Expand Down
8 changes: 2 additions & 6 deletions tools/tslint-rules/requireLicenseBannerRule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import * as Lint from 'tslint';
import minimatch from 'minimatch';
import ts from 'typescript';
Expand Down Expand Up @@ -38,13 +37,10 @@ class RequireLicenseBannerWalker extends Lint.RuleWalker {
super(sourceFile, options);

// Globs that are used to determine which files to lint.
const fileGlobs = options.ruleArguments;

// Relative path for the current TypeScript source file.
const relativeFilePath = path.relative(process.cwd(), sourceFile.fileName);
const fileGlobs: string[] = options.ruleArguments[0];

// Whether the file should be checked at all.
this._enabled = fileGlobs.some(p => minimatch(relativeFilePath, p));
this._enabled = !fileGlobs.some(p => minimatch(sourceFile.fileName, p));
}

override visitSourceFile(sourceFile: ts.SourceFile) {
Expand Down
11 changes: 4 additions & 7 deletions tools/tslint-rules/validateDecoratorsRule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import * as Lint from 'tslint';
import ts from 'typescript';
import minimatch from 'minimatch';
Expand Down Expand Up @@ -69,15 +68,13 @@ class Walker extends Lint.RuleWalker {
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);

// Globs that are used to determine which files to lint.
const fileGlobs = options.ruleArguments.slice(1) || [];

// Relative path for the current TypeScript source file.
const relativeFilePath = path.relative(process.cwd(), sourceFile.fileName);
// Globs that are used to determine which files to exclude from linting.
const fileGlobs: string[] = options.ruleArguments[1] || [];

this._rules = this._generateRules(options.ruleArguments[0]);
this._enabled =
Object.keys(this._rules).length > 0 && fileGlobs.some(p => minimatch(relativeFilePath, p));
Object.keys(this._rules).length > 0 &&
!fileGlobs.some(p => minimatch(sourceFile.fileName, p));
}

override visitClassDeclaration(node: ts.ClassDeclaration) {
Expand Down
27 changes: 19 additions & 8 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@
"require-breaking-change-version": true,
"no-nested-ternary": true,
"prefer-plain-enum": true,
"no-lifecycle-invocation": [true, "**/!(*.spec).ts"],
"no-lifecycle-invocation": [true, ["**/*.spec.ts"]],
"lightweight-tokens": [
true,
["src/**/!(*.spec).ts"],
["**/*.spec.ts"],
// Directionality is always used in Angular Material and therefore does not
// need a lightweight token. Date Adapter is not very significant and does not
// need a dedicated token.
Expand Down Expand Up @@ -120,18 +120,29 @@
}
}
},
"src/!(e2e-app|components-examples|universal-app|dev-app)/**/!(*.spec).ts"
[
// Internal code that doesn't need to follow the same standards.
"**/+(e2e-app|components-examples|universal-app|dev-app|integration)/**",
"**/*.spec.ts"
]
],
"require-license-banner": [
true,
"src/!(e2e-app|components-examples|universal-app)/**/!(*.spec).ts"
[
// Internal files that don't need license banners.
"**/+(e2e-app|components-examples|universal-app|dev-app|integration|tools|scripts)/**",
"**/*.spec.ts"
]
],
"no-cross-entry-point-relative-imports": [
true,
"src/!(e2e-app|components-examples|universal-app|dev-app)/**/!(*.spec).ts",
"!src/+(cdk|material)/schematics/**/*",
"!src/cdk/testing/+(private|tests)/**/*",
"!src/google-maps/testing/**/*"
[
// Files that we don't publish to npm so the relative imports don't matter.
"**/+(dev-app|components-examples|schematics|tools)/**",
"**/google-maps/testing/**",
"**/cdk/testing/+(tests|private)/**",
"**/*.spec.ts"
]
],
"file-name-casing": [
true,
Expand Down
Loading