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 — Compiler warnings / errors for common automatic semi-colon insertion (ASI) issues · Issue #2575 · microsoft/TypeScript

Compiler warnings / errors for common automatic semi-colon insertion (ASI) issues #2575

New issue
Copy link
New issue
Copy link
Closed
Closed
Compiler warnings / errors for common automatic semi-colon insertion (ASI) issues#2575
Copy link
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

Description

@jeffmay
Jeff May (jeffmay)
opened on Apr 1, 2015
Issue body actions

Personally, I like the look (and simplicity) of ECMA6 / TypeScript without semi-colons. The only problem is that there are some known danger-zones with automatic semi-colon insertion. Could we have compiler warnings to prevent these from slipping through the cracks?

Example:

function dangerous(): string {
    return
        "watch the semi-colons!"
}
dangerous()

Compiles to the following with no warnings or compiler errors (even though the function actually doesn't return a string):

function dangerous() {
    return;
    "watch the semi-colons!";
}
dangerous();

This specific case is kind of scary / annoying, but it might be worth looking into others.

Thoughts?

Reacted by Mark Penner

Activity

Next
  1. Mohamed Hegazy (mhegazy)
    added
    SuggestionAn idea for TypeScriptAn idea for TypeScript
    on Apr 1, 2015
  2. basarat commented on Apr 2, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Apr 2, 2015
    Contributor
    More actions

    Good suggestion 👍 Seems more like a tslint thing for me though.

    My 2c:
    I find not having semicolons throws off formatting and code analysis in a bunch of JS tools so I insert them whenever.

  3. jeffmay commented on Apr 2, 2015

    @jeffmay
    Jeff May (jeffmay)
    on Apr 2, 2015
    Author
    More actions

    Basarat Ali Syed (@basarat) do the code analysis tools apply to TypeScript or the resulting JavaScript? Because TypeScript will always insert the semi-colons on the resulting JavaScript, so it shouldn't affect any JavaScript tools.

  4. mhegazy commented on Apr 2, 2015

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Apr 2, 2015
    Contributor
    More actions

    Basarat Ali Syed (@basarat) was referring to tslin: https://github.com/palantir/tslint, and that is a typescript linter.

  5. antoinerousseau commented on Apr 13, 2015

    @antoinerousseau
    Antoine Rousseau (antoinerousseau)
    on Apr 13, 2015
    More actions

    +1

    another scary example:

    var foo = true
    // some comment
    ['a', 'b'].forEach((l: string) => {/**/})

    compiles into

    var foo = true['a', 'b'].forEach(function (l) {/**/})

    ...

    Reacted by Mark Penner and Andrew Jarvis
  6. jeffmay commented on Apr 14, 2015

    @jeffmay
    Jeff May (jeffmay)
    on Apr 14, 2015
    Author
    More actions

    Mohamed Hegazy (@mhegazy) I'm more interested in removing semi-colons from the language. A linter to require semi-colons would be going the other direction.

    Personally, I feel like the compiler should be able to detect when a missing semi-colon would fail to satisfy the type system (like in the examples given above) and give an appropriate warning or error without adding a separate build and lint step. Any time a statement without a semi-colon would fail to satisfy the expected type, it should be a compiler error and not a silent runtime failure.

  7. basarat commented on Apr 14, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Apr 14, 2015
    Contributor
    More actions

    I feel like the compiler should be able to detect when a missing semi-colon would fail to satisfy the type system

    Since undefined is compatible with anything an empty return being effectively return undefined does satisfy the type system requirement.

    But I do get what you are asking for. Just don't have a spec for it :)

  8. jeffmay commented on Apr 14, 2015

    @jeffmay
    Jeff May (jeffmay)
    on Apr 14, 2015
    Author
    More actions

    Also, Mohamed Hegazy (@mhegazy), I'm glad you mentioned the linter, I think it is important for the community to maintain a standard on style. I am just personally against semi-colons. They feel more and more clumsy these days. Especially when combined with the more functional style of fat-arrow functions, functional apis, chaining, etc.

  9. basarat commented on Apr 14, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Apr 14, 2015
    Contributor
    More actions

    . I am just personally against semi-colons

    Don't. Code analysis tools will have a hard time formatting. And as pointed out, ASI is not pretty. It is okay to be against semi-colons if all whitespace was always significant, but it isn't and JavaScript has weird edge cases as pointed out by Antoine Rousseau (@antoinerousseau)

  10. basarat commented on Apr 14, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Apr 14, 2015
    Contributor
    More actions

    You think it would add a semi-colon but it will not. And I can't know what you think. Perhaps you meant return undefined and didn't bother to write it?

  11. jeffmay commented on Apr 14, 2015

    @jeffmay
    Jeff May (jeffmay)
    on Apr 14, 2015
    Author
    More actions

    return undefined is such a strange thing to do. I would rather that we have a compiler error on return than allow it to default to return undefined. And if you want to return undefined, you should have to explicitly state return undefined or return;. As far as code-analysis, I understand how it would be an issue, but again, I don't see why the compiler couldn't just insert the semi-colons for you when it isn't dangerously ambiguous and otherwise raise an error. Maybe the tooling could call into this phase of the compiler before processing the TypeScript / JavaScript?

    I can't think of any case where an ambiguous statement due to an absent semi-colon is not just bad style that should either require an explicit semi-colon or be a compiler error. I can understand always adding it when interpreting the code line by line, because you don't know when to stop interpreting, but a compiler could reasonably bail out when a semi-colon cannot be safely added.

  12. basarat commented on Apr 14, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Apr 14, 2015
    Contributor
    More actions

    couldn't just insert the semi-colons for you when it isn't dangerously ambiguous

    Definitely not. You don't want to deviate from JS semantics. That would make porting JS code hard.

    otherwise raise an error

    This is what you have been requested and I support that ❤️

    Again, perhaps this is better suited by an additional option in tslint.

  13. DanielRosenwasser commented on Apr 14, 2015

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Apr 14, 2015
    Member
    More actions

    return undefined is such a strange thing to do.

    Yup. Some people don't want to write undefined the same way some people don't want to write semicolons.

  14. antoinerousseau commented on Apr 14, 2015

    @antoinerousseau
    Antoine Rousseau (antoinerousseau)
    on Apr 14, 2015
    More actions

    OK so the best would be to implement warnings on edge cases like these, we just have to choose between the TypeScript compiler and the linters like tslint

  15. DanielRosenwasser commented on Apr 14, 2015

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Apr 14, 2015
    Member
    More actions

    tslint is honestly probably the better place for this. I'm already inclined to say this is somewhat out of scope since our errors are effectively warnings, and the only checking we do at the whim of a flag is with noImplicitAny. I think it would be a awkward to create a flag tailored specifically to this.

  16. 2 remaining items

  17. antoinerousseau commented on Apr 14, 2015

    @antoinerousseau
    Antoine Rousseau (antoinerousseau)
    on Apr 14, 2015
    More actions

    They are optional in Javascript (see ASI), thus optional in TypeScript, since it is a superset of Javascript

  18. jeffmay commented on Apr 14, 2015

    @jeffmay
    Jeff May (jeffmay)
    on Apr 14, 2015
    Author
    More actions

    I understand the TypeScript (the language) is a superset of JavaScript and should remain as close to that as possible. However, TypeScript (the compiler) could be a little more helpful on catching bugs that would slip through the cracks. I don't think finding bugs is a matter of style. Although, I suppose I can just pay for WebStorm to solve the issue.

  19. mrkaspa commented on Aug 21, 2015

    @mrkaspa
    Michel Perez (mrkaspa)
    on Aug 21, 2015
    More actions

    +1 for removing semicolons

  20. andreas-bergstrom commented on Oct 10, 2015

    @andreas-bergstrom
    Andreas Bergström (andreas-bergstrom)
    on Oct 10, 2015
    More actions

    +1 for no semicolons.

  21. basarat commented on Oct 11, 2015

    @basarat
    Basarat Ali Syed (basarat)
    on Oct 11, 2015
    Contributor
    More actions

    This specific case is kind of scary / annoying

    There is already a tslint rule that errors in this case (early return), Add the following to tslint.json:

    "no-unreachable" : true

    🌹

  22. DanielRosenwasser commented on Oct 11, 2015

    @DanielRosenwasser
    Daniel Rosenwasser (DanielRosenwasser)
    on Oct 11, 2015
    Member
    More actions

    I think Vladimir Matveev (@vladima)'s CFA work covers this (i.e. reachability analysis). We should close it when appropriate.

  23. ytechie commented on Oct 28, 2015

    @ytechie
    Jason Young (ytechie)
    on Oct 28, 2015
    More actions

    Just wanted to throw in my 2 cents - this problem bit me in the ass today. Spent a decent amount of time figuring it out, and then found this issue.

  24. mhegazy commented on Dec 9, 2015

    @mhegazy
    Mohamed Hegazy (mhegazy)
    on Dec 9, 2015
    Contributor
    More actions

    the original issue for return statements will now be flagged as error with #4788. please reopen/refile if there are other issues that are not covered by the reachability checks reported by the compiler in TypeScript 1.8.

  25. Mohamed Hegazy (mhegazy)
    closed this as completedon Dec 9, 2015
  26. Mohamed Hegazy (mhegazy)
    added
    FixedA PR has been merged for this issueA PR has been merged for this issue
    on Dec 9, 2015
  27. mnpenner commented on Aug 31, 2017

    @mnpenner
    Mark Penner (mnpenner)
    on Aug 31, 2017
    More actions

    Mohamed Hegazy (@mhegazy) What about @antoinerousseau's scenario?

  28. cogitaria-admin commented on Jan 19, 2018

    @cogitaria-admin
    cogitaria-admin
    on Jan 19, 2018
    More actions

    I don't think "scary" or "dangerous" are good characterizations. In particular, the dangerous() function example will never produce the desired result. It would never pass an automatic or a manual test. For those who do not use unnecessary semicolons, the code jumps off of the page as an error. It's no more "dangerous" than for(...); { ... } really.

    Having said that, the dangerous() example will actually produce a typical lint warning: unreachable code.

  29. microsoft
    locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueA PR has been merged for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.