Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-string-literal: correctly fix property names with leading underscores #3184

Merged
merged 1 commit into from
Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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