Skip to content

Commit

Permalink
no-string-literal: correctly fix property names with leading undersco…
Browse files Browse the repository at this point in the history
…res (palantir#3184)

The AST contains the escaped name. That bug is fixed in typescript@2.5.0. This adds a fix for older versions.
Fixes: palantir#2965
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent 333a04f commit 08324b5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/rules/noStringLiteralRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ function walk(ctx: Lint.WalkContext<void>) {
if (isElementAccessExpression(node)) {
const argument = node.argumentExpression;
if (argument !== undefined && isStringLiteral(argument) && isValidPropertyAccess(argument.text)) {
// for compatibility with typescript@<2.5.0 to avoid fixing expr['__foo'] to expr.___foo
const propertyName = ts.unescapeIdentifier(argument.text); // tslint:disable-line:deprecation
ctx.addFailureAtNode(
argument,
Rule.FAILURE_STRING,
// expr['foo'] -> expr.foo
Lint.Replacement.replaceFromTo(node.expression.end, node.end, `.${argument.text}`),
Lint.Replacement.replaceFromTo(node.expression.end, node.end, `.${propertyName}`),
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/rules/no-string-literal/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ obj["?a#$!$^&%&"];
// invalid accessor - no crash
obj[]

// that underscrore escaping thing typescript does
obj.__foo__;

4 changes: 4 additions & 0 deletions test/rules/no-string-literal/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ obj["?a#$!$^&%&"];
// invalid accessor - no crash
obj[]

// that underscrore escaping thing typescript does
obj['__foo__'];
~~~~~~~~~ [0]

[0]: object access via string literals is disallowed

0 comments on commit 08324b5

Please sign in to comment.