mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
As outlined in the previous commit which enabled the `esModuleInterop` TypeScript compiler option, we need to update all namespace imports for `typescript` to default imports. This is needed to allow for TypeScript to be imported at runtime from an ES module. Similar changes are needed for modules like `semver` where the types incorrectly suggest named exports that will not exist at runtime when imported from ESM. This commit refactors all imports to match with the lint rule we have configured in the previous commit. See the previous commit for more details on why certain imports have been changed. A special case are the imports to `@babel/core` and `@babel/types`. For these a special interop is needed as both default imports, or named imports break the other module format. e.g default imports would work well for ESM, but it breaks for CJS. For CJS, the named imports would only work, but in ESM, only the default export exist. We work around this for now until the devmode is using ESM as well (which would be consistent with prodmode and gives us more valuable test results). More details on the interop can be found in the `babel_core.ts` files (two interops are needed for both localize/or the compiler-cli). PR Close #43431
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
/**
|
|
* @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 {RuleFailure} from 'tslint/lib';
|
|
import {RuleWalker} from 'tslint/lib/language/walker';
|
|
import {AbstractRule} from 'tslint/lib/rules';
|
|
import ts from 'typescript';
|
|
|
|
export class Rule extends AbstractRule {
|
|
override apply(sourceFile: ts.SourceFile): RuleFailure[] {
|
|
const typedefWalker = new TypedefWalker(sourceFile, this.getOptions());
|
|
return this.applyWithWalker(typedefWalker);
|
|
}
|
|
}
|
|
|
|
class TypedefWalker extends RuleWalker {
|
|
protected visitPropertyDeclaration(node: ts.PropertyDeclaration): void {
|
|
this.assertInternalAnnotationPresent(node);
|
|
super.visitPropertyDeclaration(node);
|
|
}
|
|
|
|
public visitMethodDeclaration(node: ts.MethodDeclaration): void {
|
|
this.assertInternalAnnotationPresent(node);
|
|
super.visitMethodDeclaration(node);
|
|
}
|
|
|
|
private hasInternalAnnotation(range: ts.CommentRange): boolean {
|
|
const text = this.getSourceFile().text;
|
|
const comment = text.substring(range.pos, range.end);
|
|
return comment.indexOf('@internal') >= 0;
|
|
}
|
|
|
|
private assertInternalAnnotationPresent(node: ts.NamedDeclaration) {
|
|
if (node.name && node.name.getText().charAt(0) !== '_') return;
|
|
if (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Private) return;
|
|
|
|
const ranges = ts.getLeadingCommentRanges(this.getSourceFile().text, node.pos);
|
|
if (ranges) {
|
|
for (let i = 0; i < ranges.length; i++) {
|
|
if (this.hasInternalAnnotation(ranges[i])) return;
|
|
}
|
|
}
|
|
this.addFailure(this.createFailure(
|
|
node.getStart(), node.getWidth(),
|
|
`module-private member ${node.name?.getText()} must be annotated @internal`));
|
|
}
|
|
}
|