X Tutup
The Wayback Machine - https://web.archive.org/web/20210124104946/https://github.com/sql-js/sql.js/pull/383
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

eslint /test/ directory #383

Closed
wants to merge 2 commits into from
Closed

eslint /test/ directory #383

wants to merge 2 commits into from

Conversation

@kaizhu256
Copy link
Member

@kaizhu256 kaizhu256 commented Apr 5, 2020

this pull-request currently has 4 commits:

commit 1

- first-pass to eslint directory /test/ (exclude /test/test_worker.js)
- enforce 80-column max-width
- use 4-space indent
- replace fat-arrow (=>) with function
- replace let/const with var
- replace == with === and != with !==
- replace x++ with x += 1
- eslint rule changes:
```diff
+    "global-require": "off", // test-code has embedded/non-top-level require(...)
+    "func-names": "off", // needed when replacing fat-arrows with anonymous-functions
+    "import/no-dynamic-require": "off", // test-code uses dynamic-require
+    "max-len": ["error", { code: 80 }], // 80-column
+    "no-console": ["error", { allow: ["error", "log"] }], // console.error
-    strict: ["error", "function"],
+    strict: ["error", "safe"], // top-level use-strict in commonjs-module
```

commit 2

- document reason for each eslint-rule

commit 3

- eslint /test/test_worker.js

commit 4

- tighten eslint-rule "vars-on-top": "on"
- to prevent test-writers from haphazardly leaving var-statements all over test-code

not sure if others feel commit 4 is acceptably motivated or not. but am open to revert/change it (or any of the other commits).

Copy link
Member

@lovasoa lovasoa left a comment

I would like to avoid unnecessary changes in api.js :

  • keep the var in front of variable names
  • I think we can keep "func-names", just give names to callbacks in tests
.eslintrc.js Outdated Show resolved Hide resolved
@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Apr 5, 2020

keep the var in front of variable names

do you mean revert commit 4 (hoist all vars to top of function-scope)? i can do that. just making sure that is the meaning.

@lovasoa
Copy link
Member

@lovasoa lovasoa commented Apr 5, 2020

do you mean revert commit 4 (hoist all vars to top of function-scope)? i can do that. just making sure that is the meaning.

yes

@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Apr 5, 2020

added 2 more follow-on commits:

commit 6

- revert previous commit and relax eslint-rule "vars-on-top": "on" to "vars-on-top": "off"

commit 7

- remove from /test/ invalid-code containing free-variable assert

i still want to keep relaxed the eslint-rule:

        // reason - test-cases use nameless, anonymous-functions for callbacks
        "func-names": "off",

reason is if you look at /test/test_worker.js, it has 14 anonymous-function callbacks, and is PITA to maintain unique func-name for each of them :`(

test/test_transactions.js Outdated Show resolved Hide resolved
@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Apr 12, 2020

pinging about status of this pull-request? have more pull-requests in pipeline, but would like a style-guide inplace first if possible.

src/api.js Outdated Show resolved Hide resolved
test/test_blob.js Outdated Show resolved Hide resolved
test/test_database.js Outdated Show resolved Hide resolved
@lovasoa
Copy link
Member

@lovasoa lovasoa commented Apr 13, 2020

There is a lot of what looks like unchecked auto-formatting. Can you proofread the whole code you generated, and re-request a review once it's clean ?

@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Apr 14, 2020

K, I'll try to get around to it this weekend.

@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Apr 14, 2020

want to re-introduce default airbnb eslint-rule "vars-on-top": "on" to enforce program-correctness.

according to official es5 language-spec @ https://www.ecma-international.org/ecma-262/5.1/#sec-12.1:

A variable statement declares variables that are created as defined in 10.5. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

so this code in src/api.js:

function extract_blob(ptr) {
    ...
    for (var j = 0; j < size; j += 1) {
        blob_arg[j] = HEAP8[blob_ptr + j];
    }
    return blob_arg;
}

is internally transformed by spec-compliant engines to this:

 function extract_blob(ptr) {
+    var j = undefined; // Variables are initialised to undefined when created as defined in 10.5
     ...
-    for (var j = 0; j < size; j += 1) {
+    for (j = 0; j < size; j += 1) { // Initialiser is assigned when executed
         blob_arg[j] = HEAP8[blob_ptr + j];
     }
     return blob_arg;
 }

the eslint-rule "vars-on-top": "on" forces users to explicitly/correctly do what js-engines already do internally.

it also makes proof-reading /test/ directory easier, since i (and future test-writers) no longer have to keep track of where the first variable assignment took place (when there are multiple assignments).

@lovasoa
Copy link
Member

@lovasoa lovasoa commented Apr 14, 2020

I know how variable declarations in javascript work, thank you 😃
This can be discussed, but I find the code with variable declarations on top to be much more difficult to read.

@kaizhu256 kaizhu256 force-pushed the kaizhu256:eslint.test branch from f8c6351 to 213ba20 Jun 18, 2020
@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Jun 18, 2020

finished proofreading/linting the 2 most problematic test-files:

  • /test/test_functions.js
  • /test/test_workers.js

with proposed eslint-flag changes:

+        // reason - test-cases use nameless, anonymous-functions for callbacks
+        "func-names": "off",
+        // reason - test-cases use lazy/conditional require
+        "global-require": "off",
+        // reason - test-cases use console.log and console.error
+        "no-console": ["error", { allow: ["error", "log"] }],
-        "vars-on-top": "off"

lets just focus this pull-request on above 2 test-files.

once merged, a separate pull-request to lint remaining test files should be trivial.

@kaizhu256
Copy link
Member Author

@kaizhu256 kaizhu256 commented Oct 2, 2020

closing this due to inactivity, but may re-open in future.

@kaizhu256 kaizhu256 closed this Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
X Tutup