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

Run babel on compilation errors as well as on parsing ones #1861

Merged
merged 4 commits into from
Feb 18, 2021

Conversation

mstoykov
Copy link
Contributor

Unfortunately parsing checks syntax, not whether what the syntax did was
actually ... supported(sometimes).

As can be seen this fixes a lot of cases where this is a problem.

This is actually probably going to be an even bigger problem in the
future as goja gets more features in it which will let the parsing
to pass even though some feature actually isn't supported but the syntax
is now "valid".

Good example (which was how this was kind of found) is that destructuring
looks a lot like an object with shorthand properties, but this is only
noticable once the code is compiled, not while parsing. So in the future
when goja for example has support for all the rest of the syntax (like
let) it will be harder to catch.

Even now code such as let [x] = [2] which is desctructuring doesn't
return an error on the parsing stage but instead in the compilation.
This example might be a bug in goja, but I would argue we can have
others.

Unfortunately parsing checks syntax, not whether what the syntax did was
actually ... supported(sometimes).

As can be seen this fixes a lot of cases where this is a problem.

This is actually probably going to be an even bigger problem in the
future as goja gets more features in it which will let the parsing
to pass even though some feature actually isn't supported but the syntax
is now "valid".

Good example (which was how this was kind of found) is that destructuring
looks a lot like an object with shorthand properties, but this is only
noticable once the code is compiled, not while parsing. So in the future
when goja for example has support for all the rest of the syntax (like
`let`) it will be harder to catch.

Even now code such as `let [x] = [2]` which is desctructuring doesn't
return an error on the parsing stage but instead in the compilation.
This example might be a bug in goja, but I would argue we can have
others.
@mstoykov mstoykov added this to the v0.31.0 milestone Feb 17, 2021
@mstoykov mstoykov requested review from imiric and na-- February 17, 2021 10:10
@@ -3186,6 +3104,7 @@
"test/language/literals/regexp/S7.8.5_A1.4_T2.js-strict:true": "test/language/literals/regexp/S7.8.5_A1.4_T2.js: Test262Error: Code unit: d800 Expected SameValue(«\\\\\\ud800», «\\�») to be true at harness/sta.js:22:9(49)",
"test/language/literals/regexp/S7.8.5_A2.1_T2.js-strict:true": "test/language/literals/regexp/S7.8.5_A2.1_T2.js: Test262Error: Code unit: d800 Expected SameValue(«nnnn\\ud800», «nnnn�») to be true at harness/sta.js:22:9(49)",
"test/language/literals/regexp/S7.8.5_A2.4_T2.js-strict:true": "test/language/literals/regexp/S7.8.5_A2.4_T2.js: Test262Error: Code unit: d800 Expected SameValue(«a\\\\\\ud800», «a\\�») to be true at harness/sta.js:22:9(49)",
"test/language/literals/regexp/early-err-flags-unicode-escape.js-strict:true": "test/language/literals/regexp/early-err-flags-unicode-escape.js: error is not an object (Test262: This statement should not be evaluated.)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is babel/babel#6691 and practically ... not a problem for us.

@@ -4724,16 +4643,15 @@
"test/language/statements/for-in/head-const-bound-names-dup.js-strict:true": "test/language/statements/for-in/head-const-bound-names-dup.js: unexpected error type (TypeError), expected (SyntaxError)",
"test/language/statements/for-in/head-const-bound-names-fordecl-tdz.js-strict:true": "test/language/statements/for-in/head-const-bound-names-fordecl-tdz.js: Test262Error: Expected a ReferenceError to be thrown but no exception was thrown at all at harness/sta.js:22:9(49)",
"test/language/statements/for-in/head-const-bound-names-in-stmt.js-strict:true": "test/language/statements/for-in/head-const-bound-names-in-stmt.js: error is not an object (Test262: This statement should not be evaluated.)",
"test/language/statements/for-in/head-let-bound-names-dup.js-strict:true": "test/language/statements/for-in/head-let-bound-names-dup.js: unexpected error type (TypeError), expected (SyntaxError)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually now returns the "correcter":

ERRO[0000] TypeError: file:///home/mstoykov/work/k6_copy/js/tc39/d.js: Duplicate declaration "x"
> 1 | for (let [x, x] in {}) {}
    |              ^
  2 |  at <eval>:2:28542(114)

instead of

