Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dbarabashh wants to merge3 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromdbarabashh:fix-token-identifier-unicode
Open
Show file tree
Hide file tree
Changes fromall 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 deletionpackages/typescript-estree/src/node-utils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
},
);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Question] I'm not very familiar with these\u shenanigans and don't particularly feel a drive to be. Is there a standard package that does this? If not, maybe it would make sense to make one?

/**
* 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 deletionpackages/typescript-estree/tests/lib/node-utils.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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}');
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Testing] Nit: I'm a fan of having one thing under test perit() case. I think it would make sense to split each of these two-paired ones into twoit()s. But I wouldn't block on it, this file already doesn't align to that (and even more so in existing code!).

Loading

[8]ページ先頭

©2009-2025 Movatter.jp