-
Notifications
You must be signed in to change notification settings - Fork 23
/
prefer-dot-notation.ts
21 lines (20 loc) · 1.02 KB
/
prefer-dot-notation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { AbstractRule, Replacement, excludeDeclarationFiles } from '@fimbul/ymir';
import { isElementAccessExpression, isTextualLiteral, isValidPropertyAccess } from 'tsutils';
@excludeDeclarationFiles
export class Rule extends AbstractRule {
public apply() {
for (const node of this.context.getFlatAst()) {
if (isElementAccessExpression(node) &&
// for compatiblity with typescript@<2.9.0
node.argumentExpression !== undefined && // wotan-disable-line no-useless-predicate
isTextualLiteral(node.argumentExpression) && isValidPropertyAccess(node.argumentExpression.text)) {
const property = node.argumentExpression.text;
this.addFailureAtNode(
node.argumentExpression,
`Prefer 'obj.${property}' over 'obj[${node.argumentExpression.getText(this.sourceFile)}]'.`,
Replacement.replace(node.expression.end, node.end, '.' + property),
);
}
}
}
}