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

make the quote symbol configurable for import codefix #13270

Closed
zpdDG4gta8XKpMCd opened this issue Jan 3, 2017 · 63 comments · Fixed by #17750
Closed

make the quote symbol configurable for import codefix #13270

zpdDG4gta8XKpMCd opened this issue Jan 3, 2017 · 63 comments · Fixed by #17750
Labels
Bug A bug in TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. Domain: Refactorings e.g. extract to constant or function, rename symbol VS Code Tracked There is a VS Code equivalent to this issue

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Jan 3, 2017

currently the import codefix uses double quotes for the module path string literal which makes our linter unhappy, i wish the choice of a quote was configurable

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. Domain: Refactorings e.g. extract to constant or function, rename symbol labels Jan 3, 2017
@aluanhaddad
Copy link
Contributor

Yes please, this is very frustrating.

@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Jan 6, 2017

the temporary fix is rather trivial if you go ./node_modules/typescript/lib/*.js and look for "\"" around codefixes namespace

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 6, 2017

It'd be great if this was just inferred in cases where the option isn't explicitly listed.

@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Jan 6, 2017

yeah, just look around and do it as in 90% cases (you can collect the statistics which quotes are used around the code, can't you?)

@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Jan 6, 2017

another thing to consider is that since codefixes emit typescript (not javascript, i beg you), the emitter should comply to some standards

namely the quote mark should be configured at the top level of the codefix emitter configuration, so that any codefix out there uses it for emitting properly formatted string literals with the right quote symbol

@aluanhaddad
Copy link
Contributor

namely the quote mark should be configured at the top level of the codefix emitter configuration, so that any codefix out there uses it for emitting properly formatted string literals with the right quote symbol

Great point. This is the way that Roslyn C# code fixes are starting to work, they emit code based on style preferences which are now more configurable and enforceable as well.

@ghost
Copy link

ghost commented Mar 7, 2017

A related thing to consider are quick fixes of missing interface implementations. The array type declarations should also be configurable.

For example SomeType[] versus Array<SomeType>.

@vytautas-pranskunas-
Copy link

do you have any idea when it is going to be fixed?

@mjbvz
Copy link
Contributor

mjbvz commented Apr 3, 2017

@mhegazy Marking as high-property for VSCode since we've seen a fair number of reports of this. We could add a workaround on our side to replace the quotes but other consumers of TypeScript would also benefit from this. Can we please target 2.3 with a fix?

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed High Priority Suggestion An idea for TypeScript labels Apr 3, 2017
@RyanCavanaugh
Copy link
Member

Where should the configuration for this be stored?

I like the idea of inferring from the current file, though. It seems fine to make the user fix the first one themselves then the remainder are "correct"

@pgrm
Copy link

pgrm commented Apr 3, 2017

Inferring from the current file would be great. If there is nothing to infer from yet, it would be awesome if the tool could try to find a configuration for the tslint quotemark rule. Currently in VS Code I use auto import code fix, followed by the tslint code fix to change from double to single quote.

@vivainio
Copy link

vivainio commented Apr 5, 2017

Making it configurable is overkill. Just find the first quote character used the file and use that.

@logic01
Copy link

logic01 commented Apr 5, 2017

I agree with pgrm. The tslint.json quotemark rule is the perfect place to infer from.

@lukeautry
Copy link

lukeautry commented Apr 6, 2017

Reading from the file (and optionally in cases where there are no quotes, reading from tslint.json) would work for me. Is there any precedent for TypeScript reading directly from tslint configuration though?

@mickdekkers
Copy link

@vivainio prettier has a --single-quote option.

@aluanhaddad
Copy link
Contributor

@vivainio

Can't believe this bikeshedding is still going on - is it because the inference logic had a bug?

This was never a bug. This is a feature request. Also, it works correctly in the nightly builds. It's been fixed for a while.

@mickdekkers @vivainio

Here's some evidence: Prettier is using double quotes. With everybody gradually adopting prettier, we'll be back to double quote world soon.

Why would I want another formatter operating on my *.tsx? files? I already have TypeScript and TSLint. Maybe if you are using a linter that does not format it would make sense. Everyone should use a linter.

@Maximaximum
Copy link

Today I've found out that the bug came back.

While using VS Code, when I use Typescript v2.6.1 (the VSCode's version), the codefix uses single quotes, which is what I need.

However, when I switch to my project's version (Typescript v2.4.2), the codefix always uses double quotes.

Is there a way to force VSCode to use single quotes while runing Typescript v2.4.2? Do we need to apply a bugfix to Typescript v2.4.2 and release a new, patched version 2.4.3?

Note: I can't upgrade to use Typescript v2.6.x in my project right now.

@mjbvz
Copy link
Contributor

mjbvz commented Dec 1, 2017

@Maximaximum This is only fixed in TS 2.5+. There is no way to fix this with older TS versions

@Maximaximum
Copy link

@mjbvz Ok, thanks for letting me know

@smasala
Copy link

smasala commented Jan 10, 2018

I'm using 2.6.2 and I'm getting double quotes :s

@PlugaruT
Copy link

yep, double quotes are used in 2.6.2

@smasala
Copy link

smasala commented Jan 12, 2018

@PlugaruT Thanks. So if I've understood correctly it was double, then at some point single and now double again but still not configurable?

@PlugaruT
Copy link

I don't really know, I just started to write typescript so for me are just double quotes.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 13, 2018

The original behavior was always double quotes. That was modified in #17750 to infer from the imports in the current file the quote style; this means that if this is the first import in the file there is no place to infer the style from and couple quotes will be used.
Issue #20619 tracks making it based on a configuration in your settings file.

@filipef101
Copy link

filipef101 commented Feb 9, 2018

How do I disable semicolons in auto imports?

@mhegazy
Copy link
Contributor

mhegazy commented Feb 9, 2018

tracked by #19882

@filipef101
Copy link

@mhegazy Hey thanks, I did that, but still the same, do I need to specify to use ts importer somehow? I still get a ; with auto imports

@mhegazy
Copy link
Contributor

mhegazy commented Feb 9, 2018

the suggestion of having semicolons being configurable is tracked by #19882. I did not say it was implemented.

@filipef101
Copy link

@mhegazy Oh, still, any idea? xd
And do you know if the line numbers can be disable or at least made shorter? they occupy a lot of space :(

@aluanhaddad
Copy link
Contributor

@filipef101 line numbers are a general text editor feature. How are they related to TypeScript?

@miqmago
Copy link

miqmago commented Feb 17, 2018

Comented in microsoft/vscode#23559 (comment):

As @adyavanapalli very well mentioned, you can edit the typescript.json file to get around this issue on /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript/snippets/ for MacOS and C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\typescript\snippets\ for Windows.

Thx buddy.

Resuming:

  • OSX: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript/snippets/typescript.json
  • OSX: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript-basics/snippets/typescript.json
  • Windows: C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\typescript\snippets\typescrypt.json
  • Ubuntu: /usr/share/code/resources/app/extensions/typescript-basics/snippets/typescript.json

Look for import snippet, replace double quotes by single ones:

	"Import external module.": {
		"prefix": "import statement",
		"body": [
			"import { $0 } from '${1:module}';"
		],
		"description": "Import external module."
	},

@daniborgs
Copy link

On VS Code 1.21.1 you can correct this editing /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/typescript-basics/snippets/typescript.json

@cdoremus
Copy link

cdoremus commented Mar 27, 2018

On Ubuntu Linux (VS Code 1.21.1), you need to edit /usr/share/code/resources/app/extensions/typescript-basics/snippets/typescript.json.

In the 'Import external module' section of that file make the body array property to be the value "import { $0 } from '${1:module}';" The section will then look like this:

	"Import external module.": {
		"prefix": "import statement",
		"body": [
			"import { $0 } from '${1:module}';"
		],
		"description": "Import external module."
	},

@ReallyNotARussianSpy
Copy link

Since VSCode 1.24 (June 2018) there is an option for configuring this.

https://code.visualstudio.com/updates/v1_24#_preferences-for-auto-imports-and-generated-code

@imcotton
Copy link
Contributor

imcotton commented Jun 7, 2018

Since VSCode 1.24 (June 2018) there is an option for configuring this.

But the import statement completion still gives you double quote 😞

@Local9
Copy link

Local9 commented Jun 21, 2018

@imcotton only works with TypeScript 2.9 and higher from what I can find

@ReallyNotARussianSpy
Copy link

@MurhafSousli https://code.visualstudio.com/updates/v1_24#_preferences-for-auto-imports-and-generated-code

If you are still getting the double quote then your preferences.quoteStyle are probably either set to "double" or "auto". If it is set to "auto" (the default) then it infers the import style from existing imports. If there are no existing imports, then I believe it defaults to double quote. You can change preferences.quoteStyle (see above link) to "single" if you always want single quotes.

@Local9
Copy link

Local9 commented Jul 24, 2018

Recent update, the auto generated import is working as expected with single quotes. As for auto generated snipets these are still double quoted.

@corysimmons
Copy link

VSCode settings.json

{
  "typescript.preferences.quoteStyle": "single"
}

No semis if you're cool:

{
  "typescript.preferences.quoteStyle": "single",
  "typescript.format.semicolons": "remove"
}

Example:

import Hello from 'components/Hello'

export default () => (
  <>
    <Hello />
    <h1>app</h1>
  </>
)

This issue can be locked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. Domain: Refactorings e.g. extract to constant or function, rename symbol VS Code Tracked There is a VS Code equivalent to this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.