Skip to content

Commit

Permalink
Add source mappings for all nodes on parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-canestraro committed Mar 15, 2023
1 parent 0e777bf commit 742e662
Show file tree
Hide file tree
Showing 21 changed files with 532 additions and 85 deletions.
4 changes: 3 additions & 1 deletion src/expression/node/AccessorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({
* @return {AccessorNode}
*/
clone () {
return new AccessorNode(this.object, this.index)
const cloned = new AccessorNode(this.object, this.index)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ArrayNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @return {ArrayNode}
*/
clone () {
return new ArrayNode(this.items.slice(0))
const cloned = new ArrayNode(this.items.slice(0))
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/AssignmentNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies,
* @return {AssignmentNode}
*/
clone () {
return new AssignmentNode(this.object, this.index, this.value)
const cloned = new AssignmentNode(this.object, this.index, this.value)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/BlockNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ export const createBlockNode = /* #__PURE__ */ factory(name, dependencies, ({ Re
}
})

return new BlockNode(blocks)
const cloned = new BlockNode(blocks)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ConditionalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export const createConditionalNode = /* #__PURE__ */ factory(name, dependencies,
* @return {ConditionalNode}
*/
clone () {
return new ConditionalNode(this.condition, this.trueExpr, this.falseExpr)
const cloned = new ConditionalNode(this.condition, this.trueExpr, this.falseExpr)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ConstantNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({
* @return {ConstantNode}
*/
clone () {
return new ConstantNode(this.value)
const cloned = new ConstantNode(this.value)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/FunctionAssignmentNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ export const createFunctionAssignmentNode = /* #__PURE__ */ factory(name, depend
* @return {FunctionAssignmentNode}
*/
clone () {
return new FunctionAssignmentNode(
const cloned = new FunctionAssignmentNode(
this.name, this.params.slice(0), this.expr)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/FunctionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ export const createFunctionNode = /* #__PURE__ */ factory(name, dependencies, ({
* @return {FunctionNode}
*/
clone () {
return new FunctionNode(this.fn, this.args.slice(0))
const cloned = new FunctionNode(this.fn, this.args.slice(0))
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/IndexNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export const createIndexNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @return {IndexNode}
*/
clone () {
return new IndexNode(this.dimensions.slice(0), this.dotNotation)
const cloned = new IndexNode(this.dimensions.slice(0), this.dotNotation)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/expression/node/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWit
}

class Node {
sources = []

get type () { return 'Node' }
get isNode () { return true }

Expand Down Expand Up @@ -204,6 +206,15 @@ export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWit
throw new Error('Cannot clone a Node interface')
}

/**
* Set the source indices mapping this node back to its
* location in the original source string
* @param {SourceMapping[]} sources - the data mapping this node back to its source string
*/
setSources (sources) {
this.sources = sources
}

/**
* Create a deep clone of this node
* @return {Node}
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ObjectNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export const createObjectNode = /* #__PURE__ */ factory(name, dependencies, ({ N
properties[key] = this.properties[key]
}
}
return new ObjectNode(properties)
const cloned = new ObjectNode(properties)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/OperatorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ export const createOperatorNode = /* #__PURE__ */ factory(name, dependencies, ({
* @return {OperatorNode}
*/
clone () {
return new OperatorNode(
const cloned = new OperatorNode(
this.op, this.fn, this.args.slice(0), this.implicit, this.isPercentage)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ParenthesisNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export const createParenthesisNode = /* #__PURE__ */ factory(name, dependencies,
* @return {ParenthesisNode}
*/
clone () {
return new ParenthesisNode(this.content)
const cloned = new ParenthesisNode(this.content)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/RangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export const createRangeNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @return {RangeNode}
*/
clone () {
return new RangeNode(this.start, this.end, this.step && this.step)
const cloned = new RangeNode(this.start, this.end, this.step && this.step)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/RelationalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export const createRelationalNode = /* #__PURE__ */ factory(name, dependencies,
* @return {RelationalNode}
*/
clone () {
return new RelationalNode(this.conditionals, this.params)
const cloned = new RelationalNode(this.conditionals, this.params)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/SymbolNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
* @return {SymbolNode}
*/
clone () {
return new SymbolNode(this.name)
const cloned = new SymbolNode(this.name)
cloned.sources = this.sources
return cloned
}

/**
Expand Down
Loading

0 comments on commit 742e662

Please sign in to comment.