Skip to content

Commit

Permalink
Add directive property to follow ESTree
Browse files Browse the repository at this point in the history
ESTree added `directive` property to `ExpressionStatement` which is in directive prologue: estree/estree@b3de58c...1da8e60
  • Loading branch information
mysticatea authored and marijnh committed Oct 10, 2017
1 parent 89ca0d0 commit bf94140
Show file tree
Hide file tree
Showing 6 changed files with 1,389 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/expression.js
Expand Up @@ -734,6 +734,7 @@ pp.parseFunctionBody = function(node, isArrowFunction) {
this.checkParams(node, !oldStrict && !useStrict && !isArrowFunction && this.isSimpleParamList(node.params))
node.body = this.parseBlock(false)
node.expression = false
this.adaptDirectivePrologue(node.body.body)
this.labels = oldLabels
}
this.exitFunctionScope()
Expand Down
11 changes: 8 additions & 3 deletions src/loose/expression.js
Expand Up @@ -514,8 +514,8 @@ lp.parseMethod = function(isGenerator, isAsync) {
node.async = !!isAsync
this.inAsync = node.async
node.params = this.parseFunctionParams()
node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tt.braceL
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
node.body = this.parseBlock()
this.toks.adaptDirectivePrologue(node.body.body)
this.inAsync = oldInAsync
return this.finishNode(node, "FunctionExpression")
}
Expand All @@ -528,7 +528,12 @@ lp.parseArrowExpression = function(node, params, isAsync) {
this.inAsync = node.async
node.params = this.toAssignableList(params, true)
node.expression = this.tok.type !== tt.braceL
node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
if (node.expression) {
node.body = this.parseMaybeAssign()
} else {
node.body = this.parseBlock()
this.toks.adaptDirectivePrologue(node.body.body)
}
this.inAsync = oldInAsync
return this.finishNode(node, "ArrowFunctionExpression")
}
Expand Down
2 changes: 2 additions & 0 deletions src/loose/statement.js
Expand Up @@ -8,6 +8,7 @@ lp.parseTopLevel = function() {
let node = this.startNodeAt(this.options.locations ? [0, getLineInfo(this.input, 0)] : 0)
node.body = []
while (this.tok.type !== tt.eof) node.body.push(this.parseStatement())
this.toks.adaptDirectivePrologue(node.body)
this.last = this.tok
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
Expand Down Expand Up @@ -330,6 +331,7 @@ lp.parseFunction = function(node, isStatement, isAsync) {
this.inAsync = node.async
node.params = this.parseFunctionParams()
node.body = this.parseBlock()
this.toks.adaptDirectivePrologue(node.body.body)
this.inAsync = oldInAsync
return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
}
Expand Down
17 changes: 17 additions & 0 deletions src/statement.js
Expand Up @@ -21,6 +21,7 @@ pp.parseTopLevel = function(node) {
let stmt = this.parseStatement(true, true, exports)
node.body.push(stmt)
}
this.adaptDirectivePrologue(node.body)
this.next()
if (this.options.ecmaVersion >= 6) {
node.sourceType = this.options.sourceType
Expand Down Expand Up @@ -760,3 +761,19 @@ pp.parseImportSpecifiers = function() {
}
return nodes
}

// Set `ExpressionStatement#directive` property for directive prologues.
pp.adaptDirectivePrologue = function(statements) {
for (let i = 0; i < statements.length && this.isDirectiveCandidate(statements[i]); ++i) {
statements[i].directive = statements[i].expression.raw.slice(1, -1)
}
}
pp.isDirectiveCandidate = function(statement) {
return (
statement.type === "ExpressionStatement" &&
statement.expression.type === "Literal" &&
typeof statement.expression.value === "string" &&
// Reject parenthesized strings.
(this.input[statement.start] === "\"" || this.input[statement.start] === "'")
)
}
1 change: 1 addition & 0 deletions test/run.js
Expand Up @@ -9,6 +9,7 @@
require("./tests-asyncawait.js");
require("./tests-trailing-commas-in-func.js");
require("./tests-template-literal-revision.js");
require("./tests-directive.js");
acorn = require("../dist/acorn")
require("../dist/acorn_loose")
} else {
Expand Down

0 comments on commit bf94140

Please sign in to comment.