Skip to content
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

fix inconsistencies in import UMD code fixes adapting to module format #19572

Merged

Conversation

aluanhaddad
Copy link
Contributor

As described in #17860, the import code fix for UMD modules has been updated such that it

  • uses import m from "specifier"; import under --allowSyntheticDefaultImports
  • uses import m = require("specifier"); syntax where supported
  • falls back to import * as m from "specifier"; syntax where necessary
  • displays a diagnostic message matching the import that results from applying it

Fixes #17860

I'm not sure how to go about writing unit tests for this.

const { module, allowSyntheticDefaultImports } = context.compilerOptions;

// Prefer to import as a synthetic `default` if available.
if (allowSyntheticDefaultImports || module === ModuleKind.System && allowSyntheticDefaultImports !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a function to src\core.ts to getAllowSyntheticDefaultImports(compilerOptions) that has this logic, and share it with the checker as well.

@@ -644,6 +665,19 @@ namespace ts.codefix {
Debug.fail("Either the symbol or the JSX namespace should be a UMD global if we got here");
}

const { module, allowSyntheticDefaultImports } = context.compilerOptions;
Copy link
Contributor

Choose a reason for hiding this comment

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

consider moving compilerOptions to top destructuring declaration.

}

// When a synthetic `default` is unavailable, use `import..require` if the module kind supports it.
if (module === ModuleKind.AMD || module === ModuleKind.CommonJS || module === ModuleKind.UMD) {
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure i agree we need this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The argument here is that if require is available, it is a reliable away to obtain module.exports even when it is callable. I am happy to remove this as requested but believe it is valuable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is actually what's causing the build failures because of expectations in the four slash tests.

Would you like me to remove the require changes? I'd be happy to open a separate issue for them.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2017

For tests, take a look at tests/cases/fourslash/importNameCodeFixNewImportFileDetachedComments.ts

@aluanhaddad aluanhaddad force-pushed the module-format-aware-import-fixes branch from 266ea5f to af6b43a Compare November 1, 2017 16:52
 - use default import under --allowSyntheticDefaultImports
 - import..require support
 - make make quick fix info match resulting import
 - make diagnostics
 - extract test for synethetic default imports into getAllowSyntheticDefaultImports in core.ts
 - use getAllowSyntheticDefaultImports in checker.ts and importFixes.ts
 - move compilerOptions to top level destructuring
@aluanhaddad aluanhaddad force-pushed the module-format-aware-import-fixes branch from 06cd3c3 to 1be8b21 Compare November 4, 2017 20:30
const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);

// Prefer to import as a synthetic `default` if available.
if (allowSyntheticDefaultImports) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@weswigham this is another place for your new change in #19675 to be plugged in. we would like to always show the default import in this case and never fallback to import .. = require

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Roger. Thank you!

@mhegazy
Copy link
Contributor

mhegazy commented Nov 6, 2017

@aluanhaddad can you please address the test failures.

@aluanhaddad
Copy link
Contributor Author

I updated it to always uses the synthetic defaults for UMD modules unless --getAllowSyntheticDefaultImports(compilerOptions) returns false in which case it suggests a namespace import. This also resolves the test failures.

@mhegazy mhegazy merged commit 70cabdd into microsoft:master Nov 7, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

thanks @aluanhaddad!

Want to redo the import .. =require support for AMD/CommonJS/UMD? I think we should do that as well.

@aluanhaddad
Copy link
Contributor Author

@mhegazy I will create a PR for that shortly.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

thanks!

@aluanhaddad aluanhaddad deleted the module-format-aware-import-fixes branch November 7, 2017 08:33
@@ -237,6 +237,8 @@ namespace ts.codefix {
return parent as ImportDeclaration;
case SyntaxKind.ExternalModuleReference:
return (parent as ExternalModuleReference).parent;
case SyntaxKind.ImportEqualsDeclaration:
Copy link

Choose a reason for hiding this comment

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

This will never be the parent of a LiteralExpression. Removing in #19667.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quick Fix info inconsistent when offering to import a UMD declaration.
2 participants