From 08324b513ac41f3e3a06d36edc479a235a2d017d Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Thu, 31 Aug 2017 20:07:06 +0200 Subject: [PATCH] no-string-literal: correctly fix property names with leading underscores (#3184) The AST contains the escaped name. That bug is fixed in typescript@2.5.0. This adds a fix for older versions. Fixes: #2965 --- src/rules/noStringLiteralRule.ts | 4 +++- test/rules/no-string-literal/test.ts.fix | 3 +++ test/rules/no-string-literal/test.ts.lint | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/rules/noStringLiteralRule.ts b/src/rules/noStringLiteralRule.ts index f4e0a459a14..7c1477dae0a 100644 --- a/src/rules/noStringLiteralRule.ts +++ b/src/rules/noStringLiteralRule.ts @@ -52,11 +52,13 @@ function walk(ctx: Lint.WalkContext) { 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}`), ); } } diff --git a/test/rules/no-string-literal/test.ts.fix b/test/rules/no-string-literal/test.ts.fix index 06f5f80c526..ad0703fba31 100644 --- a/test/rules/no-string-literal/test.ts.fix +++ b/test/rules/no-string-literal/test.ts.fix @@ -24,3 +24,6 @@ obj["?a#$!$^&%&"]; // invalid accessor - no crash obj[] +// that underscrore escaping thing typescript does +obj.__foo__; + diff --git a/test/rules/no-string-literal/test.ts.lint b/test/rules/no-string-literal/test.ts.lint index 9963cee7e06..00c61285f4c 100644 --- a/test/rules/no-string-literal/test.ts.lint +++ b/test/rules/no-string-literal/test.ts.lint @@ -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