Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}, | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [Question] I'm not very familiar with these | ||
/** | ||
* Extends and formats a given ts.Token, for a given AST | ||
*/ | ||
@@ -589,8 +598,11 @@ export function convertToken( | ||
? token.getFullStart() | ||
: token.getStart(ast); | ||
const end = token.getEnd(); | ||
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); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { | ||
unescapeStringLiteralText, | ||
unescapeUnicodeIdentifier, | ||
} from '../../src/node-utils'; | ||
describe(unescapeStringLiteralText, () => { | ||
it('should not modify content', () => { | ||
@@ -42,3 +45,30 @@ describe(unescapeStringLiteralText, () => { | ||
).toBe(`a\n<>"'&©∆℞😂\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}'); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [Testing] Nit: I'm a fan of having one thing under test per |
Uh oh!
There was an error while loading.Please reload this page.