GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — importModuleSpecifierEnding changes .ts string completions to .js by jessetrinity · Pull Request #44602 · microsoft/TypeScript

Conversation

@jessetrinity

@jessetrinity Jesse Trinity (jessetrinity) commented Jun 15, 2021

Copy link
Copy Markdown
Contributor

Fixes #44374

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jessetrinity
Jesse Trinity (jessetrinity) requested a review from Andrew Branch (andrewbranch) June 15, 2021 19:17
@typescript-bot TypeScript Bot (typescript-bot) assigned Jesse Trinity (jessetrinity) Jun 15, 2021
@typescript-bot TypeScript Bot (typescript-bot) added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jun 15, 2021
@jessetrinity Jesse Trinity (jessetrinity) changed the title Do not autocomplete import paths with .ts extension importModuleSpecifierEnding changes .ts string completions to .js Jun 15, 2021
andrewbranch
Andrew Branch (andrewbranch) requested changes Jun 15, 2021
View reviewed changes
Comment thread src/services/stringCompletions.ts Outdated
foundFileName = removeFileExtension(getBaseFileName(filePath));
foundFiles.set(foundFileName, tryGetExtensionFromPath(filePath));
}
else if (includeExtensionsOption === IncludeExtensionsOption.ModuleSpecifierCompletion && fileExtensionIs(filePath, Extension.Ts)) {

@andrewbranch Andrew Branch (andrewbranch) Jun 15, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

What we really need, and I’m not sure whether we have (but we might), is a function that maps input file extensions to emitted file extensions. So we would have

  • .ts → .js
  • .tsx → ? (I don’t know if this is always .js or if we might emit .jsx with --jsx=preserve?)
  • .mts → .mjs (someday soon)
  • .d.ts → undefined because it doesn’t get emitted
  • .json → .json (only if --resolveJsonModule?)
  • .js → .js

If we don’t have that function, we need to (a) figure out what the right thing for TSX is and implement it now, and (b) remember to update it for .mts and .cts if/when we add those.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jessetrinity Jesse Trinity (jessetrinity) Jun 15, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Emitter.ts has getOutputExtension which does approximately that, but it works on a sourceFile. Since we're just working working with the paths here we probably just want a new similar function that handle the cases we need.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 16, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

👍 in absence of something better, just leave comments on each function (getOutputExtension and the new function) saying to keep them in sync with each other.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 17, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Found it:

TypeScript/src/compiler/moduleSpecifiers.ts

Line 696 in fad9122

function getJSExtensionForFile(fileName: string, options: CompilerOptions): Extension {

I would maybe just export that function so it doesn’t immediately cause merge conflicts with #44501.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 17, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actually, you might need to refactor it to return undefined instead of throwing on unsupported extensions, and move the debug assertions to its caller, or another wrapped version of it. But otherwise I think this function is exactly the thing we were looking for.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/services/stringCompletions.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch Andrew Branch (andrewbranch) mentioned this pull request Jun 21, 2021
Fix Issue #39155 (importModuleSpecifierEnding does not apply for module specifier completions) #42467
Merged
Jesse Trinity (jessetrinity) and others added 3 commits June 21, 2021 10:26
@jessetrinity
don't add .ts extensions to imports
bc80712
Jesse Trinity @andrewbranch @jessetrinity
Update src/services/stringCompletions.ts
76509c3
Co-authored-by: Andrew Branch <[email protected]>
@jessetrinity
add other supported extension types
25555c3
@jessetrinity
Jesse Trinity (jessetrinity) force-pushed the fix44374 branch from 8bc4f1f to 25555c3 Compare June 21, 2021 19:56
@jessetrinity
add final newlines
ab950c0
andrewbranch
Andrew Branch (andrewbranch) reviewed Jun 21, 2021
View reviewed changes
Comment thread src/compiler/moduleSpecifiers.ts Outdated
}

function getJSExtensionForFile(fileName: string, options: CompilerOptions): Extension {
function getJSExtensionForFileIfApplicable(fileName: string, options: CompilerOptions): Extension {

@andrewbranch Andrew Branch (andrewbranch) Jun 21, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

To me this one sounds like the one that never throws. I would suggest naming the undefined-returning one tryGetJSExtensionForFile and the throwing one getJSExtensionForFile.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
andrewbranch
Andrew Branch (andrewbranch) requested changes Jun 21, 2021
View reviewed changes
Comment thread src/compiler/moduleSpecifiers.ts Outdated
}

export function getJSExtensionForFile(fileName: string, options: CompilerOptions): Extension | undefined {
const ext = extensionFromPath(fileName);

@andrewbranch Andrew Branch (andrewbranch) Jun 21, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I thought it was sketchy that this function didn’t have undefined in its return type, so I looked and found that it too has an undefined-returning counterpart, and this one throws! This may need to be swapped for tryGetExtensionFromPath for good measure. This also makes me realize we should have a test that actually returns completions for a non-source-file file extension, like index.css or something.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jessetrinity Jesse Trinity (jessetrinity) Jun 21, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I don't think we even read other extension types from disk, but I think you mean something like

//@filename: index.css
body {}

//@filename: index.html
<!DOCTYPE html>

//@filename: foo.ts
import "./

And expect nothing at the uncompleted string?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 21, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hm, I looked at stringCompletions and saw tryReadDirectory, so I assumed we were offering everything we could find, with extension substitutions as appropriate. Not sure what’s going on then 🤔

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 21, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You could certainly be seeing an artifact of the fourslash client 😬. Have you checked the behavior against a Real Editor?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jessetrinity Jesse Trinity (jessetrinity) Jun 21, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I was pretty sure that tryReadDirectory only returned files that matched its extensions argument, if provided. I might be misreading though.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jessetrinity Jesse Trinity (jessetrinity) Jun 21, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

either way, updated to not use the throwing variant :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@andrewbranch Andrew Branch (andrewbranch) Jun 21, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, I missed that parameter 🤦‍♂️

It might actually be nice to allow all extensions here in the future, since we can no longer seriously claim to believe we’re in a world where people only import things we know how to analyze. This doesn’t even account for declare module "*.css" pattern ambient module declarations. But that’s for another day. Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jessetrinity Jesse Trinity (jessetrinity) Jun 21, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Oh geez TIL. Is that a wildcard module declaration?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Jesse Trinity (jessetrinity) added 2 commits June 21, 2021 13:51
@jessetrinity
adress PR comment
5adaabc
@jessetrinity
add unsupported extension test
15a4735
andrewbranch
Andrew Branch (andrewbranch) approved these changes Jun 21, 2021
View reviewed changes
@jessetrinity
Jesse Trinity (jessetrinity) merged commit 753feb8 into microsoft:main Jun 21, 2021
@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.

Reviewers

@andrewbranch Andrew Branch (andrewbranch) andrewbranch approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@jessetrinity Jesse Trinity (jessetrinity)

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

importModuleSpecifierEnding = js doesn't apply for autocomplete within import paths

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@jessetrinity @andrewbranch @typescript-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.