Repository navigation
autoimport should care about semicolons #19882
Description
Activity
- addedVS Code TrackedThere is a VS Code equivalent to this issueThere is a VS Code equivalent to this issueand removed
on Nov 9, 2017 - addedSuggestionAn idea for TypeScriptAn idea for TypeScriptAwaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this feature
on Nov 9, 2017 We could add a new set of CodeFixSettings that include semicolon and quotes as well as any other things that would be needed by code fixes..
alternatively, we can teach the formatter about semi colons.
Reacted by Dmitri, Tim C, Daniel M., Enteleform and SominemoI like ts importer. You can configure semicon, space by youself.
Reacted by Ivan Wang and wildeyesyep, i think that updating the formatter can be the best option too.
Reacted by Tim Cjust to add my 2 cents, a few lint rules can probably fixed at the same time. Here are the rules that are breaking for me when I auto fix an import:
Reacted by Kacper, Kyle VanDeRiet, Julien Roubieu, Chen Asraf, Dan Ryan and Sominemo43 remaining items
- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Jun 17, 2019 I noticed that there was an algorithm for detecting semi colons, but in this fix (that has been closed), does it only apply this to import checks? the reason I ask is I have seen it very common in certain codebases (especially React) not to include semi colons for import/export, but to do so for all other code. If all that was considered was the entire file having at least 3/4 semi colons on each statement, it would not detect this sort of scenario properly or intelligently.
rob2d no, the fix considers any statements that normally have semicolons but don’t require them.
If all that was considered was the entire file having at least 3/4 semi colons on each statement
That said, it starts looking from the top of the file and quits once it sees a handful of examples to work with, so if you had a bunch of semicolon-less imports at the top of your file, it should infer that you don’t want semicolons. So in that case, auto-imports will do what you want, and other code insertions won’t.
I have seen it very common in certain codebases (especially React) not to include semi colons for import/export, but to do so for all other code
Before joining TypeScript, I worked heavily in React for several years, and I’ve never heard of such a thing. Can you send some examples?
Reacted by Brad Zacher and rob2dAndrew Branch (@andrewbranch) thanks for explanation on first part 🙃
I'm not doubting that you have experience working in React, but some of the most popular OSS libraries for their domain follow this convention so that is a little bit weird... Perhaps you had seen it and not noticed? I've been using React since v0.3.x and remember literally > half of the libraries that I was importing at one point used this convention, and many very popular and upcoming repos still follow it to some extent... though I can recognize it's gaining popularity to use semis since VSCode is now becoming a very good ES7 IDE -- thanks partly to TS.There is also an option for this in ESLint and Prettier -- ironically what causes many to be give up is strong adoption of VSCode which thanks to TS has defaults which are not quite as accommodating for the entire community (though I do have to say I am very impressed with it's rapid improvement/progress).Just to emphasize I'm not simply stubborn or something: the main reason I do not use semis on imports personally is because it is a bit noisy to look at in the top of my files -- have a bit of OCD about clutter with imports and can justify mentally separating it as the only statically linked part of an ES7 app.
Obviously though, in codebases I am touching that do not share this view, "when in Rome" and all. Either way I don't knock using semis in your imports since everyone is different but just explaining that it's a normal thing for some devs and not exactly violating the spec.
For a list of some of the soms popular libraries in off of the top of my head:Redux:
https://github.com/reduxjs/reduxReact Redux:
https://github.com/reduxjs/react-redux/tree/master/testCSSinJS:
https://github.com/cssinjs/jssReact Router (not currently but for most of its life):
https://github.com/ReactTraining/react-router/blob/v3/modules/IndexRedirect.jsEdit: crossing out terrible misinformation.
rob2d Unless I’m really missing something, every example you just sent doesn’t use semicolons at all.
In my memory, eslint-config-standard, which prefers no semicolons, was really popular with many React folks in the early days, and more recently eslint-config-react-app I think has become a de-facto choice.
To be clear, I have absolutely zero judgement on how people format their code—I want you to put semicolons wherever they make you happy, no more and no less—it’s just that I feel like we’re unlikely to add additional heuristics to TypeScript to support preferences that are atypical and/or more work to test for. That’s why I asked for examples—not because I don’t believe you or think that’s a weird preference to have, but rather because I want to try to form an idea of how common this is by looking at the size of projects that adopt such a convention.
Reacted by Anton Bessonov and Ryan Cavanaughwell, I put some links there? Seems you did not click them if you are making this statement. I mean obviously in terms of that, check the React code and not the webpack code or course.Edit: no, you definitely clicked.
Reacted by Anton BessonovI went into each repo you linked and found a random JS file, so possibly we’re looking at different files. Can you send a specific file where you see this convention? Here’s what I looked at:
- https://github.com/reduxjs/redux/blob/master/src/createStore.js
- https://github.com/reduxjs/react-redux/blob/master/test/components/Provider.spec.js
- https://github.com/cssinjs/jss/blob/master/packages/jss-plugin-compose/src/index.js
- https://github.com/ReactTraining/react-router/blob/v3/modules/IndexRedirect.js
Seems you did not click them if you are making this statement.
I clicked on the links and they are not using semicolons at all
Edit: upon further reflection, I hadn't noticed that they were either all or nothing, and I am an idiot 🙃 my whole life has just changed a bit thanks to this thread.
TLDR: you all have a very good point. Sorry about that.
Reacted by Andres KalleAndrew Branch (@andrewbranch) - Thanks for working on the issue, I'm testing latest version to see if this issue was solved in
PrinterOptionswhich has a property exactly for this which was working in the past so we developers can have control . Will update with results here. Left some questions / opinions #31801 (comment) thanksIt might make more sense to have this discussion on #31801
RyanCavanaugh commented
on Aug 6, 2019 MemberMore actionsSebastián Gurin (@cancerberoSgx) replying to your pre-edit content, our belief was that implementing this as a heuristic would actually be the best behavior, since the tool would just "do the right thing" with no necessary configuration from the user. It seems it'd be better if TS simply inserted semicolons in files that seemed to use semicolons so that users didn't have to go find the configuration option.
Reacted by Jakob Norlin and Yuya TanakaReacted by EnteleformRyan Cavanaugh (@RyanCavanaugh) I give my feedback #31801 so there's less noise, thanks - ("do the right thing" sounds strange particularly if in a platform extendible by third parties with their own opinions- I think "the right thing" is to consistently support any code style that the JavaScript language specification allows any user to write their code with (since there's no standard for that and I don't think airbnb style guide or any other is even a defacto standard on this regard.Currently semicolons behavior and settings API is inconsistent with the rest of code format related semantics)
Reacted by Dmitri, Enteleform, Eric Haynes and Pedr Browneany news?

From Francesco Soncina (@phra) on November 9, 2017 20:11
i usually write my ts/js code without semicolons and there is now way to configure vscode to avoid adding semicolons to auto imports. it would be nice an automatic parsing of the tslint config or at least a configurable option in the settings.
Steps to Reproduce:
Copied from original issue: microsoft/vscode#37991