Prevent valid JSX from being seen as the start of a generic arrow function, fix crashes (#52450)

This commit is contained in:
Jake Bailey
2023-01-31 16:55:48 -08:00
committed by GitHub
parent 3d07795092
commit 95840557bf
14 changed files with 141 additions and 1 deletions

View File

@@ -5201,6 +5201,7 @@ namespace Parser {
switch (fourth) {
case SyntaxKind.EqualsToken:
case SyntaxKind.GreaterThanToken:
case SyntaxKind.SlashToken:
return false;
default:
return true;

View File

@@ -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 };
}

View 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;

View 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))
}

View 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
}

View 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'.
}

View 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;

View 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))
}

View 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
}

View 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/>
}

View 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/>
}

View File

@@ -0,0 +1,6 @@
/// <reference path="fourslash.ts" />
// @filename: index.tsx
//// <const T extends/>
verify.getSuggestionDiagnostics([]);

View File

@@ -0,0 +1,6 @@
/// <reference path="fourslash.ts" />
// @filename: index.tsx
//// <T extends/>
verify.getSuggestionDiagnostics([]);

View File

@@ -0,0 +1,6 @@
/// <reference path="fourslash.ts" />
// @filename: index.tsx
//// <T extends /=>
verify.getSuggestionDiagnostics([]);