2016-10-23 16:37:15 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-10-23 16:37:15 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-12-27 17:55:58 -05:00
|
|
|
import {RuleFailure} from 'tslint/lib';
|
2016-05-26 13:45:37 -04:00
|
|
|
import {RuleWalker} from 'tslint/lib/language/walker';
|
2015-10-28 18:04:55 -04:00
|
|
|
import {AbstractRule} from 'tslint/lib/rules';
|
2015-12-21 17:50:56 -05:00
|
|
|
import * as ts from 'typescript';
|
2015-10-28 18:04:55 -04:00
|
|
|
|
|
|
|
export class Rule extends AbstractRule {
|
2021-07-07 13:58:22 -04:00
|
|
|
override apply(sourceFile: ts.SourceFile): RuleFailure[] {
|
2015-10-28 18:04:55 -04:00
|
|
|
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 {
|
2016-11-12 08:08:58 -05:00
|
|
|
const text = this.getSourceFile().text;
|
|
|
|
const comment = text.substring(range.pos, range.end);
|
2016-05-26 13:45:37 -04:00
|
|
|
return comment.indexOf('@internal') >= 0;
|
2015-10-28 18:04:55 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 21:40:32 -04:00
|
|
|
private assertInternalAnnotationPresent(node: ts.NamedDeclaration) {
|
|
|
|
if (node.name && node.name.getText().charAt(0) !== '_') return;
|
2017-01-24 12:05:34 -05:00
|
|
|
if (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Private) return;
|
2015-10-28 18:04:55 -04:00
|
|
|
|
|
|
|
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(),
|
2021-07-07 13:58:22 -04:00
|
|
|
`module-private member ${node.name?.getText()} must be annotated @internal`));
|
2015-10-28 18:04:55 -04:00
|
|
|
}
|
|
|
|
}
|