Skip to content

Commit b2de531

Browse files
committed
chore: disallows unicode escape sequence
1 parent b45cf0d commit b2de531

File tree

7 files changed

+194
-67
lines changed

7 files changed

+194
-67
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6322,6 +6322,10 @@
63226322
"category": "Error",
63236323
"code": 17018
63246324
},
6325+
"Unicode escape sequence cannot appear here.": {
6326+
"category": "Error",
6327+
"code": 17019
6328+
},
63256329
"Circularity detected while resolving configuration: {0}": {
63266330
"category": "Error",
63276331
"code": 18000

src/compiler/parser.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,13 @@ namespace ts {
19911991
return createIdentifier(tokenIsIdentifierOrKeyword(token()), diagnosticMessage);
19921992
}
19931993

1994+
function parseIdentifierNameErrorOnUnicodeEscapeSequence(): Identifier {
1995+
if (scanner.hasUnicodeEscape() || scanner.hasExtendedUnicodeEscape()) {
1996+
parseErrorAtCurrentToken(Diagnostics.Unicode_escape_sequence_cannot_appear_here);
1997+
}
1998+
return createIdentifier(tokenIsIdentifierOrKeyword(token()));
1999+
}
2000+
19942001
function isLiteralPropertyName(): boolean {
19952002
return tokenIsIdentifierOrKeyword(token()) ||
19962003
token() === SyntaxKind.StringLiteral ||
@@ -2855,7 +2862,7 @@ namespace ts {
28552862
return finishNode(factory.createQualifiedName(entity, name), entity.pos);
28562863
}
28572864

2858-
function parseRightSideOfDot(allowIdentifierNames: boolean, allowPrivateIdentifiers: boolean): Identifier | PrivateIdentifier {
2865+
function parseRightSideOfDot(allowIdentifierNames: boolean, allowPrivateIdentifiers: boolean, allowUnicodeEscapeSequenceInIdentifierName = true): Identifier | PrivateIdentifier {
28592866
// Technically a keyword is valid here as all identifiers and keywords are identifier names.
28602867
// However, often we'll encounter this in error situations when the identifier or keyword
28612868
// is actually starting another valid construct.
@@ -2891,7 +2898,11 @@ namespace ts {
28912898
return allowPrivateIdentifiers ? node : createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics.Identifier_expected);
28922899
}
28932900

2894-
return allowIdentifierNames ? parseIdentifierName() : parseIdentifier();
2901+
if (allowIdentifierNames) {
2902+
return allowUnicodeEscapeSequenceInIdentifierName ? parseIdentifierName() : parseIdentifierNameErrorOnUnicodeEscapeSequence();
2903+
}
2904+
2905+
return parseIdentifier();
28952906
}
28962907

28972908
function parseTemplateSpans(isTaggedTemplate: boolean) {
@@ -5425,9 +5436,9 @@ namespace ts {
54255436
// We can't just simply use parseLeftHandSideExpressionOrHigher because then we will start consider class,function etc as a keyword
54265437
// We only want to consider "this" as a primaryExpression
54275438
let expression: JsxTagNameExpression = token() === SyntaxKind.ThisKeyword ?
5428-
parseTokenNode<ThisExpression>() : parseIdentifierName();
5439+
parseTokenNode<ThisExpression>() : parseIdentifierNameErrorOnUnicodeEscapeSequence();
54295440
while (parseOptional(SyntaxKind.DotToken)) {
5430-
expression = finishNode(factory.createPropertyAccessExpression(expression, parseRightSideOfDot(/*allowIdentifierNames*/ true, /*allowPrivateIdentifiers*/ false)), pos) as JsxTagNamePropertyAccess;
5441+
expression = finishNode(factory.createPropertyAccessExpression(expression, parseRightSideOfDot(/*allowIdentifierNames*/ true, /*allowPrivateIdentifiers*/ false, /** allowUnicodeEscapeSequenceInIdentifierName */ false)), pos) as JsxTagNamePropertyAccess;
54315442
}
54325443
return expression;
54335444
}
@@ -5468,7 +5479,7 @@ namespace ts {
54685479
const pos = getNodePos();
54695480
return finishNode(
54705481
factory.createJsxAttribute(
5471-
parseIdentifierName(),
5482+
parseIdentifierNameErrorOnUnicodeEscapeSequence(),
54725483
token() !== SyntaxKind.EqualsToken ? undefined :
54735484
scanJsxAttributeValue() === SyntaxKind.StringLiteral ? parseLiteralNode() as StringLiteral :
54745485
parseJsxExpression(/*inExpressionContext*/ true)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
tests/cases/conformance/jsx/file.tsx(15,4): error TS17019: Unicode escape sequence cannot appear here.
2+
tests/cases/conformance/jsx/file.tsx(16,4): error TS17019: Unicode escape sequence cannot appear here.
3+
tests/cases/conformance/jsx/file.tsx(17,4): error TS17019: Unicode escape sequence cannot appear here.
4+
tests/cases/conformance/jsx/file.tsx(18,4): error TS17019: Unicode escape sequence cannot appear here.
5+
tests/cases/conformance/jsx/file.tsx(19,6): error TS17019: Unicode escape sequence cannot appear here.
6+
tests/cases/conformance/jsx/file.tsx(20,4): error TS17019: Unicode escape sequence cannot appear here.
7+
tests/cases/conformance/jsx/file.tsx(21,4): error TS17019: Unicode escape sequence cannot appear here.
8+
tests/cases/conformance/jsx/file.tsx(22,4): error TS17019: Unicode escape sequence cannot appear here.
9+
tests/cases/conformance/jsx/file.tsx(23,4): error TS17019: Unicode escape sequence cannot appear here.
10+
tests/cases/conformance/jsx/file.tsx(26,9): error TS17019: Unicode escape sequence cannot appear here.
11+
tests/cases/conformance/jsx/file.tsx(27,9): error TS17019: Unicode escape sequence cannot appear here.
12+
13+
14+
==== tests/cases/conformance/jsx/file.tsx (11 errors) ====
15+
import * as React from "react";
16+
declare global {
17+
namespace JSX {
18+
interface IntrinsicElements {
19+
"a-b": any;
20+
"a-c": any;
21+
}
22+
}
23+
}
24+
const Compa = (x: {x: number}) => <div>{"" + x}</div>;
25+
const x = { video: () => null }
26+
27+
// unicode escape sequence is not allowed in tag name or JSX attribute name.
28+
// tag name:
29+
; <\u0061></a>
30+
~~~~~~
31+
!!! error TS17019: Unicode escape sequence cannot appear here.
32+
; <\u0061-b></a-b>
33+
~~~~~~~~
34+
!!! error TS17019: Unicode escape sequence cannot appear here.
35+
; <a-\u0063></a-c>
36+
~~~~~~~~
37+
!!! error TS17019: Unicode escape sequence cannot appear here.
38+
; <Comp\u0061 x={12} />
39+
~~~~~~~~~~
40+
!!! error TS17019: Unicode escape sequence cannot appear here.
41+
; <x.\u0076ideo />
42+
~~~~~~~~~~
43+
!!! error TS17019: Unicode escape sequence cannot appear here.
44+
; <\u{0061}></a>
45+
~~~~~~~~
46+
!!! error TS17019: Unicode escape sequence cannot appear here.
47+
; <\u{0061}-b></a-b>
48+
~~~~~~~~~~
49+
!!! error TS17019: Unicode escape sequence cannot appear here.
50+
; <a-\u{0063}></a-c>
51+
~~~~~~~~~~
52+
!!! error TS17019: Unicode escape sequence cannot appear here.
53+
; <Comp\u{0061} x={12} />
54+
~~~~~~~~~~~~
55+
!!! error TS17019: Unicode escape sequence cannot appear here.
56+
57+
// attribute name
58+
;<video data-\u0076ideo />
59+
~~~~~~~~~~~~~~~
60+
!!! error TS17019: Unicode escape sequence cannot appear here.
61+
;<video \u0073rc="" />
62+
~~~~~~~~
63+
!!! error TS17019: Unicode escape sequence cannot appear here.
64+

tests/baselines/reference/unicodeEscapesInJsxtags.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,37 @@ declare global {
99
}
1010
}
1111
const Compa = (x: {x: number}) => <div>{"" + x}</div>;
12+
const x = { video: () => null }
1213

13-
let a = <\u0061></a>; // works
14-
let ab = <\u0061-b></a-b>; // works
15-
let ac = <a-\u0063></a-c>; // works
16-
let compa = <Comp\u0061 x={12} />; // works
14+
// unicode escape sequence is not allowed in tag name or JSX attribute name.
15+
// tag name:
16+
; <\u0061></a>
17+
; <\u0061-b></a-b>
18+
; <a-\u0063></a-c>
19+
; <Comp\u0061 x={12} />
20+
; <x.\u0076ideo />
21+
; <\u{0061}></a>
22+
; <\u{0061}-b></a-b>
23+
; <a-\u{0063}></a-c>
24+
; <Comp\u{0061} x={12} />
1725

18-
let a2 = <\u{0061}></a>; // works
19-
let ab2 = <\u{0061}-b></a-b>; // works
20-
let ac2 = <a-\u{0063}></a-c>; // works
21-
let compa2 = <Comp\u{0061} x={12} />; // works
26+
// attribute name
27+
;<video data-\u0076ideo />
28+
;<video \u0073rc="" />
2229

2330

2431
//// [file.js]
2532
import * as React from "react";
2633
const Compa = (x) => React.createElement("div", null, "" + x);
27-
let a = React.createElement("a", null); // works
28-
let ab = React.createElement("a-b", null); // works
29-
let ac = React.createElement("a-c", null); // works
30-
let compa = React.createElement(Comp\u0061, { x: 12 }); // works
31-
let a2 = React.createElement("a", null); // works
32-
let ab2 = React.createElement("a-b", null); // works
33-
let ac2 = React.createElement("a-c", null); // works
34-
let compa2 = React.createElement(Comp\u{0061}, { x: 12 }); // works
34+
const x = { video: () => null };
35+
React.createElement("a", null);
36+
React.createElement("a-b", null);
37+
React.createElement("a-c", null);
38+
React.createElement(Comp\u0061, { x: 12 });
39+
React.createElement(x.\u0076ideo, null);
40+
React.createElement("a", null);
41+
React.createElement("a-b", null);
42+
React.createElement("a-c", null);
43+
React.createElement(Comp\u{0061}, { x: 12 });
44+
React.createElement("video", { "data-video": true });
45+
React.createElement("video", { \u0073rc: "" });

tests/baselines/reference/unicodeEscapesInJsxtags.symbols

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,55 @@ const Compa = (x: {x: number}) => <div>{"" + x}</div>;
2727
>x : Symbol(x, Decl(file.tsx, 9, 15))
2828
>div : Symbol(JSX.IntrinsicElements.div, Decl(react.d.ts, 2400, 45))
2929

30-
let a = <\u0061></a>; // works
31-
>a : Symbol(a, Decl(file.tsx, 11, 3))
30+
const x = { video: () => null }
31+
>x : Symbol(x, Decl(file.tsx, 10, 5))
32+
>video : Symbol(video, Decl(file.tsx, 10, 11))
33+
34+
// unicode escape sequence is not allowed in tag name or JSX attribute name.
35+
// tag name:
36+
; <\u0061></a>
3237
>\u0061 : Symbol(JSX.IntrinsicElements.a, Decl(react.d.ts, 2370, 33))
3338
>a : Symbol(JSX.IntrinsicElements.a, Decl(react.d.ts, 2370, 33))
3439

35-
let ab = <\u0061-b></a-b>; // works
36-
>ab : Symbol(ab, Decl(file.tsx, 12, 3))
40+
; <\u0061-b></a-b>
3741
>\u0061-b : Symbol(JSX.IntrinsicElements["a-b"], Decl(file.tsx, 3, 37))
3842
>a-b : Symbol(JSX.IntrinsicElements["a-b"], Decl(file.tsx, 3, 37))
3943

40-
let ac = <a-\u0063></a-c>; // works
41-
>ac : Symbol(ac, Decl(file.tsx, 13, 3))
44+
; <a-\u0063></a-c>
4245
>a-\u0063 : Symbol(JSX.IntrinsicElements["a-c"], Decl(file.tsx, 4, 23))
4346
>a-c : Symbol(JSX.IntrinsicElements["a-c"], Decl(file.tsx, 4, 23))
4447

45-
let compa = <Comp\u0061 x={12} />; // works
46-
>compa : Symbol(compa, Decl(file.tsx, 14, 3))
48+
; <Comp\u0061 x={12} />
4749
>Comp\u0061 : Symbol(Compa, Decl(file.tsx, 9, 5))
48-
>x : Symbol(x, Decl(file.tsx, 14, 23))
50+
>x : Symbol(x, Decl(file.tsx, 17, 13))
51+
52+
; <x.\u0076ideo />
53+
>x.\u0076ideo : Symbol(video, Decl(file.tsx, 10, 11))
54+
>x : Symbol(x, Decl(file.tsx, 10, 5))
55+
>\u0076ideo : Symbol(video, Decl(file.tsx, 10, 11))
4956

50-
let a2 = <\u{0061}></a>; // works
51-
>a2 : Symbol(a2, Decl(file.tsx, 16, 3))
57+
; <\u{0061}></a>
5258
>\u{0061} : Symbol(JSX.IntrinsicElements.a, Decl(react.d.ts, 2370, 33))
5359
>a : Symbol(JSX.IntrinsicElements.a, Decl(react.d.ts, 2370, 33))
5460

55-
let ab2 = <\u{0061}-b></a-b>; // works
56-
>ab2 : Symbol(ab2, Decl(file.tsx, 17, 3))
61+
; <\u{0061}-b></a-b>
5762
>\u{0061}-b : Symbol(JSX.IntrinsicElements["a-b"], Decl(file.tsx, 3, 37))
5863
>a-b : Symbol(JSX.IntrinsicElements["a-b"], Decl(file.tsx, 3, 37))
5964

60-
let ac2 = <a-\u{0063}></a-c>; // works
61-
>ac2 : Symbol(ac2, Decl(file.tsx, 18, 3))
65+
; <a-\u{0063}></a-c>
6266
>a-\u{0063} : Symbol(JSX.IntrinsicElements["a-c"], Decl(file.tsx, 4, 23))
6367
>a-c : Symbol(JSX.IntrinsicElements["a-c"], Decl(file.tsx, 4, 23))
6468

65-
let compa2 = <Comp\u{0061} x={12} />; // works
66-
>compa2 : Symbol(compa2, Decl(file.tsx, 19, 3))
69+
; <Comp\u{0061} x={12} />
6770
>Comp\u{0061} : Symbol(Compa, Decl(file.tsx, 9, 5))
68-
>x : Symbol(x, Decl(file.tsx, 19, 26))
71+
>x : Symbol(x, Decl(file.tsx, 22, 15))
72+
73+
// attribute name
74+
;<video data-\u0076ideo />
75+
>video : Symbol(JSX.IntrinsicElements.video, Decl(react.d.ts, 2481, 44))
76+
>data-\u0076ideo : Symbol(data-\u0076ideo, Decl(file.tsx, 25, 7))
77+
78+
;<video \u0073rc="" />
79+
>video : Symbol(JSX.IntrinsicElements.video, Decl(react.d.ts, 2481, 44))
80+
>\u0073rc : Symbol(\u0073rc, Decl(file.tsx, 26, 7))
6981

tests/baselines/reference/unicodeEscapesInJsxtags.types

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,71 @@ const Compa = (x: {x: number}) => <div>{"" + x}</div>;
2727
>x : { x: number; }
2828
>div : any
2929

30-
let a = <\u0061></a>; // works
31-
>a : JSX.Element
30+
const x = { video: () => null }
31+
>x : { video: () => any; }
32+
>{ video: () => null } : { video: () => any; }
33+
>video : () => any
34+
>() => null : () => any
35+
>null : null
36+
37+
// unicode escape sequence is not allowed in tag name or JSX attribute name.
38+
// tag name:
39+
; <\u0061></a>
3240
><\u0061></a> : JSX.Element
33-
>\u0061 : JSX.Element
34-
>a : JSX.Element
41+
>\u0061 : any
42+
>a : any
3543

36-
let ab = <\u0061-b></a-b>; // works
37-
>ab : JSX.Element
44+
; <\u0061-b></a-b>
3845
><\u0061-b></a-b> : JSX.Element
3946
>\u0061-b : any
4047
>a-b : any
4148

42-
let ac = <a-\u0063></a-c>; // works
43-
>ac : JSX.Element
49+
; <a-\u0063></a-c>
4450
><a-\u0063></a-c> : JSX.Element
4551
>a-\u0063 : any
4652
>a-c : any
4753

48-
let compa = <Comp\u0061 x={12} />; // works
49-
>compa : JSX.Element
54+
; <Comp\u0061 x={12} />
5055
><Comp\u0061 x={12} /> : JSX.Element
5156
>Comp\u0061 : (x: { x: number; }) => JSX.Element
5257
>x : number
5358
>12 : 12
5459

55-
let a2 = <\u{0061}></a>; // works
56-
>a2 : JSX.Element
60+
; <x.\u0076ideo />
61+
><x.\u0076ideo /> : JSX.Element
62+
>x.\u0076ideo : () => any
63+
>x : { video: () => any; }
64+
>\u0076ideo : () => any
65+
66+
; <\u{0061}></a>
5767
><\u{0061}></a> : JSX.Element
58-
>\u{0061} : JSX.Element
59-
>a : JSX.Element
68+
>\u{0061} : any
69+
>a : any
6070

61-
let ab2 = <\u{0061}-b></a-b>; // works
62-
>ab2 : JSX.Element
71+
; <\u{0061}-b></a-b>
6372
><\u{0061}-b></a-b> : JSX.Element
6473
>\u{0061}-b : any
6574
>a-b : any
6675

67-
let ac2 = <a-\u{0063}></a-c>; // works
68-
>ac2 : JSX.Element
76+
; <a-\u{0063}></a-c>
6977
><a-\u{0063}></a-c> : JSX.Element
7078
>a-\u{0063} : any
7179
>a-c : any
7280

73-
let compa2 = <Comp\u{0061} x={12} />; // works
74-
>compa2 : JSX.Element
81+
; <Comp\u{0061} x={12} />
7582
><Comp\u{0061} x={12} /> : JSX.Element
7683
>Comp\u{0061} : (x: { x: number; }) => JSX.Element
7784
>x : number
7885
>12 : 12
7986

87+
// attribute name
88+
;<video data-\u0076ideo />
89+
><video data-\u0076ideo /> : JSX.Element
90+
>video : any
91+
>data-\u0076ideo : true
92+
93+
;<video \u0073rc="" />
94+
><video \u0073rc="" /> : JSX.Element
95+
>video : any
96+
>\u0073rc : string
97+

tests/cases/conformance/jsx/unicodeEscapesInJsxtags.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ declare global {
1515
}
1616
}
1717
const Compa = (x: {x: number}) => <div>{"" + x}</div>;
18+
const x = { video: () => null }
1819

19-
let a = <\u0061></a>; // works
20-
let ab = <\u0061-b></a-b>; // works
21-
let ac = <a-\u0063></a-c>; // works
22-
let compa = <Comp\u0061 x={12} />; // works
20+
// unicode escape sequence is not allowed in tag name or JSX attribute name.
21+
// tag name:
22+
; <\u0061></a>
23+
; <\u0061-b></a-b>
24+
; <a-\u0063></a-c>
25+
; <Comp\u0061 x={12} />
26+
; <x.\u0076ideo />
27+
; <\u{0061}></a>
28+
; <\u{0061}-b></a-b>
29+
; <a-\u{0063}></a-c>
30+
; <Comp\u{0061} x={12} />
2331

24-
let a2 = <\u{0061}></a>; // works
25-
let ab2 = <\u{0061}-b></a-b>; // works
26-
let ac2 = <a-\u{0063}></a-c>; // works
27-
let compa2 = <Comp\u{0061} x={12} />; // works
32+
// attribute name
33+
;<video data-\u0076ideo />
34+
;<video \u0073rc="" />

0 commit comments

Comments
 (0)