-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importFixes: Support missing "React" at a JSXOpeningElement #16066
Changes from 1 commit
4881b1b
dbb4d46
e31b4e7
6905639
0ce998b
1aaade7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2385,7 +2385,10 @@ namespace FourSlash { | |
const codeFixes = this.getCodeFixActions(this.activeFile.fileName, errorCode); | ||
|
||
if (!codeFixes || codeFixes.length === 0) { | ||
this.raiseError("No codefixes returned."); | ||
if (expectedTextArray.length !== 0) { | ||
this.raiseError("No codefixes returned."); | ||
} | ||
return; | ||
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. I don't understand why this change was made. 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.
|
||
} | ||
|
||
const actualTextArray: string[] = []; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,8 +138,22 @@ namespace ts.codefix { | |
|
||
const currentTokenMeaning = getMeaningFromLocation(token); | ||
if (context.errorCode === Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead.code) { | ||
const symbol = checker.getAliasedSymbol(checker.getSymbolAtLocation(token)); | ||
return getCodeActionForImport(symbol, /*isDefault*/ false, /*isNamespaceImport*/ true); | ||
const umdSymbol = checker.getSymbolAtLocation(token); | ||
let symbol: ts.Symbol; | ||
let symbolName: string; | ||
if (umdSymbol.flags & ts.SymbolFlags.Alias) { | ||
symbol = checker.getAliasedSymbol(umdSymbol); | ||
symbolName = name; | ||
} else if (isJsxOpeningLikeElement(token.parent) && token.parent.tagName === token) { | ||
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.
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. D'oh, linter was doing nothing. #16078 |
||
// The error wasn't for the symbolAtLocation, it was for the JSX tag itself, which needs access to e.g. `React`. | ||
symbol = checker.getAliasedSymbol(checker.getJsxNamespaceSymbol(token)); | ||
symbolName = symbol.name; | ||
} | ||
else { | ||
Debug.fail("Either the symbol or the JSX namespace should be a UMD global if we got here"); | ||
} | ||
|
||
return getCodeActionForImport(symbol, symbolName, /*isDefault*/ false, /*isNamespaceImport*/ true); | ||
} | ||
|
||
const candidateModules = checker.getAmbientModules(); | ||
|
@@ -159,15 +173,15 @@ namespace ts.codefix { | |
if (localSymbol && localSymbol.name === name && checkSymbolHasMeaning(localSymbol, currentTokenMeaning)) { | ||
// check if this symbol is already used | ||
const symbolId = getUniqueSymbolId(localSymbol); | ||
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, /*isDefault*/ true)); | ||
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name, /*isDefault*/ true)); | ||
} | ||
} | ||
|
||
// check exports with the same name | ||
const exportSymbolWithIdenticalName = checker.tryGetMemberInModuleExports(name, moduleSymbol); | ||
if (exportSymbolWithIdenticalName && checkSymbolHasMeaning(exportSymbolWithIdenticalName, currentTokenMeaning)) { | ||
const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName); | ||
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol)); | ||
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name)); | ||
} | ||
} | ||
|
||
|
@@ -218,7 +232,7 @@ namespace ts.codefix { | |
return declarations ? some(symbol.declarations, decl => !!(getMeaningFromDeclaration(decl) & meaning)) : false; | ||
} | ||
|
||
function getCodeActionForImport(moduleSymbol: Symbol, isDefault?: boolean, isNamespaceImport?: boolean): ImportCodeAction[] { | ||
function getCodeActionForImport(moduleSymbol: Symbol, symbolName: string, isDefault?: boolean, isNamespaceImport?: boolean): ImportCodeAction[] { | ||
const existingDeclarations = getImportDeclarations(moduleSymbol); | ||
if (existingDeclarations.length > 0) { | ||
// With an existing import statement, there are more than one actions the user can do. | ||
|
@@ -375,10 +389,10 @@ namespace ts.codefix { | |
const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier || getModuleSpecifierForNewImport()); | ||
const changeTracker = createChangeTracker(); | ||
const importClause = isDefault | ||
? createImportClause(createIdentifier(name), /*namedBindings*/ undefined) | ||
? createImportClause(createIdentifier(symbolName), /*namedBindings*/ undefined) | ||
: isNamespaceImport | ||
? createImportClause(/*name*/ undefined, createNamespaceImport(createIdentifier(name))) | ||
: createImportClause(/*name*/ undefined, createNamedImports([createImportSpecifier(/*propertyName*/ undefined, createIdentifier(name))])); | ||
? createImportClause(/*name*/ undefined, createNamespaceImport(createIdentifier(symbolName))) | ||
: createImportClause(/*name*/ undefined, createNamedImports([createImportSpecifier(/*propertyName*/ undefined, createIdentifier(symbolName))])); | ||
const importDecl = createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, importClause, createLiteral(moduleSpecifierWithoutQuotes)); | ||
if (!lastImportDeclaration) { | ||
changeTracker.insertNodeAt(sourceFile, sourceFile.getStart(), importDecl, { suffix: `${context.newLineCharacter}${context.newLineCharacter}` }); | ||
|
@@ -392,7 +406,7 @@ namespace ts.codefix { | |
// are there are already a new line seperating code and import statements. | ||
return createCodeAction( | ||
Diagnostics.Import_0_from_1, | ||
[name, `"${moduleSpecifierWithoutQuotes}"`], | ||
[symbolName, `"${moduleSpecifierWithoutQuotes}"`], | ||
changeTracker.getChanges(), | ||
"NewImport", | ||
moduleSpecifierWithoutQuotes | ||
|
@@ -412,8 +426,9 @@ namespace ts.codefix { | |
removeFileExtension(getRelativePath(moduleFileName, sourceDirectory)); | ||
|
||
function tryGetModuleNameFromAmbientModule(): string { | ||
if (moduleSymbol.valueDeclaration.kind !== SyntaxKind.SourceFile) { | ||
return moduleSymbol.name; | ||
const decl = moduleSymbol.valueDeclaration; | ||
if (isModuleDeclaration(decl) && isStringLiteral(decl.name)) { | ||
return decl.name.text; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @jsx: react | ||
|
||
// @Filename: /node_modules/@types/react/index.d.ts | ||
////export = React; | ||
////export as namespace React; | ||
////declare namespace React { | ||
//// export class Component { render(): JSX.Element | null; } | ||
////} | ||
////declare global { | ||
//// namespace JSX { | ||
//// interface Element {} | ||
//// } | ||
////} | ||
|
||
// @Filename: /a.tsx | ||
////[|import { Component } from "react"; | ||
////export class MyMap extends Component { } | ||
////<MyMap/>;|] | ||
|
||
goTo.file("/a.tsx"); | ||
|
||
verify.importFixAtPosition([ | ||
`import { Component } from "react"; | ||
import * as React from "react"; | ||
export class MyMap extends Component { } | ||
<MyMap/>;`]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// @jsx: react | ||
|
||
// @Filename: /node_modules/@types/react/index.d.ts | ||
////export = React; | ||
////export as namespace React; | ||
////declare namespace React { | ||
//// export class Component { render(): JSX.Element | null; } | ||
////} | ||
////declare global { | ||
//// namespace JSX { | ||
//// interface Element {} | ||
//// } | ||
////} | ||
|
||
// @Filename: /a.tsx | ||
////[|import { Component } from "react"; | ||
////export class MyMap extends Component { } | ||
////<MyMap></MyMap>;|] | ||
|
||
goTo.file("/a.tsx"); | ||
|
||
verify.importFixAtPosition([ | ||
`import { Component } from "react"; | ||
import * as React from "react"; | ||
export class MyMap extends Component { } | ||
<MyMap></MyMap>;`]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
// https://github.com/Microsoft/TypeScript/issues/16065 | ||
|
||
// @jsx: react | ||
// @jsxFactory: factory | ||
|
||
// @Filename: /factory.ts | ||
////export function factory() { return {}; } | ||
////declare global { | ||
//// namespace JSX { | ||
//// interface Element {} | ||
//// } | ||
////} | ||
|
||
// @Filename: /a.tsx | ||
////[|<div/>|] | ||
|
||
goTo.file("/a.tsx"); | ||
verify.not | ||
verify.importFixAtPosition([]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a misleading name - why not
getSymbolFromJsxNamespace
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why not just return the JSX namespace itself and look up the original entity in the
exports
for that symbol?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method returns the JSX namespace symbol, such as
React
. It doesn't take it as a parameter. It's useful if the JSX namespace was missing and we can import it.The function
getJsxNamespace
just returns a string, so that isn't usable outside of the checker.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading through, I think I see why I was confused. JSX is usually a global, but in theory it could be locally imported. In that case, maybe
resolveJsxNamespaceAtLocation
is more accurate.