diff --git a/tsc/internal/parser/parser.go b/tsc/internal/parser/parser.go index a58db02e5c70f..40d77c437fdbe 100644 --- a/tsc/internal/parser/parser.go +++ b/tsc/internal/parser/parser.go @@ -5981,6 +5981,10 @@ func (p *Parser) parseIdentifierNameErrorOnUnicodeEscapeSequence() *ast.Node { if p.scanner.HasUnicodeEscape() || p.scanner.HasExtendedUnicodeEscape() { p.parseErrorAtCurrentToken(diagnostics.Unicode_escape_sequence_cannot_appear_here) } + if p.token == ast.KindPrivateIdentifier { + // tokenIsIdentifierOrKeyword accepts private identifiers, but '#' is not a valid JSX identifier character. + return p.createIdentifierWithDiagnostic(false /*isIdentifier*/, nil /*diagnosticMessage*/, diagnostics.Identifier_expected) + } return p.createIdentifier(tokenIsIdentifierOrKeyword(p.token)) } diff --git a/tsc/internal/scanner/scanner.go b/tsc/internal/scanner/scanner.go index 7dc5199678d93..3b53bd0fb3c37 100644 --- a/tsc/internal/scanner/scanner.go +++ b/tsc/internal/scanner/scanner.go @@ -1370,7 +1370,8 @@ func (s *Scanner) ScanJsxTokenEx(allowMultilineJsxText bool) ast.Kind { // Scans a JSX identifier; these differ from normal identifiers in that they allow dashes func (s *Scanner) ScanJsxIdentifier() ast.Kind { - if tokenIsIdentifierOrKeyword(s.token) { + // Private identifiers are not valid JSX identifiers; leave them for the parser to report. + if tokenIsIdentifierOrKeyword(s.token) && s.token != ast.KindPrivateIdentifier { // An identifier or keyword has already been parsed - check for a `-` or a single instance of `:` and then append it and // everything after it to the token // Do note that this means that `scanJsxIdentifier` effectively _mutates_ the visible token without advancing to a new token diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).errors.txt b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).errors.txt new file mode 100644 index 0000000000000..9697f33d51d91 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).errors.txt @@ -0,0 +1,59 @@ +jsxPrivateIdentifierNames.tsx(8,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(8,12): error TS2304: Cannot find name '#a'. +jsxPrivateIdentifierNames.tsx(8,17): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(8,17): error TS2304: Cannot find name '#a'. +jsxPrivateIdentifierNames.tsx(9,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(9,12): error TS2304: Cannot find name '#b'. +jsxPrivateIdentifierNames.tsx(10,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(11,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(12,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(13,18): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(14,14): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(15,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(16,16): error TS1003: Identifier expected. + + +==== jsxPrivateIdentifierNames.tsx (13 errors) ==== + declare const React: any; + declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; + } + } + + const a = <#a>; + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#a'. + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#a'. + const b = <#b />; + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#b'. + const c =
; + ~~ +!!! error TS1003: Identifier expected. + const d =
; + ~~ +!!! error TS1003: Identifier expected. + const e =
; + ~~ +!!! error TS1003: Identifier expected. + const f =
; + ~~ +!!! error TS1003: Identifier expected. + const g = ; + ~~ +!!! error TS1003: Identifier expected. + const h = <#i:j />; + ~~ +!!! error TS1003: Identifier expected. + const i = ; + +!!! error TS1003: Identifier expected. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).js b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).js new file mode 100644 index 0000000000000..34aeef1037819 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +//// [jsxPrivateIdentifierNames.tsx] +declare const React: any; +declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; + } +} + +const a = <#a>; +const b = <#b />; +const c =
; +const d =
; +const e =
; +const f =
; +const g = ; +const h = <#i:j />; +const i = ; + + +//// [jsxPrivateIdentifierNames.jsx] +"use strict"; +const a = <#a>; +const b = <#b />; +const c =
; +const d =
; +const e =
; +const f =
; +const g = ; +const h = <#i:j />; +const i = ; diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).symbols b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).symbols new file mode 100644 index 0000000000000..08324f72544ac --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).symbols @@ -0,0 +1,53 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +=== jsxPrivateIdentifierNames.tsx === +declare const React: any; +>React : Symbol(React, Decl(jsxPrivateIdentifierNames.tsx, 0, 13)) + +declare namespace JSX { +>JSX : Symbol(JSX, Decl(jsxPrivateIdentifierNames.tsx, 0, 25)) + + interface IntrinsicElements { +>IntrinsicElements : Symbol(IntrinsicElements, Decl(jsxPrivateIdentifierNames.tsx, 1, 23)) + + [name: string]: any; +>name : Symbol(name, Decl(jsxPrivateIdentifierNames.tsx, 3, 9)) + } +} + +const a = <#a>; +>a : Symbol(a, Decl(jsxPrivateIdentifierNames.tsx, 7, 5)) + +const b = <#b />; +>b : Symbol(b, Decl(jsxPrivateIdentifierNames.tsx, 8, 5)) + +const c =
; +>c : Symbol(c, Decl(jsxPrivateIdentifierNames.tsx, 9, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#c : Symbol(#c, Decl(jsxPrivateIdentifierNames.tsx, 9, 14)) + +const d =
; +>d : Symbol(d, Decl(jsxPrivateIdentifierNames.tsx, 10, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#d : Symbol(#d, Decl(jsxPrivateIdentifierNames.tsx, 10, 14)) + +const e =
; +>e : Symbol(e, Decl(jsxPrivateIdentifierNames.tsx, 11, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#e:f : Symbol(#e:f, Decl(jsxPrivateIdentifierNames.tsx, 11, 14)) + +const f =
; +>f : Symbol(f, Decl(jsxPrivateIdentifierNames.tsx, 12, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>f:#g : Symbol(f:#g, Decl(jsxPrivateIdentifierNames.tsx, 12, 14)) + +const g = ; +>g : Symbol(g, Decl(jsxPrivateIdentifierNames.tsx, 13, 5)) + +const h = <#i:j />; +>h : Symbol(h, Decl(jsxPrivateIdentifierNames.tsx, 14, 5)) + +const i = ; +>i : Symbol(i, Decl(jsxPrivateIdentifierNames.tsx, 15, 5)) +>a : Symbol(a, Decl(jsxPrivateIdentifierNames.tsx, 7, 5)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).types b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).types new file mode 100644 index 0000000000000..510d0b5b444e7 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=preserve).types @@ -0,0 +1,71 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +=== jsxPrivateIdentifierNames.tsx === +declare const React: any; +>React : any + +declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; +>name : string + } +} + +const a = <#a>; +>a : any +><#a> : any +>#a : any +>#a : any + +const b = <#b />; +>b : any +><#b /> : any +>#b : any + +const c =
; +>c : any +>
: any +>div : any +>#c : true + +const d =
; +>d : any +>
: any +>div : any +>#d : string + +const e =
; +>e : any +>
: any +>div : any +>#e:f : true +>#e : any +>f : any + +const f =
; +>f : any +>
: any +>div : any +>f:#g : true +>f : any +>#g : any + +const g = ; +>g : any +> : any +>a : any +>#h : any + +const h = <#i:j />; +>h : any +><#i:j /> : any +>#i : any +>j : any + +const i = ; +>i : any +> : any +>a.#k : any +>a : any +> : any + diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).errors.txt b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).errors.txt new file mode 100644 index 0000000000000..9697f33d51d91 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).errors.txt @@ -0,0 +1,59 @@ +jsxPrivateIdentifierNames.tsx(8,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(8,12): error TS2304: Cannot find name '#a'. +jsxPrivateIdentifierNames.tsx(8,17): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(8,17): error TS2304: Cannot find name '#a'. +jsxPrivateIdentifierNames.tsx(9,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(9,12): error TS2304: Cannot find name '#b'. +jsxPrivateIdentifierNames.tsx(10,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(11,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(12,16): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(13,18): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(14,14): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(15,12): error TS1003: Identifier expected. +jsxPrivateIdentifierNames.tsx(16,16): error TS1003: Identifier expected. + + +==== jsxPrivateIdentifierNames.tsx (13 errors) ==== + declare const React: any; + declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; + } + } + + const a = <#a>; + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#a'. + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#a'. + const b = <#b />; + ~~ +!!! error TS1003: Identifier expected. + ~~ +!!! error TS2304: Cannot find name '#b'. + const c =
; + ~~ +!!! error TS1003: Identifier expected. + const d =
; + ~~ +!!! error TS1003: Identifier expected. + const e =
; + ~~ +!!! error TS1003: Identifier expected. + const f =
; + ~~ +!!! error TS1003: Identifier expected. + const g = ; + ~~ +!!! error TS1003: Identifier expected. + const h = <#i:j />; + ~~ +!!! error TS1003: Identifier expected. + const i = ; + +!!! error TS1003: Identifier expected. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).js b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).js new file mode 100644 index 0000000000000..35c07dc568477 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +//// [jsxPrivateIdentifierNames.tsx] +declare const React: any; +declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; + } +} + +const a = <#a>; +const b = <#b />; +const c =
; +const d =
; +const e =
; +const f =
; +const g = ; +const h = <#i:j />; +const i = ; + + +//// [jsxPrivateIdentifierNames.js] +"use strict"; +const a = React.createElement(#a, null); +const b = React.createElement(#b, null); +const c = React.createElement("div", { "#c": true }); +const d = React.createElement("div", { "#d": "d" }); +const e = React.createElement("div", { "#e:f": true }); +const f = React.createElement("div", { "f:#g": true }); +const g = React.createElement("a:#h", null); +const h = React.createElement("#i:j", null); +const i = React.createElement(a., null); diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).symbols b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).symbols new file mode 100644 index 0000000000000..08324f72544ac --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).symbols @@ -0,0 +1,53 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +=== jsxPrivateIdentifierNames.tsx === +declare const React: any; +>React : Symbol(React, Decl(jsxPrivateIdentifierNames.tsx, 0, 13)) + +declare namespace JSX { +>JSX : Symbol(JSX, Decl(jsxPrivateIdentifierNames.tsx, 0, 25)) + + interface IntrinsicElements { +>IntrinsicElements : Symbol(IntrinsicElements, Decl(jsxPrivateIdentifierNames.tsx, 1, 23)) + + [name: string]: any; +>name : Symbol(name, Decl(jsxPrivateIdentifierNames.tsx, 3, 9)) + } +} + +const a = <#a>; +>a : Symbol(a, Decl(jsxPrivateIdentifierNames.tsx, 7, 5)) + +const b = <#b />; +>b : Symbol(b, Decl(jsxPrivateIdentifierNames.tsx, 8, 5)) + +const c =
; +>c : Symbol(c, Decl(jsxPrivateIdentifierNames.tsx, 9, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#c : Symbol(#c, Decl(jsxPrivateIdentifierNames.tsx, 9, 14)) + +const d =
; +>d : Symbol(d, Decl(jsxPrivateIdentifierNames.tsx, 10, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#d : Symbol(#d, Decl(jsxPrivateIdentifierNames.tsx, 10, 14)) + +const e =
; +>e : Symbol(e, Decl(jsxPrivateIdentifierNames.tsx, 11, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>#e:f : Symbol(#e:f, Decl(jsxPrivateIdentifierNames.tsx, 11, 14)) + +const f =
; +>f : Symbol(f, Decl(jsxPrivateIdentifierNames.tsx, 12, 5)) +>div : Symbol(JSX.IntrinsicElements.__index, Decl(jsxPrivateIdentifierNames.tsx, 2, 33)) +>f:#g : Symbol(f:#g, Decl(jsxPrivateIdentifierNames.tsx, 12, 14)) + +const g = ; +>g : Symbol(g, Decl(jsxPrivateIdentifierNames.tsx, 13, 5)) + +const h = <#i:j />; +>h : Symbol(h, Decl(jsxPrivateIdentifierNames.tsx, 14, 5)) + +const i = ; +>i : Symbol(i, Decl(jsxPrivateIdentifierNames.tsx, 15, 5)) +>a : Symbol(a, Decl(jsxPrivateIdentifierNames.tsx, 7, 5)) + diff --git a/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).types b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).types new file mode 100644 index 0000000000000..510d0b5b444e7 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/jsxPrivateIdentifierNames(jsx=react).types @@ -0,0 +1,71 @@ +//// [tests/cases/compiler/jsxPrivateIdentifierNames.tsx] //// + +=== jsxPrivateIdentifierNames.tsx === +declare const React: any; +>React : any + +declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; +>name : string + } +} + +const a = <#a>; +>a : any +><#a> : any +>#a : any +>#a : any + +const b = <#b />; +>b : any +><#b /> : any +>#b : any + +const c =
; +>c : any +>
: any +>div : any +>#c : true + +const d =
; +>d : any +>
: any +>div : any +>#d : string + +const e =
; +>e : any +>
: any +>div : any +>#e:f : true +>#e : any +>f : any + +const f =
; +>f : any +>
: any +>div : any +>f:#g : true +>f : any +>#g : any + +const g = ; +>g : any +> : any +>a : any +>#h : any + +const h = <#i:j />; +>h : any +><#i:j /> : any +>#i : any +>j : any + +const i = ; +>i : any +> : any +>a.#k : any +>a : any +> : any + diff --git a/tsc/testdata/tests/cases/compiler/jsxPrivateIdentifierNames.tsx b/tsc/testdata/tests/cases/compiler/jsxPrivateIdentifierNames.tsx new file mode 100644 index 0000000000000..8f6eee12c9cae --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/jsxPrivateIdentifierNames.tsx @@ -0,0 +1,19 @@ +// @target: es2015 +// @jsx: preserve,react + +declare const React: any; +declare namespace JSX { + interface IntrinsicElements { + [name: string]: any; + } +} + +const a = <#a>; +const b = <#b />; +const c =
; +const d =
; +const e =
; +const f =
; +const g = ; +const h = <#i:j />; +const i = ;