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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ func (c *Compiler) Compile(src, filename, pre, post string,
return nil, code, err
}
pgm, err := goja.CompileAST(ast, strict)
// Parsing only checks the syntax, not whether what the syntax expresses
// is actually supported (sometimes).
//
// For example, destructuring looks a lot like an object with shorthand
// properties, but this is only noticeable once the code is compiled, not
// while parsing. Even now code such as `let [x] = [2]` doesn't return an
// error on the parsing stage but instead in the compilation in base mode.
//
// So, because of this, if there is an error during compilation, it still might
// be worth it to transform the code and try again.
if err != nil {
if compatMode == lib.CompatibilityModeExtended {
code, _, err = c.Transform(src, filename)
if err != nil {
return nil, code, err
}
// the compatibility mode "decreases" here as we shouldn't transform twice
return c.Compile(code, filename, pre, post, strict, lib.CompatibilityModeBase)
}
return nil, code, err
}
return pgm, code, err
}

Expand Down
Loading