TypeScript/tests/cases/fourslash/importNameCodeFix_require_importVsRequire_importWins.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

52 lines
1.3 KiB
TypeScript

/// <reference path="fourslash.ts" />
// @allowJs: true
// @checkJs: true
// @Filename: blah.js
////export default class Blah {}
////export const Named1 = 0;
////export const Named2 = 1;
// @Filename: addToExisting.js
////const { Named2 } = require('./blah')
////import { Named1 } from './blah'
////
////new Blah
// @Filename: newImport.js
////import fs from 'fs';
////const path = require('path');
////
////new Blah
// If an "add to existing" fix could be applied both to an `import`
// and to a `require` declaration, prefer the `import`.
goTo.file("addToExisting.js");
verify.codeFix({
index: 0,
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
description: `Add default import 'Blah' to existing import declaration from "./blah"`,
newFileContent:
`const { Named2 } = require('./blah')
import Blah, { Named1 } from './blah'
new Blah`
});
// If a file contains `import` and `require` declarations but none
// can be used for an "add to existing" fix, prefer `import` for the
// new declaration.
goTo.file("newImport.js");
verify.codeFix({
index: 0,
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
description: `Import default 'Blah' from module "./blah"`,
newFileContent:
`import fs from 'fs';
import Blah from './blah';
const path = require('path');
new Blah`
});