Skip to content

fix(typescript-estree): the token value of an escaped identifier differs with espree #11116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ export function getTokenType(
return AST_TOKEN_TYPES.Identifier;
}

export function unescapeUnicodeIdentifier(text: string): string {
return text.replaceAll(
/\\u\{([0-9a-fA-F]+)\}|\\u([0-9a-fA-F]{4})/g,
(_match: string, curlyHex: string, shortHex: string) => {
const codePoint = parseInt(curlyHex || shortHex, 16);
return String.fromCodePoint(codePoint);
},
);
}
/**
* Extends and formats a given ts.Token, for a given AST
*/
Expand All @@ -589,8 +598,11 @@ export function convertToken(
? token.getFullStart()
: token.getStart(ast);
const end = token.getEnd();
const value = ast.text.slice(start, end);
const tokenType = getTokenType(token);
const value =
token.kind === SyntaxKind.Identifier
? unescapeUnicodeIdentifier(ast.text.slice(start, end))
: ast.text.slice(start, end);
const range: TSESTree.Range = [start, end];
const loc = getLocFor(range, ast);

Expand Down
32 changes: 31 additions & 1 deletion packages/typescript-estree/tests/lib/node-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { unescapeStringLiteralText } from '../../src/node-utils';
import {
unescapeStringLiteralText,
unescapeUnicodeIdentifier,
} from '../../src/node-utils';

describe(unescapeStringLiteralText, () => {
it('should not modify content', () => {
Expand Down Expand Up @@ -42,3 +45,30 @@ describe(unescapeStringLiteralText, () => {
).toBe(`a\n<>"'&©∆&rx;😂\u0000\u0001`);
});
});

describe('unescapeUnicodeIdentifier', () => {
it('should decode simple \\uXXXX escape', () => {
expect(unescapeUnicodeIdentifier('\\u0061')).toBe('a');
expect(unescapeUnicodeIdentifier('\\u0042')).toBe('B');
});

it('should decode \\u{X} escape', () => {
expect(unescapeUnicodeIdentifier('\\u{61}')).toBe('a');
expect(unescapeUnicodeIdentifier('\\u{1F602}')).toBe('😂');
});

it('should decode multiple escapes in one string', () => {
expect(unescapeUnicodeIdentifier('\\u0061\\u0062')).toBe('ab');
expect(unescapeUnicodeIdentifier('\\u{61}\\u{62}')).toBe('ab');
});

it('should leave non-escape text unchanged', () => {
expect(unescapeUnicodeIdentifier('foo')).toBe('foo');
expect(unescapeUnicodeIdentifier('a\\u0062c')).toBe('abc');
});

it('should not decode invalid escapes', () => {
expect(unescapeUnicodeIdentifier('\\u00ZZ')).toBe('\\u00ZZ');
expect(unescapeUnicodeIdentifier('\\u{ZZ}')).toBe('\\u{ZZ}');
});
});
Loading