Skip to content

Add code fix to convert 'require' in a '.ts' file to an 'import' #23711

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
3 commits merged into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only add suggestion for modules
  • Loading branch information
Andy Hanson committed Apr 30, 2018
commit b1a728fdacacf7089e8995f6a5caddac27ef5e62
2 changes: 1 addition & 1 deletion src/services/suggestionDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ts {
}
check(sourceFile);

if (!isJsFile) {
if (!isJsFile && sourceFile.externalModuleIndicator) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i take back my original comment. we should just do this all the time. with this check enabled a simple scenario like #23780 would not trigger the suggestion..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about flip-flopping on this issue.

for (const statement of sourceFile.statements) {
if (isVariableStatement(statement) &&
statement.declarationList.flags & NodeFlags.Const &&
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/fourslash/codeFixRequireInTs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////export {};
////const a = [|require("a")|];
////a;

verify.getSuggestionDiagnostics([{
message: "'require' call may be converted to an import.",
Expand All @@ -11,5 +12,8 @@ verify.getSuggestionDiagnostics([{
verify.codeFix({
description: "Convert 'require' to 'import'",
newFileContent:
`import a = require("a");`,
// TODO: GH#23781
`export {};
import a = require("a");
a;`,
});
6 changes: 4 additions & 2 deletions tests/cases/fourslash/codeFixRequireInTs_all.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////export {};
////const a = [|require("a")|];
////const b = [|require("b")|];

verify.codeFixAll({
fixId: "requireInTs",
fixAllDescription: "Convert all 'require' to 'import'",
newFileContent:
`import a = require("a");
// TODO: GH#23781
`export {};
import a = require("a");
import b = require("b");`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

// @allowSyntheticDefaultImports: true

// @Filename: /a.ts
////export {};
////const a = [|require("a")|];
////a;

verify.codeFix({
description: "Convert 'require' to 'import'",
newFileContent: `import a from "a";`,
newFileContent:
// TODO: GH#23781
`export {};
import a from "a";
a;`,
});
5 changes: 5 additions & 0 deletions tests/cases/fourslash/codeFixRequireInTs_onlyIfModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path='fourslash.ts' />

////const a = [|require("a")|];

verify.getSuggestionDiagnostics([]);