Skip to content

add support for Extension Preference in import - #25073

Closed
Wenlu Wang (Kingwl) wants to merge 2 commits into
microsoft:masterfrom
Kingwl:jsExtensionPreference
Closed

Wenlu Wang (Kingwl) wants to merge 2 commits into
microsoft:masterfrom
Kingwl:jsExtensionPreference

Conversation

@Kingwl

Copy link
Copy Markdown
Contributor

Fixes #24779

Comment thread src/compiler/moduleSpecifiers.ts Outdated
namespace ts.moduleSpecifiers {
export interface ModuleSpecifierPreferences {
importModuleSpecifierPreference?: "relative" | "non-relative";
includingJsExtensionOnAutoImports?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it is not always .js, it can also be .jsx. so the name of the flag should not include Js. also this also applies to all LS operations and not only auto-imports for completions. i would just call it includeExtensionInImports

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in src/compiler/moduleSpecifiers L400 removeExtensionAndIndexPostFix
i saw it only handle the .js Extension, should i make a change for that to support jsx?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is also a minor problem in importNameCodeFix_jsExtension test case:
why could it import a file with .js extension even if that was a ts file,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That was supported intentionally in #8895.

Comment thread src/compiler/moduleSpecifiers.ts Outdated
function usesJsExtensionOnImports({ imports }: SourceFile): boolean {
return firstDefined(imports, ({ text }) => pathIsRelative(text) ? fileExtensionIs(text, Extension.Js) : undefined) || false;
function usesJsExtensionOnImports({ imports }: SourceFile, preferences: ModuleSpecifierPreferences): boolean {
return firstDefined(imports, ({ text }) => pathIsRelative(text) ? fileExtensionIs(text, Extension.Js) : undefined) || !!preferences.includingJsExtensionOnAutoImports;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we need to handle .jsx here as well.

also please add a test to it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think an explicit (non-undefined) preference should always take priority over looking through the source file for an existing import.

@mhegazy
Mohamed Hegazy (mhegazy) requested a review from a user June 19, 2018 17:16
@mhegazy

Copy link
Copy Markdown
Contributor

Andy (Andrewkraft) (@Andy-MS) can you please review

@Kingwl
Wenlu Wang (Kingwl) force-pushed the jsExtensionPreference branch 2 times, most recently from 04f3e1a to 4d57419 Compare June 22, 2018 17:08
@Kingwl

Wenlu Wang (Kingwl) commented Jun 22, 2018 •

Copy link
Copy Markdown
Contributor Author

i have try track the issue to understand that why the js or jsx extension been append after filename
IMO, that output the file extension for other packager or runtime,
it need an artificial extension(Preference) rather than the extension that itself has,
i have a try that take extension name by itself file name and i drop that
currently, i take a Preference that allow user set the extension (js or jsx)

Mohamed Hegazy (@mhegazy) Andy (Andrewkraft) (@Andy-MS) could you give some suggestion please😢

@Kingwl Wenlu Wang (Kingwl) changed the title add support for jsExtension Preference on auto import add support for Extension Preference in import Jun 22, 2018
Comment thread src/compiler/moduleSpecifiers.ts Outdated
includeExtensionInImports?: Extension.Js | Extension.Jsx;
}

// Note: fromSourceFile is just for usesJsExtensionOnImports

@ghost Deleted user (ghost) Jun 22, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

From #25074 (comment) -- maybe we shouldn't take fromSourceFile as input and just use includeExtensionInImports for that.

Comment thread src/compiler/moduleSpecifiers.ts Outdated
namespace ts.moduleSpecifiers {
export interface ModuleSpecifierPreferences {
importModuleSpecifierPreference?: "relative" | "non-relative";
includeExtensionInImports?: Extension.Js | Extension.Jsx;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems wrong for this to be configurable as always .js or .jsx. I think the right policy is: if the file you're importing from is a .ts or .js file you should import with .js (if the setting is on), if the file is .tsx or .jsx you should import with .jsx (if the setting is on). Then all the user has to set is: yes add extensions, or no don't add extensions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actually, .tsx files are typically compiled to .js, right? So it would still make sense to use a .js import always? Mohamed Hegazy (@mhegazy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, .tsx files are typically compiled to .js, right?

depends on the setting.. --JSX preserve will result in .jsx, --JSX react/--JSX react-native will result in .js.

@mhegazy

Copy link
Copy Markdown
Contributor

Andy (Andrewkraft) (@Andy-MS) can you take another look.

Comment thread src/compiler/moduleSpecifiers.ts Outdated
return addJsExtension
? noExtension + ".js"
const actualExtension = tryGetActualExtension(fileName, compilerOptions);
return (preferences.includeExtensionInImports && actualExtension)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If includeExtensionInImports is undefined (so not explicitly set), we should still be doing what the usesJsExtensionOnImports function did - context based method of determining which way we should go based on what's already there.

@Kingwl
Wenlu Wang (Kingwl) force-pushed the jsExtensionPreference branch 2 times, most recently from b449ff0 to 29d7c73 Compare July 6, 2018 09:35
@shrinktofit

Copy link
Copy Markdown

How to config the tsconfig?

@Kingwl

Copy link
Copy Markdown
Contributor Author

Andy (Andrewkraft) (@Andy-MS) should i close this?

@Kingwl

Copy link
Copy Markdown
Contributor Author

Ryan Cavanaugh (@RyanCavanaugh) is the milestone means i need to update the branch?🤦🏻‍♂️

@Kingwl

Copy link
Copy Markdown
Contributor Author

update soon🚲

@Kingwl

Copy link
Copy Markdown
Contributor Author

Seems already implements

@Kingwl
Wenlu Wang (Kingwl) deleted the jsExtensionPreference branch June 18, 2019 06:11
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
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.

7 participants