difftastic icon indicating copy to clipboard operation
difftastic copied to clipboard

Consider Javascript automatic semicolon insertion

Open ElianCordoba opened this issue 1 year ago • 3 comments

Javascript has a feature called automatic semicolon insertion (ASI in short) that, as the name implies, will insert semicolons, this powers the optionality of semicolon when coding.

This behaviour can produce a change in the behaviour of the code, for example

function test() {
	return 1
}

// => 1

// vs 

function test() {
	return
	1
}

// => undefined

This is because the second function gets a semicolon which makes the code look like this

function test() {
	return;
	1;
};

Difftastic currently outputs this as no changes, but this can be dangerous because the behaviour of the code changed.

ElianCordoba avatar Feb 03 '24 16:02 ElianCordoba

to add to this, needing to add semicolons, in general, slows down JS interpreters (the parser needs to find an error, backtrack and attempt to insert a semicolon to recover from the parse error, then it can continue)

i would recommend all semicolons show up in a diff, not just the ones that would change e.g. function results like the above

croconut avatar Feb 14 '24 18:02 croconut

Oof, I agree this is a bug. Thanks for the report.

It looks to me like a diffing bug, the parser seems to be doing the right thing.

Wilfred avatar Feb 19 '24 20:02 Wilfred

This is probably the same root issue as #587.

Wilfred avatar Feb 20 '24 08:02 Wilfred