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