mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
Prevent valid JSX from being seen as the start of a generic arrow function, fix crashes (#52450)
This commit is contained in:
@@ -5201,6 +5201,7 @@ namespace Parser {
|
||||
switch (fourth) {
|
||||
case SyntaxKind.EqualsToken:
|
||||
case SyntaxKind.GreaterThanToken:
|
||||
case SyntaxKind.SlashToken:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
|
||||
@@ -9079,7 +9079,7 @@ export function rangeOfNode(node: Node): TextRange {
|
||||
export function rangeOfTypeParameters(sourceFile: SourceFile, typeParameters: NodeArray<TypeParameterDeclaration>): TextRange {
|
||||
// Include the `<>`
|
||||
const pos = typeParameters.pos - 1;
|
||||
const end = skipTrivia(sourceFile.text, typeParameters.end) + 1;
|
||||
const end = Math.min(sourceFile.text.length, skipTrivia(sourceFile.text, typeParameters.end) + 1);
|
||||
return { pos, end };
|
||||
}
|
||||
|
||||
|
||||
18
tests/baselines/reference/parseJsxExtends1.js
Normal file
18
tests/baselines/reference/parseJsxExtends1.js
Normal file
@@ -0,0 +1,18 @@
|
||||
//// [index.tsx]
|
||||
declare const React: any;
|
||||
|
||||
export function Foo() {
|
||||
// No error; "const" is lowercase and therefore intrinsic.
|
||||
return <const T extends/>
|
||||
}
|
||||
|
||||
|
||||
//// [index.js]
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Foo = void 0;
|
||||
function Foo() {
|
||||
// No error; "const" is lowercase and therefore intrinsic.
|
||||
return React.createElement("const", { T: true, extends: true });
|
||||
}
|
||||
exports.Foo = Foo;
|
||||
13
tests/baselines/reference/parseJsxExtends1.symbols
Normal file
13
tests/baselines/reference/parseJsxExtends1.symbols
Normal file
@@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.tsx ===
|
||||
declare const React: any;
|
||||
>React : Symbol(React, Decl(index.tsx, 0, 13))
|
||||
|
||||
export function Foo() {
|
||||
>Foo : Symbol(Foo, Decl(index.tsx, 0, 25))
|
||||
|
||||
// No error; "const" is lowercase and therefore intrinsic.
|
||||
return <const T extends/>
|
||||
>T : Symbol(T, Decl(index.tsx, 4, 17))
|
||||
>extends : Symbol(extends, Decl(index.tsx, 4, 19))
|
||||
}
|
||||
|
||||
15
tests/baselines/reference/parseJsxExtends1.types
Normal file
15
tests/baselines/reference/parseJsxExtends1.types
Normal file
@@ -0,0 +1,15 @@
|
||||
=== tests/cases/compiler/index.tsx ===
|
||||
declare const React: any;
|
||||
>React : any
|
||||
|
||||
export function Foo() {
|
||||
>Foo : () => any
|
||||
|
||||
// No error; "const" is lowercase and therefore intrinsic.
|
||||
return <const T extends/>
|
||||
><const T extends/> : error
|
||||
>const : any
|
||||
>T : true
|
||||
>extends : true
|
||||
}
|
||||
|
||||
13
tests/baselines/reference/parseJsxExtends2.errors.txt
Normal file
13
tests/baselines/reference/parseJsxExtends2.errors.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
tests/cases/compiler/index.tsx(5,13): error TS2304: Cannot find name 'T'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/index.tsx (1 errors) ====
|
||||
declare const React: any;
|
||||
|
||||
export function Foo() {
|
||||
// Error: T is not declared.
|
||||
return <T extends/>
|
||||
~
|
||||
!!! error TS2304: Cannot find name 'T'.
|
||||
}
|
||||
|
||||
18
tests/baselines/reference/parseJsxExtends2.js
Normal file
18
tests/baselines/reference/parseJsxExtends2.js
Normal file
@@ -0,0 +1,18 @@
|
||||
//// [index.tsx]
|
||||
declare const React: any;
|
||||
|
||||
export function Foo() {
|
||||
// Error: T is not declared.
|
||||
return <T extends/>
|
||||
}
|
||||
|
||||
|
||||
//// [index.js]
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Foo = void 0;
|
||||
function Foo() {
|
||||
// Error: T is not declared.
|
||||
return React.createElement(T, { extends: true });
|
||||
}
|
||||
exports.Foo = Foo;
|
||||
12
tests/baselines/reference/parseJsxExtends2.symbols
Normal file
12
tests/baselines/reference/parseJsxExtends2.symbols
Normal file
@@ -0,0 +1,12 @@
|
||||
=== tests/cases/compiler/index.tsx ===
|
||||
declare const React: any;
|
||||
>React : Symbol(React, Decl(index.tsx, 0, 13))
|
||||
|
||||
export function Foo() {
|
||||
>Foo : Symbol(Foo, Decl(index.tsx, 0, 25))
|
||||
|
||||
// Error: T is not declared.
|
||||
return <T extends/>
|
||||
>extends : Symbol(extends, Decl(index.tsx, 4, 13))
|
||||
}
|
||||
|
||||
14
tests/baselines/reference/parseJsxExtends2.types
Normal file
14
tests/baselines/reference/parseJsxExtends2.types
Normal file
@@ -0,0 +1,14 @@
|
||||
=== tests/cases/compiler/index.tsx ===
|
||||
declare const React: any;
|
||||
>React : any
|
||||
|
||||
export function Foo() {
|
||||
>Foo : () => any
|
||||
|
||||
// Error: T is not declared.
|
||||
return <T extends/>
|
||||
><T extends/> : any
|
||||
>T : any
|
||||
>extends : true
|
||||
}
|
||||
|
||||
9
tests/cases/compiler/parseJsxExtends1.ts
Normal file
9
tests/cases/compiler/parseJsxExtends1.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// @jsx: react
|
||||
// @filename: index.tsx
|
||||
|
||||
declare const React: any;
|
||||
|
||||
export function Foo() {
|
||||
// No error; "const" is lowercase and therefore intrinsic.
|
||||
return <const T extends/>
|
||||
}
|
||||
9
tests/cases/compiler/parseJsxExtends2.ts
Normal file
9
tests/cases/compiler/parseJsxExtends2.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// @jsx: react
|
||||
// @filename: index.tsx
|
||||
|
||||
declare const React: any;
|
||||
|
||||
export function Foo() {
|
||||
// Error: T is not declared.
|
||||
return <T extends/>
|
||||
}
|
||||
6
tests/cases/fourslash/jsxElementExtendsNoCrash1.ts
Normal file
6
tests/cases/fourslash/jsxElementExtendsNoCrash1.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @filename: index.tsx
|
||||
//// <const T extends/>
|
||||
|
||||
verify.getSuggestionDiagnostics([]);
|
||||
6
tests/cases/fourslash/jsxElementExtendsNoCrash2.ts
Normal file
6
tests/cases/fourslash/jsxElementExtendsNoCrash2.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @filename: index.tsx
|
||||
//// <T extends/>
|
||||
|
||||
verify.getSuggestionDiagnostics([]);
|
||||
6
tests/cases/fourslash/jsxElementExtendsNoCrash3.ts
Normal file
6
tests/cases/fourslash/jsxElementExtendsNoCrash3.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @filename: index.tsx
|
||||
//// <T extends /=>
|
||||
|
||||
verify.getSuggestionDiagnostics([]);
|
||||
Reference in New Issue
Block a user