Skip to content

Commit

Permalink
Skip esvalid errors about unsupported node types
Browse files Browse the repository at this point in the history
Esvalid https://github.com/estools/esvalid hasn't been updated for a
while, so it doesn't recognise newer JS standards' node types as valid
at all.

Rather than skip error checking altogether, I decided to just create a
list of all the node types esvalid supports, and ignore errors relating
to types other than those.  This way, we retain sanity checks for stuff
like variable names containing property access

    { type: "Identifier", name: "a.b" }

and other such insanity, which escodegen doesn't throw errors for for
performance reasons, but which are very likely to be user error.

Ideally in the long term we should patch esvalid to check new node types
for sanity also.
  • Loading branch information
anko committed Jan 16, 2020
1 parent bc6792a commit a792d03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/translate.ls
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ compile = require \./compile

{ errors } = require \esvalid

errors-about-nodes-esvalid-understands = do ->
# Ignore errors about nodes that esvalid can't currently handle.
(ast) ->
# This list is gathered from
# https://github.com/estools/esvalid/blob/2693f0906a3336de05d3325d10f8aa8297211bdb/index.js
# At time of writing, esvalid version is 1.1.0
esvalid-supported-node-types = <[
ArrayExpression AssignmentExpression BinaryExpression BlockStatement
BreakStatement CallExpression CatchClause ConditionalExpression
ContinueStatement DebuggerStatement DoWhileStatement EmptyStatement
ExpressionStatement ForInStatement ForStatement FunctionDeclaration
FunctionExpression Identifier IfStatement LabeledStatement Literal
LogicalExpression MemberExpression NewExpression ObjectExpression Program
ReturnStatement SequenceExpression SwitchCase SwitchStatement ThisExpression
ThrowStatement TryStatement UnaryExpression UpdateExpression
VariableDeclaration VariableDeclarator WhileStatement WithStatement
]>
err = errors ast
.filter (.node.type in esvalid-supported-node-types)

module.exports = (root-env, ast, options={}) ->

transform-macros = (options.transform-macros || []) .map (func) ->
Expand All @@ -30,7 +50,7 @@ module.exports = (root-env, ast, options={}) ->
|> (.filter (isnt null)) # because macro definitions emit null
|> (.map statementify)

err = errors program-ast
err = errors-about-nodes-esvalid-understands program-ast
if err.length
first-error = err.0
throw first-error
Expand Down
15 changes: 15 additions & 0 deletions test.ls
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,21 @@ test "macro can compile and return parameter as estree" ->
'''
..`@equals` "3;\n'hi';\nc();\n({ a: b });"

test "macro can handle new-ish JS features like ClassDeclaration" ->
esl '''
(macro test
(lambda ()
(return (object
"type" "ClassDeclaration"
"id" (object "type" "Identifier" "name" "A")
"superClass" null
"body" (object
"type" "ClassBody"
"body" (array))))))
(test)
'''
..`@equals` "class A {\n}"

test "multiple invocations of the compiler are separate" ->
esl "(macro what (lambda () (return 'hi)))"
esl "(what)"
Expand Down

0 comments on commit a792d03

Please sign in to comment.