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
Disallow statements starting with continuation characters #1402
Comments
This is a fascinating suggestion. The first step is to look into ecosystem impact of the change by temporarily adding the rule to @christianbundy Want to give that a shot? |
Yeah! It looks like the
I ran the test suite on the rule as-is, which only throws errors when it sees a leading semicolon that does not circumvent an unexpected multi-line statement, which it sees as an extra semicolon, but I'm sure there will be more problems found once I fix the rule in ESLint. I'll report back when I have some more info. 👍 |
I made a small change to ESLint to get the behavior we're discussing. To be explicit about the new behavior: // ✔️ fine because there's no semi or unexpected multi-line statement
[1, 2, 3].forEach((x) => console.log(x))
// ❌ error because of the unexpected multi-line statement
foo()
[1, 2, 3].forEach((x) => console.log(x))
// ❌ error because of the semicolon
foo()
;[1, 2, 3].forEach((x) => console.log(x))
// ✔️ fine because there's no semi or unexpected multi-line
foo()
var nums = [1, 2, 3]
nums.forEach((x) => console.log(x)) I re-ran the tests and it looks like only four modules needed fixes, all were super simple:
Note: this transfers all responsibility for avoiding unexpected multi-line expressions to I've included the solution diffs below. I've made the smallest change, using `--fix` when possible, to illustrate how this rule change will interact with these projects.simple-getdiff --git a/index.js b/index.js
index ab88116..92aad0c 100644
--- a/index.js
+++ b/index.js
@@ -91,7 +91,7 @@ simpleGet.concat = (opts, cb) => {
})
}
-;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
+['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
simpleGet[method] = (opts, cb) => {
if (typeof opts === 'string') opts = { url: opts }
return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb) binary-splitdiff --git a/test.js b/test.js
index f122e08..b7230fd 100644
--- a/test.js
+++ b/test.js
@@ -37,9 +37,11 @@ test('custom matcher', function (t) {
t.equals(items.length, 5)
t.equals(items.join(' '), 'hello yes this is dog')
t.end()
- });
+ })
+
+ var strings = ['hello yes ', 'this', ' is d', 'og']
- ['hello yes ', 'this', ' is d', 'og'].map(function (chunk) {
+ strings.map(function (chunk) {
return bufferFrom ? Buffer.from(chunk) : new Buffer(chunk) // eslint-disable-line
}).forEach(function (chunk) {
splitStream.write(chunk) reusifydiff --git a/benchmarks/createNoCodeFunction.js b/benchmarks/createNoCodeFunction.js
index ce1aac7..2d19fff 100644
--- a/benchmarks/createNoCodeFunction.js
+++ b/benchmarks/createNoCodeFunction.js
@@ -11,13 +11,13 @@ function createNoCodeFunction () {
/* eslint no-constant-condition: "off" */
var num = 100
- ;(function () {
+ {
if (null) {
// do nothing
} else {
fib(num)
}
- })()
+ }
}
for (var i = 0; i < max; i++) { bitmididiff --git a/src/views/adsense.mjs b/src/views/adsense.mjs
index ada6982..d652f32 100644
--- a/src/views/adsense.mjs
+++ b/src/views/adsense.mjs
@@ -6,7 +6,7 @@ import { isBrowser, isProd, tokens } from '../config'
export default class Adsense extends Component {
componentDidMount () {
if (!isBrowser || !isProd) return
- ;(window.adsbygoogle = window.adsbygoogle || []).push({})
+ (window.adsbygoogle = window.adsbygoogle || []).push({})
}
shouldComponentUpdate () { |
Thank you for this suggestion and work, @christianbundy. Personally, I'd like to see more examples before I can feel about this. |
Update: I wrote a new rule that resolves this by throwing errors on any statement that starts with a continuation character. Documentation is available here, but in summary this prevents statements from starting with Code that passes this rule should be able to be safely used, copied and pasted, and even stripped of semicolons without changing the semantics. Here's the new pass/fail info for the code above: // ❌ error because of the [
[1, 2, 3].forEach((x) => console.log(x))
// ❌ error because of the [
foo()
[1, 2, 3].forEach((x) => console.log(x))
// ❌ error because of the [
foo()
;[1, 2, 3].forEach((x) => console.log(x))
// ✔️ fine because no statements start with a continuation character
foo()
var nums = [1, 2, 3]
nums.forEach((x) => console.log(x)) This was what I was originally looking for when I opened the issue, I just didn't realize that it'd require writing a new rule. 😅 It passes on 177 / 188 (95%) modules in the test suite, and I'm a bit surprised that it only fails on 9 modules with 13 errors total. I'd love feedback either here or in my ESLint PR, I'm super open to questions and suggestions. |
I have felt into this and I'm okay with it. What would make it clear that this is desirable is how much more concise the semicolons section in the rules docs would be, I imagine. If you're willing to make a PR for that change, I feel that it would make it clear that this is desirable. |
What version of this package are you using? 14.1.0
What problem do you want to solve? I'd like to continue the semicolon purge by disallowing semicolons as a workaround for lines that start with
[
,(
,`
,+
,*
,/
,-
,,
, or.
. In other words:What do you think is the correct solution to this problem??
Before:
After:
Are you willing to submit a pull request to implement this change? Absolutely.
Pairs well with #633.
The text was updated successfully, but these errors were encountered: