Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deno(fmt): adds optional semicolon thinking it's not optional #24285

Closed
BlackAsLight opened this issue Jun 20, 2024 · 9 comments
Closed

deno(fmt): adds optional semicolon thinking it's not optional #24285

BlackAsLight opened this issue Jun 20, 2024 · 9 comments
Labels
bug Something isn't working correctly deno fmt Related to the "deno fmt" subcommand or dprint

Comments

@BlackAsLight
Copy link

Version: Deno 1.44.4

When the semicolon option is set to false for deno fmt, Deno adds the mandatory semicolons at the start of a line instead of at the end of the previous line.

It for some reason falsely thinks this line ++x requires a semicolon at the start ;++x, but this is not the case as with or without the semicolon, the interpretation of the code does not change, and I can't seem to think of one instance where something like this would be valid code.

x
++

Reproduction

Before

let x = 0
++x

After

let x = 0
;++x
@lucacasonato
Copy link
Member

@dsherret probably there is a rule you have that if a line starts with +, you insert a semicolon. However that is not necessary for ++ and --, because they are prefixed with a no-newline-terminator-here.

@lucacasonato lucacasonato added bug Something isn't working correctly deno fmt Related to the "deno fmt" subcommand or dprint labels Jun 20, 2024
@yazan-abdalrahman
Copy link
Contributor

@lucacasonato @BlackAsLight @dsherret
can provide a proper example when we add a semicolon as a prefix with + or -.

I think we should remove + - from the is_prefix_semi_colon_insertion_char rule ?
or just prevent ++ -- case

@BlackAsLight
Copy link
Author

I think we should remove + - from the is_prefix_semi_colon_insertion_char rule ? or just prevent ++ -- case

While a semicolon before a ++x won't change the way it's interpreted, a semicolon before +x can change the way it's interpreted, as prefixing + and - to a variable is a way to implicitly convert the value to a number.

With the below code, a semicolon prefixing the last line will change the way it's interpreted from the value of y being '55' or '555'.

let x = '5'
let y = x + x
+x

On the other hand though, a line that started with ;+x would be a pointless expression so one could argue that if a line starts with + then it is meant to be part of the previous line and no semicolon should be added.

@yazan-abdalrahman
Copy link
Contributor

yazan-abdalrahman commented Jul 10, 2024

I think we should remove + - from the is_prefix_semi_colon_insertion_char rule ? or just prevent ++ -- case

While a semicolon before a ++x won't change the way it's interpreted, a semicolon before +x can change the way it's interpreted, as prefixing + and - to a variable is a way to implicitly convert the value to a number.

With the below code, a semicolon prefixing the last line will change the way it's interpreted from the value of y being '55' or '555'.

let x = '5'
let y = x + x
+x

On the other hand though, a line that started with ;+x would be a pointless expression so one could argue that if a line starts with + then it is meant to be part of the previous line and no semicolon should be added.

Yes, I understand and I did that. I've introduced a case for '+x' and '-x' that should add ; to be ';+x' ';-x', and I've prevented add ; in the ++ -- scenario.

check this PR it has test explaining all cases
dprint/dprint-plugin-typescript#648

`

== shouldn't insert semi-colons at the start of lines beginning with a bracket ==
let x = 0
++x

[expect]
let x = 0
++x

== shouldn't insert semi-colons at the start of lines beginning with a bracket ==
let x = 0
--x

[expect]
let x = 0
--x

== should insert semi-colons at the start of lines beginning with a bracket ==
+5

[expect]
;+5

== should insert semi-colons at the start of lines beginning with a bracket ==
-5

[expect]
;-5
`

@hourianto
Copy link

hourianto commented Jul 17, 2024

Hello, not sure if this is the same issue or not, but I also tried using no semicolons with fmt and Deno inserts a semicolon in a very unexpected place.

Minimal repro code:

let resp = [1]
let status = null

if (Array.isArray(resp)) {
  [status] = resp
}

After running deno fmt:

let resp = [1]
let status = null

if (Array.isArray(resp)) {
  ;[status] = resp
}

A semicolon was added before [status]

@hourianto
Copy link

@dsherret thanks for the info, I'm quite new with TS so it just looked very out of place :)

@yazan-abdalrahman
Copy link
Contributor

yazan-abdalrahman commented Jul 17, 2024

@dsherret , @BlackAsLight

Hello, is my solution acceptable? Please take a look.

@dsherret
Copy link
Member

dsherret commented Aug 1, 2024

Fixed in dprint/dprint-plugin-typescript#648 via dprint-plugin-typescript upgrade in #24819

@dsherret dsherret closed this as completed Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly deno fmt Related to the "deno fmt" subcommand or dprint
Projects
None yet
Development

No branches or pull requests

5 participants