Skip to content

[Proposal] Error on ASI ambiguous cases #2130

Open

Description

@MaxGraey

Similar proposal in TypeScript closed and delegated to tslint:
microsoft/TypeScript#2575

 // Basically not possible in AS for now
function ambiguousReturn() {
    return
    {}
}
// could be:
//   return;
// or
//   return {};

More valid example:

let ambiguousAssignment = new Array<Array<i32>>()
let a = ambiguousAssignment
[1].sort()

// could be:
//   let a = ambiguousAssignment;
//   [1].sort()
// or
//   let a = ambiguousAssignment[1].sort();

I propose to identify such ambiguities and give a syntax error suggesting to insert semicolon ";" or remove the line break separating the keyword and "{" or "[" tokens.

Activity

  1. trusktr commented on Nov 17, 2021

    @trusktr
    Member

    Just to clarify,

        return
        {}

    is always equivalent to return; which is different than the other example. Anything after a return statement without a semi colon never interacts with the return statement. But in the example

    let a = ambiguousAssignment
    [1].sort()

    the item on the next line does interact with the previous line.

    In my mind, there's no ambiguity with the return statement.

    I believe we should not error on these, but we should match JS syntax. People should learn the syntax.

    A warning might be ok, as long as it allows compilation of the program (with valid syntax) to complete.

    I write all my code without semi colons.

  2. MaxGraey commented on Nov 17, 2021

    @MaxGraey
    MemberAuthor

    I believe that any ambiguity that might create undefined behaviour or very different behaviour should produce an error. JS design especially as far as errors is concerned should be avoided altogether. JS is designed to execute in as many cases as possible and only throw a syntax error in the most extreme cases.

  3. MaxGraey commented on Sep 1, 2022

    @MaxGraey
    MemberAuthor

    More example:

    // nested call (without diagnostics errors)
    function b(x: () => void): () => void { return x }
    
    let value = b
    (() => console.log('Hello'))()
  4. afederl commented on Oct 15, 2026

    @afederl

    Here is another example of unexpected behavior that may even be considered a bug:

            {
                let i: i32 = 3 
                let j: i32 = 45
                let k: i32 = 13
    
                i = j
                ++k
    
                console.log(`\n\n\nWITHOUT SEMICOLONS:  i = ${i}  j = ${j}  k = ${k}`)
            }
    
            {
                let i: i32 = 3;
                let j: i32 = 45;
                let k: i32 = 13;
    
                i = j;
                ++k;
    
                console.log(`\n\n\nWITH SEMICOLONS:     i = ${i}  j = ${j}  k = ${k}\n\n\n`);
            }

    Unlike JavaScript and TypeScript, AssemblyScript produces the following output:

    WITHOUT SEMICOLONS:  i = 45  j = 46  k = 13
    WITH SEMICOLONS:     i = 45  j = 45  k = 14

    In JavaScript and TypeScript, both versions produce the same output:

    WITHOUT SEMICOLONS:  i = 45  j = 45  k = 14
    WITH SEMICOLONS:     i = 45  j = 45  k = 14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions