assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

[Proposal] Error on ASI ambiguous cases

Open MaxGraey opened this issue 2 years ago • 3 comments

Similar proposal in TypeScript closed and delegated to tslint: https://github.com/microsoft/TypeScript/issues/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.

MaxGraey avatar Nov 09 '21 11:11 MaxGraey

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.

trusktr avatar Nov 17 '21 19:11 trusktr

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.

MaxGraey avatar Nov 17 '21 19:11 MaxGraey

More example:

// nested call (without diagnostics errors)
function b(x: () => void): () => void { return x }

let value = b
(() => console.log('Hello'))()

MaxGraey avatar Sep 01 '22 13:09 MaxGraey