Skip to content

Commit

Permalink
Parameterize dummy value and export isDummy
Browse files Browse the repository at this point in the history
  • Loading branch information
tuesmiddt authored Feb 24, 2020
1 parent fa3ad8c commit 12ae8fe
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions acorn-loose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ take the **`LooseParser`** class exported by the module, and call its
static `extend` method with one or more plugins to get a customized
parser class. The class has a static `parse` method that acts like the
top-level `parse` method.

**isDummy**`(node)` takes a `Node` and returns `true` if it is a dummy node
inserted by the parser. The function performs a simple equality check on the
node's name.
1 change: 1 addition & 0 deletions acorn-loose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import "./statement"
import "./expression"

export {LooseParser} from "./state"
export {isDummy} from "./parseutil"

defaultOptions.tabSize = 4

Expand Down
4 changes: 3 additions & 1 deletion acorn-loose/src/parseutil.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export function isDummy(node) { return node.name === "✖" }
export const dummyValue = "✖"

export function isDummy(node) { return node.name === dummyValue }
5 changes: 3 additions & 2 deletions acorn-loose/src/state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Parser, SourceLocation, tokTypes as tt, Node, lineBreak, isNewLine} from "acorn"
import {dummyValue} from "./parseutil"

function noop() {}

Expand Down Expand Up @@ -63,13 +64,13 @@ export class LooseParser {

dummyIdent() {
let dummy = this.dummyNode("Identifier")
dummy.name = "✖"
dummy.name = dummyValue
return dummy
}

dummyString() {
let dummy = this.dummyNode("Literal")
dummy.value = dummy.raw = "✖"
dummy.value = dummy.raw = dummyValue
return dummy
}

Expand Down
3 changes: 2 additions & 1 deletion acorn-loose/src/tokenize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {tokTypes as tt, Token, isNewLine, SourceLocation, getLineInfo, lineBreakG} from "acorn"
import {LooseParser} from "./state"
import {dummyValue} from "./parseutil"

const lp = LooseParser.prototype

Expand Down Expand Up @@ -73,7 +74,7 @@ lp.readToken = function() {
throw e
}
this.resetTo(pos)
if (replace === true) replace = {start: pos, end: pos, type: tt.name, value: "✖"}
if (replace === true) replace = {start: pos, end: pos, type: tt.name, value: dummyValue}
if (replace) {
if (this.options.locations)
replace.loc = new SourceLocation(
Expand Down

0 comments on commit 12ae8fe

Please sign in to comment.