TypeScript/tests/cases/fourslash/importNameCodeFix_require_importVsRequire_moduleTarget.ts
Andrew Branch f9945f5acf
Full support for CommonJS auto-imports in JS (#37027)
* Support add new requires

* Always use destructuring for requiring default exports

* Add more tests

* Update existing fourslash tests

* Use `getExportsAndPropertiesOfModule`

* Add UMD test

* Apply suggestions from code review

Fix typos

Co-Authored-By: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
2020-02-28 14:34:20 -08:00

41 lines
886 B
TypeScript

// If the module target is es2015+ and the file has no existing CommonJS
// indicators, use `import` declarations.
// @allowJs: true
// @checkJs: true
// @module: es2015
// @Filename: a.js
////export const x = 0;
// @Filename: index.js
////x
goTo.file("index.js");
verify.codeFix({
index: 0,
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
description: `Import 'x' from module "./a"`,
applyChanges: false,
newFileContent:
`import { x } from "./a";
x`
});
// If the module target is es2015+ but the file already uses `require`
// (and not `import`), use `require`.
goTo.position(0);
edit.insertLine("const fs = require('fs');\n");
verify.codeFix({
index: 0,
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
description: `Import 'x' from module "./a"`,
applyChanges: false,
newFileContent:
`const fs = require('fs');
const { x } = require('./a');
x`
});