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

Feature/node source location #2615

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mocharc.json
@@ -1,6 +1,6 @@
{
"reporter": "dot",
"timeout": 60000,
"forbid-only": true,
"forbid-only": false,
"recursive": true
}
4 changes: 3 additions & 1 deletion src/expression/node/AccessorNode.js
Expand Up @@ -30,8 +30,9 @@ export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({
* @param {Node} object The object from which to retrieve
* a property or subset.
* @param {IndexNode} index IndexNode containing ranges
* @param {SourceLocation | undefined} source Node source location
*/
function AccessorNode (object, index) {
function AccessorNode (object, index, source) {
if (!(this instanceof AccessorNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -45,6 +46,7 @@ export const createAccessorNode = /* #__PURE__ */ factory(name, dependencies, ({

this.object = object || null
this.index = index
this.source = source

// readonly property name
Object.defineProperty(this, 'name', {
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ArrayNode.js
Expand Up @@ -13,13 +13,15 @@ export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @extends {Node}
* Holds an 1-dimensional array with items
* @param {Node[]} [items] 1 dimensional array with items
* @param {SourceLocation | undefined} source Node source location
*/
function ArrayNode (items) {
function ArrayNode (items, source) {
if (!(this instanceof ArrayNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}

this.items = items || []
this.source = source

// validate input
if (!Array.isArray(this.items) || !this.items.every(isNode)) {
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/AssignmentNode.js
Expand Up @@ -41,15 +41,17 @@ export const createAssignmentNode = /* #__PURE__ */ factory(name, dependencies,
* the property is assigned to the
* global scope.
* @param {Node} value The value to be assigned
* @param {SourceLocation | undefined} source Node source location
*/
function AssignmentNode (object, index, value) {
function AssignmentNode (object, index, value, source) {
if (!(this instanceof AssignmentNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}

this.object = object
this.index = value ? index : null
this.value = value || index
this.source = source

// validate input
if (!isSymbolNode(object) && !isAccessorNode(object)) {
Expand Down
5 changes: 4 additions & 1 deletion src/expression/node/BlockNode.js
Expand Up @@ -17,8 +17,9 @@ export const createBlockNode = /* #__PURE__ */ factory(name, dependencies, ({ Re
* An array with blocks, where a block is constructed as an Object
* with properties block, which is a Node, and visible, which is
* a boolean. The property visible is optional and is true by default
* @param {SourceLocation | undefined} source Node source location
*/
function BlockNode (blocks) {
function BlockNode (blocks, source) {
if (!(this instanceof BlockNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -34,6 +35,8 @@ export const createBlockNode = /* #__PURE__ */ factory(name, dependencies, ({ Re

return { node, visible }
})

this.source = source
}

BlockNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ConditionalNode.js
Expand Up @@ -14,11 +14,12 @@ export const createConditionalNode = /* #__PURE__ */ factory(name, dependencies,
* @param {Node} condition Condition, must result in a boolean
* @param {Node} trueExpr Expression evaluated when condition is true
* @param {Node} falseExpr Expression evaluated when condition is true
* @param {SourceLocation | undefined} source Node source location
*
* @constructor ConditionalNode
* @extends {Node}
*/
function ConditionalNode (condition, trueExpr, falseExpr) {
function ConditionalNode (condition, trueExpr, falseExpr, source) {
if (!(this instanceof ConditionalNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -29,6 +30,7 @@ export const createConditionalNode = /* #__PURE__ */ factory(name, dependencies,
this.condition = condition
this.trueExpr = trueExpr
this.falseExpr = falseExpr
this.source = source
}

ConditionalNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ConstantNode.js
Expand Up @@ -18,15 +18,17 @@ export const createConstantNode = /* #__PURE__ */ factory(name, dependencies, ({
* new ConstantNode('hello')
*
* @param {*} value Value can be any type (number, BigNumber, string, ...)
* @param {SourceLocation | undefined} source Node source location
* @constructor ConstantNode
* @extends {Node}
*/
function ConstantNode (value) {
function ConstantNode (value, source) {
if (!(this instanceof ConstantNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}

this.value = value
this.source = source
}

ConstantNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/FunctionAssignmentNode.js
Expand Up @@ -25,8 +25,9 @@ export const createFunctionAssignmentNode = /* #__PURE__ */ factory(name, depend
* array with objects containing the name
* and type of the parameter
* @param {Node} expr The function expression
* @param {SourceLocation | undefined} source Node source location
*/
function FunctionAssignmentNode (name, params, expr) {
function FunctionAssignmentNode (name, params, expr, source) {
if (!(this instanceof FunctionAssignmentNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -45,6 +46,7 @@ export const createFunctionAssignmentNode = /* #__PURE__ */ factory(name, depend
return (param && param.type) || 'any'
})
this.expr = expr
this.source = source
}

FunctionAssignmentNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/FunctionNode.js
Expand Up @@ -21,8 +21,9 @@ export const createFunctionNode = /* #__PURE__ */ factory(name, dependencies, ({
* @param {./Node | string} fn Node resolving with a function on which to invoke
* the arguments, typically a SymboNode or AccessorNode
* @param {./Node[]} args
* @param {SourceLocation | undefined} source Node source location
*/
function FunctionNode (fn, args) {
function FunctionNode (fn, args, source) {
if (!(this instanceof FunctionNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -39,6 +40,7 @@ export const createFunctionNode = /* #__PURE__ */ factory(name, dependencies, ({

this.fn = fn
this.args = args || []
this.source = source

// readonly property name
Object.defineProperty(this, 'name', {
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/IndexNode.js
Expand Up @@ -25,14 +25,16 @@ export const createIndexNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* notation like `a.b`, or using bracket
* notation like `a["b"]` (default).
* Used to stringify an IndexNode.
* @param {SourceLocation | undefined} source Node source location
*/
function IndexNode (dimensions, dotNotation) {
function IndexNode (dimensions, dotNotation, source) {
if (!(this instanceof IndexNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}

this.dimensions = dimensions
this.dotNotation = dotNotation || false
this.source = source

// validate input
if (!Array.isArray(dimensions) || !dimensions.every(isNode)) {
Expand Down
2 changes: 2 additions & 0 deletions src/expression/node/Node.js
Expand Up @@ -33,6 +33,8 @@ export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWit

Node.prototype.comment = ''

Node.prototype.source = null

/**
* Compile the node into an optimized, evauatable JavaScript function
* @return {{evaluate: function([Object])}} object
Expand Down
5 changes: 4 additions & 1 deletion src/expression/node/ObjectNode.js
Expand Up @@ -15,8 +15,9 @@ export const createObjectNode = /* #__PURE__ */ factory(name, dependencies, ({ N
* @extends {Node}
* Holds an object with keys/values
* @param {Object.<string, Node>} [properties] object with key/value pairs
* @param {SourceLocation | undefined} source Node source location
*/
function ObjectNode (properties) {
function ObjectNode (properties, source) {
if (!(this instanceof ObjectNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -31,6 +32,8 @@ export const createObjectNode = /* #__PURE__ */ factory(name, dependencies, ({ N
throw new TypeError('Object containing Nodes expected')
}
}

this.source = source
}

ObjectNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/OperatorNode.js
Expand Up @@ -22,8 +22,9 @@ export const createOperatorNode = /* #__PURE__ */ factory(name, dependencies, ({
* @param {Node[]} args Operator arguments
* @param {boolean} [implicit] Is this an implicit multiplication?
* @param {boolean} [isPercentage] Is this an percentage Operation?
* @param {SourceLocation | undefined} source Node source location
*/
function OperatorNode (op, fn, args, implicit, isPercentage) {
function OperatorNode (op, fn, args, implicit, isPercentage, source) {
if (!(this instanceof OperatorNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -44,6 +45,7 @@ export const createOperatorNode = /* #__PURE__ */ factory(name, dependencies, ({
this.op = op
this.fn = fn
this.args = args || []
this.source = source
}

OperatorNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/ParenthesisNode.js
Expand Up @@ -12,9 +12,10 @@ export const createParenthesisNode = /* #__PURE__ */ factory(name, dependencies,
* @extends {Node}
* A parenthesis node describes manual parenthesis from the user input
* @param {Node} content
* @param {SourceLocation | undefined} source Node source location
* @extends {Node}
*/
function ParenthesisNode (content) {
function ParenthesisNode (content, source) {
if (!(this instanceof ParenthesisNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -25,6 +26,7 @@ export const createParenthesisNode = /* #__PURE__ */ factory(name, dependencies,
}

this.content = content
this.source = source
}

ParenthesisNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/RangeNode.js
Expand Up @@ -15,8 +15,9 @@ export const createRangeNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* @param {Node} start included lower-bound
* @param {Node} end included upper-bound
* @param {Node} [step] optional step
* @param {SourceLocation | undefined} source Node source location
*/
function RangeNode (start, end, step) {
function RangeNode (start, end, step, source) {
if (!(this instanceof RangeNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -30,6 +31,7 @@ export const createRangeNode = /* #__PURE__ */ factory(name, dependencies, ({ No
this.start = start // included lower-bound
this.end = end // included upper-bound
this.step = step || null // optional step
this.source = source
}

RangeNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/RelationalNode.js
Expand Up @@ -15,11 +15,12 @@ export const createRelationalNode = /* #__PURE__ */ factory(name, dependencies,
*
* @param {String[]} conditionals An array of conditional operators used to compare the parameters
* @param {Node[]} params The parameters that will be compared
* @param {SourceLocation | undefined} source Node source location
*
* @constructor RelationalNode
* @extends {Node}
*/
function RelationalNode (conditionals, params) {
function RelationalNode (conditionals, params, source) {
if (!(this instanceof RelationalNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -29,6 +30,7 @@ export const createRelationalNode = /* #__PURE__ */ factory(name, dependencies,

this.conditionals = conditionals
this.params = params
this.source = source
}

RelationalNode.prototype = new Node()
Expand Down
4 changes: 3 additions & 1 deletion src/expression/node/SymbolNode.js
Expand Up @@ -14,6 +14,7 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
/**
* Check whether some name is a valueless unit like "inch".
* @param {string} name
* @param {SourceLocation | undefined} source Node source location
* @return {boolean}
*/
function isValuelessUnit (name) {
Expand All @@ -27,7 +28,7 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
* @param {string} name
* @extends {Node}
*/
function SymbolNode (name) {
function SymbolNode (name, source) {
if (!(this instanceof SymbolNode)) {
throw new SyntaxError('Constructor must be called with the new operator')
}
Expand All @@ -36,6 +37,7 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
if (typeof name !== 'string') throw new TypeError('String expected for parameter "name"')

this.name = name
this.source = source
}

SymbolNode.prototype = new Node()
Expand Down