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

Commit

Permalink
Add runtime check for 'ts.unescapeIdentifier()' presence
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobirukov committed Jul 9, 2018
1 parent f0e368c commit 7ef96b7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/rules/noStringLiteralRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class Rule extends Lint.Rules.AbstractRule {

public static FAILURE_STRING = "object access via string literals is disallowed";

public static id(input: string) {
return input;
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
Expand All @@ -52,8 +56,12 @@ 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
// typescript@<2.5.0 has an extra underscore in escaped identifier text content,
// to avoid fixing issue `expr['__foo'] → expr.___foo`, unescapeIdentifier() is to be used
// As of typescript@3, unescapeIdentifier() removed, thus check in runtime, if the method exists
// tslint:disable-next-line no-unsafe-any strict-boolean-expressions
const unescapeIdentifier: typeof Rule["id"] = (ts as any).unescapeIdentifier || Rule.id;
const propertyName = unescapeIdentifier(argument.text);
ctx.addFailureAtNode(
argument,
Rule.FAILURE_STRING,
Expand Down

0 comments on commit 7ef96b7

Please sign in to comment.