From 09782f13f20720a1ad330e4e61c885ee8984978b Mon Sep 17 00:00:00 2001 From: Pavel Birukov Date: Mon, 9 Jul 2018 18:30:38 +0300 Subject: [PATCH] Add runtime check for 'ts.unescapeIdentifier()' presence --- src/rules/noStringLiteralRule.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/rules/noStringLiteralRule.ts b/src/rules/noStringLiteralRule.ts index 7c1477dae0a..cb8575731a9 100644 --- a/src/rules/noStringLiteralRule.ts +++ b/src/rules/noStringLiteralRule.ts @@ -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); } @@ -52,8 +56,12 @@ 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 + // 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,