SyntaxError: Unexpected strict mode reserved word at file:///home/mstoykov/work/k6_copy/js/tc39/d.js:1:6

The error comes from babel transformation so 🤷

@@ -4907,9 +4808,9 @@
"test/language/statements/for-of/head-const-bound-names-in-stmt.js-strict:true": "test/language/statements/for-of/head-const-bound-names-in-stmt.js: error is not an object (Test262: This statement should not be evaluated.)",
"test/language/statements/for-of/head-decl-no-expr.js-strict:true": "test/language/statements/for-of/head-decl-no-expr.js: error is not an object (Test262: This statement should not be evaluated.)",
"test/language/statements/for-of/head-expr-no-expr.js-strict:true": "test/language/statements/for-of/head-expr-no-expr.js: error is not an object (Test262: This statement should not be evaluated.)",
"test/language/statements/for-of/head-let-bound-names-dup.js-strict:true": "test/language/statements/for-of/head-let-bound-names-dup.js: unexpected error type (TypeError), expected (SyntaxError)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

na--
na-- previously approved these changes Feb 18, 2021
Copy link
Member

@na-- na-- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you don't refactor and remove the duplication, 👍 LGTM as it is, since it's clear what is happening. Though maybe add a comment with the explanation why we do it from the PR description?

js/compiler/compiler.go Outdated Show resolved Hide resolved
@codecov-io
Copy link

codecov-io commented Feb 18, 2021

Codecov Report

Merging #1861 (29ddfd4) into master (6bebdd6) will decrease coverage by 0.05%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1861      +/-   ##
==========================================
- Coverage   71.58%   71.52%   -0.06%     
==========================================
  Files         180      179       -1     
  Lines       13955    13961       +6     
==========================================
- Hits         9990     9986       -4     
- Misses       3329     3336       +7     
- Partials      636      639       +3     
Flag Coverage Δ
ubuntu 71.52% <0.00%> (-0.01%) ⬇️
windows ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
js/compiler/compiler.go 67.24% <0.00%> (-9.23%) ⬇️
js/common/initenv.go 86.66% <0.00%> (-13.34%) ⬇️
js/initcontext.go 90.10% <0.00%> (-2.20%) ⬇️
core/engine.go 91.04% <0.00%> (-2.00%) ⬇️
cmd/ui_windows.go
stats/cloud/collector.go 82.33% <0.00%> (+0.56%) ⬆️
lib/executor/vu_handle.go 95.23% <0.00%> (+1.90%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6bebdd6...722e200. Read the comment docs.

imiric
imiric previously approved these changes Feb 18, 2021
Copy link
Contributor

@imiric imiric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if we could get rid of this "try Babel if parsing/compiling failed" approach, but glad to see it got rid of so many errors. Could we avoid the duplication though?

@mstoykov
Copy link
Contributor Author

I'd prefer if we could get rid of this "try Babel if parsing/compiling failed" approach,

That is dependant on goja getting support for more or less everything babel currently provides us ... which will take quite some time, especially given that it is mostly 1 person working on it in their spare time. My contributions are unlikely to make a dent in that unless we prioritize it and that is actually unlikely as well as there is not so much benefit to that as for example working on things that babel doesn't provide us with (generators, Promise, async/await being the interesting ones).

Could we avoid the duplication though?

Looking at the code ... it's unlikely any refactoring will make it clearer and this code is rarely touched ... so hopefully whoever do touch it will notice the same block a comment apart :)

@mstoykov mstoykov merged commit 66667ab into master Feb 18, 2021
@mstoykov mstoykov deleted the runBabelOnAllErrors branch February 18, 2021 09:26
na-- pushed a commit that referenced this pull request Mar 1, 2021
Unfortunately parsing checks syntax, not whether what the syntax expressed
is actually ... supported(sometimes).

As can be seen, this fixes a lot of cases where this is a problem.

This is actually probably going to be an even bigger problem in the
future as goja gets more features in it which will let the parsing
to pass even though some feature actually isn't supported but the syntax
is now "valid".

A good example (which was how this was kind of found) is that destructuring
looks a lot like an object with shorthand properties, but this is only
noticeable once the code is compiled, not while parsing. So in the future
when goja for example has support for all the rest of the syntax (like
`let`) it will be harder to catch.

Even now code such as `let [x] = [2]` which is destructuring doesn't
return an error on the parsing stage but instead in the compilation.
This example might be a bug in goja, but I would argue we can have
others.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants