-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: Ban introduction of more zone-dependent code (#28711)
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as Lint from 'tslint'; | ||
import ts from 'typescript'; | ||
import minimatch from 'minimatch'; | ||
|
||
/** | ||
* NgZone properties that are ok to access. | ||
*/ | ||
const allowedNgZoneProperties = new Set<string>(['run', 'runOutsideAngular']); | ||
|
||
/** Rule to prevent adding code that depends on using zones. */ | ||
export class Rule extends Lint.Rules.TypedRule { | ||
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new Walker(sourceFile, this.getOptions(), program.getTypeChecker()), | ||
); | ||
} | ||
} | ||
|
||
class Walker extends Lint.RuleWalker { | ||
/** Whether the walker should check the current source file. */ | ||
private _enabled: boolean; | ||
|
||
constructor( | ||
sourceFile: ts.SourceFile, | ||
options: Lint.IOptions, | ||
private _typeChecker: ts.TypeChecker, | ||
) { | ||
super(sourceFile, options); | ||
|
||
// Globs that are used to determine which files to lint. | ||
const fileGlobs: string[] = options.ruleArguments[0]; | ||
|
||
// Whether the file should be checked at all. | ||
this._enabled = !fileGlobs.some(p => minimatch(sourceFile.fileName, p)); | ||
} | ||
|
||
override visitPropertyAccessExpression(node: ts.PropertyAccessExpression) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
|
||
const classType = this._typeChecker.getTypeAtLocation(node.expression); | ||
const className = classType.symbol && classType.symbol.name; | ||
const propertyName = node.name.text; | ||
|
||
if (className === 'NgZone' && !allowedNgZoneProperties.has(propertyName)) { | ||
this.addFailureAtNode(node, `Using NgZone.${propertyName} is not allowed.`); | ||
} | ||
|
||
return super.visitPropertyAccessExpression(node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters