Skip to content

Consider JSX namespace imports when moving statements between files #29451

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

Merged
merged 3 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ namespace ts.refactor {
const oldImportsNeededByNewFile = new SymbolSet();
const newFileImportsFromOldFile = new SymbolSet();

const containsJsx = find(toMove, statement => !!(statement.transformFlags & TransformFlags.ContainsJsx));
const jsxNamespaceSymbol = getJsxNamespaceSymbol(containsJsx);
if (jsxNamespaceSymbol) { // Might not exist (e.g. in non-compiling code)
oldImportsNeededByNewFile.add(jsxNamespaceSymbol);
}

for (const statement of toMove) {
forEachTopLevelDeclaration(statement, decl => {
movedSymbols.add(Debug.assertDefined(isExpressionStatement(decl) ? checker.getSymbolAtLocation(decl.expression.left) : decl.symbol));
Expand All @@ -485,13 +491,35 @@ namespace ts.refactor {
for (const statement of oldFile.statements) {
if (contains(toMove, statement)) continue;

// jsxNamespaceSymbol will only be set iff it is in oldImportsNeededByNewFile.
if (jsxNamespaceSymbol && !!(statement.transformFlags & TransformFlags.ContainsJsx)) {
unusedImportsFromOldFile.delete(jsxNamespaceSymbol);
}

forEachReference(statement, checker, symbol => {
if (movedSymbols.has(symbol)) oldFileImportsFromNewFile.add(symbol);
unusedImportsFromOldFile.delete(symbol);
});
}

return { movedSymbols, newFileImportsFromOldFile, oldFileImportsFromNewFile, oldImportsNeededByNewFile, unusedImportsFromOldFile };

function getJsxNamespaceSymbol(containsJsx: Node | undefined) {
if (containsJsx === undefined) {
return undefined;
}

const jsxNamespace = checker.getJsxNamespace(containsJsx);

// Strictly speaking, this could resolve to a symbol other than the JSX namespace.
// This will produce erroneous output (probably, an incorrectly copied import) but
// is expected to be very rare and easily reversible.
const jsxNamespaceSymbol = checker.resolveName(jsxNamespace, containsJsx, SymbolFlags.Namespace, /*excludeGlobals*/ true);

return !!jsxNamespaceSymbol && some(jsxNamespaceSymbol.declarations, isInImport)
? jsxNamespaceSymbol
: undefined;
}
}

// Below should all be utilities
Expand All @@ -512,7 +540,7 @@ namespace ts.refactor {
}
function isVariableDeclarationInImport(decl: VariableDeclaration) {
return isSourceFile(decl.parent.parent.parent) &&
decl.initializer && isRequireCall(decl.initializer, /*checkArgumentIsStringLiteralLike*/ true);
!!decl.initializer && isRequireCall(decl.initializer, /*checkArgumentIsStringLiteralLike*/ true);
}

function filterImport(i: SupportedImport, moduleSpecifier: StringLiteralLike, keep: (name: Identifier) => boolean): SupportedImportStatement | undefined {
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveJsxImport1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @jsx: preserve
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts

// @Filename: file.tsx
//// import React = require('react');
//// [|<div/>;|]
//// 1;

verify.moveToNewFile({
newFileContents: {
"/tests/cases/fourslash/file.tsx":
`1;`,
"/tests/cases/fourslash/newFile.tsx":
`import React = require('react');
<div />;
`,
}
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveJsxImport2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

// @jsx: preserve
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts

// @Filename: file.tsx
//// import React = require('react');
//// [|<div/>;|]
//// <div/>;

verify.moveToNewFile({
newFileContents: {
"/tests/cases/fourslash/file.tsx":
`import React = require('react');
<div/>;`,
"/tests/cases/fourslash/newFile.tsx":
`import React = require('react');
<div />;
`,
}
});
21 changes: 21 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveJsxImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// <reference path='fourslash.ts' />

// @jsx: preserve
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts

// @Filename: file.tsx
//// import React = require('react');
//// [|1;|]
//// <div/>;

verify.moveToNewFile({
newFileContents: {
"/tests/cases/fourslash/file.tsx":
`import React = require('react');
<div/>;`,
"/tests/cases/fourslash/newFile.tsx":
`1;
`,
}
});
29 changes: 29 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveJsxImport4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// <reference path='fourslash.ts' />

// @jsx: preserve
// @noLib: true
// @libFiles: react.d.ts,lib.d.ts,leftpad.d.ts

// @Filename: file.tsx
//// import React = require('leftpad');
//// [|function F() {
//// const React = import("react");
//// <div/>;
//// }|]
//// React;

verify.moveToNewFile({
newFileContents: {
"/tests/cases/fourslash/file.tsx":
`import React = require('leftpad');
React;`,
// NB: A perfect implementation would not copy over the import
"/tests/cases/fourslash/F.tsx":
`import React = require('leftpad');
function F() {
const React = import("react");
<div />;
}
`,
}
});