Comparing version 0.1.8 to 0.2.0
@@ -24,3 +24,6 @@ 'use strict'; | ||
if (deep) { | ||
var _iterator$feedback; | ||
const isLastNode = iterator.nextNode === null || iterator.nextNode === 'KeyExpression'; | ||
(_iterator$feedback = iterator.feedback).mutatesPos || (_iterator$feedback.mutatesPos = !isLastNode); | ||
const right = builders.sequenceExpression([builders.assignmentExpression('=', internalScope['default'].pos, isLastNode ? builders.conditionalExpression(builders.binaryExpression('!==', scope['default'].property, builders.literal(value)), builders.numericLiteral(-1), scope['default'].depth) : builders.callExpression(builders.memberExpression(scope['default'].path, builders.identifier('indexOf')), [builders.literal(value), iterator.state.pos === 0 ? internalScope['default'].pos : builders.binaryExpression('+', internalScope['default'].pos, builders.numericLiteral(1))])), builders.binaryExpression('===', internalScope['default'].pos, builders.numericLiteral(-1))]); | ||
@@ -99,7 +102,10 @@ | ||
value | ||
}) { | ||
}, tree) { | ||
var _iterator$feedback2; | ||
const esTree = jsep['default'](value); | ||
assertDefinedIdentifier(esTree); | ||
const node = builders.unaryExpression('!', rewriteESTree(esTree, iterator.state.fixed && iterator.state.pos > 0 && iterator.nextNode !== null ? iterator.state.pos + 1 : iterator.state.inverted && iterator.state.pos !== 0 ? iterator.state.pos - 1 : 0)); | ||
const node = builders.unaryExpression('!', rewriteESTree(tree, esTree, iterator.state.fixed && iterator.state.pos > 0 && iterator.nextNode !== null ? iterator.state.pos + 1 : iterator.state.inverted && iterator.state.pos !== 0 ? iterator.state.pos - 1 : 0)); | ||
if (iterator.feedback.bailed || !deep || iterator.state.inverted) return node; | ||
(_iterator$feedback2 = iterator.feedback).mutatesPos || (_iterator$feedback2.mutatesPos = iterator.nextNode !== null && iterator.nextNode !== 'KeyExpression'); | ||
const assignment = builders.sequenceExpression([builders.assignmentExpression('=', internalScope['default'].pos, builders.conditionalExpression(node, builders.numericLiteral(-1), scope['default'].depth)), builders.binaryExpression('===', internalScope['default'].pos, builders.numericLiteral(-1))]); | ||
@@ -109,3 +115,3 @@ if (iterator.state.pos === 0) return assignment; | ||
} | ||
function rewriteESTree(node, pos) { | ||
function rewriteESTree(tree, node, pos) { | ||
switch (node.type) { | ||
@@ -116,3 +122,3 @@ case 'LogicalExpression': | ||
node.operator = '==='; | ||
node.left = builders.callExpression(builders.memberExpression(node.right, builders.identifier('includes')), [rewriteESTree(node.left, pos)]); | ||
node.left = builders.callExpression(builders.memberExpression(node.right, builders.identifier('includes')), [rewriteESTree(tree, node.left, pos)]); | ||
node.right = builders.booleanLiteral(true); | ||
@@ -126,7 +132,7 @@ } else if (node.operator === '~=') { | ||
node.left = builders.callExpression(builders.memberExpression(builders.regExpLiteral(node.right.value, ''), builders.identifier('test')), [rewriteESTree(node.left, pos)]); | ||
node.left = builders.callExpression(builders.memberExpression(builders.regExpLiteral(node.right.value, ''), builders.identifier('test')), [rewriteESTree(tree, node.left, pos)]); | ||
node.right = builders.booleanLiteral(true); | ||
} else { | ||
node.left = rewriteESTree(node.left, pos); | ||
node.right = rewriteESTree(node.right, pos); | ||
node.left = rewriteESTree(tree, node.left, pos); | ||
node.right = rewriteESTree(tree, node.right, pos); | ||
assertDefinedIdentifier(node.left); | ||
@@ -139,3 +145,3 @@ assertDefinedIdentifier(node.right); | ||
case 'UnaryExpression': | ||
node.argument = rewriteESTree(node.argument, pos); | ||
node.argument = rewriteESTree(tree, node.argument, pos); | ||
assertDefinedIdentifier(node.argument); | ||
@@ -145,5 +151,5 @@ return node; | ||
case 'MemberExpression': | ||
node.object = rewriteESTree(node.object, pos); | ||
node.object = rewriteESTree(tree, node.object, pos); | ||
assertDefinedIdentifier(node.object); | ||
node.property = rewriteESTree(node.property, pos); | ||
node.property = rewriteESTree(tree, node.property, pos); | ||
@@ -158,7 +164,7 @@ if (node.computed) { | ||
if (node.callee.type === 'Identifier' && node.callee.name.startsWith('@')) { | ||
return processAtIdentifier(node.callee.name, pos); | ||
return processAtIdentifier(tree, node.callee.name, pos); | ||
} | ||
node.callee = rewriteESTree(node.callee, pos); | ||
node.arguments = node.arguments.map(argument => rewriteESTree(argument, pos)); | ||
node.callee = rewriteESTree(tree, node.callee, pos); | ||
node.arguments = node.arguments.map(argument => rewriteESTree(tree, argument, pos)); | ||
@@ -174,3 +180,3 @@ if (node.callee.type === 'MemberExpression' && node.callee.object === sandbox['default'].property && node.callee.property.name in String.prototype) { | ||
if (node.name.startsWith('@')) { | ||
return processAtIdentifier(node.name, pos); | ||
return processAtIdentifier(tree, node.name, pos); | ||
} | ||
@@ -192,3 +198,3 @@ | ||
function processAtIdentifier(name, pos) { | ||
function processAtIdentifier(tree, name, pos) { | ||
switch (name) { | ||
@@ -234,2 +240,8 @@ case '@': | ||
default: | ||
if (name.startsWith('@@')) { | ||
const shorthandName = name.slice(2); | ||
tree.attachCustomShorthand(shorthandName); | ||
return builders.callExpression(builders.memberExpression(internalScope['default'].shorthands, builders.identifier(shorthandName)), [scope['default']._]); | ||
} | ||
throw new SyntaxError(`Unsupported shorthand '${name}'`); | ||
@@ -236,0 +248,0 @@ } |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var iterator = require('../iterator.js'); | ||
var index$1 = require('../optimizer/index.js'); | ||
var emitCall = require('../templates/emit-call.js'); | ||
@@ -94,3 +95,3 @@ var fnParams = require('../templates/fn-params.js'); | ||
case 'ScriptFilterExpression': | ||
treeNode = generators.generateFilterScriptExpression(iterator$1, node); | ||
treeNode = generators.generateFilterScriptExpression(iterator$1, node, tree$1); | ||
zone === null || zone === void 0 ? void 0 : zone.resize(); | ||
@@ -135,2 +136,3 @@ break; | ||
index$1['default'](branch, iterator$1); | ||
tree$1.push(builders.blockStatement(branch), 'tree-method'); | ||
@@ -137,0 +139,0 @@ zone === null || zone === void 0 ? void 0 : zone.attach(); |
@@ -19,3 +19,3 @@ 'use strict'; | ||
const condition = builders.unaryExpression('!', generators.rewriteESTree(jsep['default'](nodes[0].value), 0), true); | ||
const condition = builders.unaryExpression('!', generators.rewriteESTree(tree, jsep['default'](nodes[0].value), 0), true); | ||
tree.pushAll([[builders.blockStatement([...(guards.isDeep(nodes[0]) ? [] : [TOP_LEVEL_DEPTH_IF_STATEMENT]), builders.ifStatement(condition, builders.returnStatement()), emitCall['default'](ctx.id, ctx.iterator.modifiers)]), 'tree-method'], [builders.stringLiteral(ctx.id), 'traverse']]); | ||
@@ -22,0 +22,0 @@ |
@@ -19,2 +19,3 @@ 'use strict'; | ||
let hadFlatMemberExpressions = false; | ||
let deepNodes = 1; | ||
@@ -24,3 +25,5 @@ for (; i < nodes.length - 1; i++) { | ||
if (!guards.isDeep(node)) { | ||
if (guards.isDeep(node)) { | ||
deepNodes++; | ||
} else { | ||
hadFlatMemberExpressions || (hadFlatMemberExpressions = guards.isMemberExpression(node) || guards.isWildcardExpression(node)); | ||
@@ -38,3 +41,3 @@ continue; | ||
return guards.isDeep(nodes[nodes.length - 1]) ? hadFlatMemberExpressions || guards.isWildcardExpression(nodes[nodes.length - 1]) : false; | ||
return guards.isDeep(nodes[nodes.length - 1]) ? hadFlatMemberExpressions || guards.isWildcardExpression(nodes[nodes.length - 1]) : deepNodes > 1; | ||
} else { | ||
@@ -192,3 +195,5 @@ deep = true; | ||
Object.assign(this.feedback, feedback); | ||
Object.assign(this.feedback, { ...feedback, | ||
mutatesPos: this.feedback.mutatesPos | ||
}); | ||
} | ||
@@ -195,0 +200,0 @@ |
@@ -9,2 +9,3 @@ 'use strict'; | ||
pos: builders.identifier('pos'), | ||
shorthands: builders.identifier('shorthands'), | ||
tree: builders.identifier('tree') | ||
@@ -11,0 +12,0 @@ }; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var jsep = require('../../parser/jsep.js'); | ||
var builders = require('../ast/builders.js'); | ||
@@ -43,2 +44,3 @@ var dump = require('../dump.js'); | ||
#tree = builders.objectExpression([]); | ||
#shorthands = builders.objectExpression([]); | ||
#runtimeDependencies = new Set(['Scope']); | ||
@@ -48,4 +50,6 @@ #program = new Set(); | ||
#traverse = new Set(); | ||
#availableShorthands; | ||
constructor({ | ||
customShorthands, | ||
format, | ||
@@ -58,2 +62,3 @@ npmProvider | ||
this.traversalZones = new traversalZones['default'](); | ||
this.#availableShorthands = customShorthands; | ||
} | ||
@@ -71,2 +76,10 @@ | ||
attachCustomShorthand(name) { | ||
if (this.#availableShorthands === null || !(name in this.#availableShorthands)) { | ||
throw new ReferenceError(`Shorthand '${name}' is not defined`); | ||
} | ||
this.#shorthands.properties.push(builders.objectMethod('method', builders.identifier(name), fnParams['default'], builders.blockStatement([builders.returnStatement(jsep['default'](this.#availableShorthands[name]))]))); | ||
} | ||
getMethodByHash(hash) { | ||
@@ -110,3 +123,3 @@ return this.#tree.properties.find(prop => prop.key.value === hash); | ||
const traversalZones = this.traversalZones.root; | ||
return dump['default'](builders.program([builders.importDeclaration([...this.#runtimeDependencies].map(dep => builders.importSpecifier(builders.identifier(dep), builders.identifier(dep))), builders.stringLiteral(`${this.npmProvider ?? ''}nimma/runtime`)), ...this.#program, traversalZones, this.#tree.properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].tree, this.#tree)]), builders.exportDefaultDeclaration(builders.functionDeclaration(null, params, builders.blockStatement([NEW_SCOPE_VARIABLE_DECLARATION, builders.tryStatement(builders.blockStatement([...this.#body, this.#traverse.size === 0 ? null : builders.expressionStatement(builders.callExpression(scope['default'].traverse, [builders.arrowFunctionExpression([], builders.blockStatement(Array.from(this.#traverse))), traversalZones === null ? builders.nullLiteral() : traversalZones.declarations[0].id]))].filter(Boolean)), null, builders.blockStatement([builders.expressionStatement(builders.callExpression(scope['default'].destroy, []))]))].filter(Boolean))))].filter(Boolean))); | ||
return dump['default'](builders.program([builders.importDeclaration([...this.#runtimeDependencies].map(dep => builders.importSpecifier(builders.identifier(dep), builders.identifier(dep))), builders.stringLiteral(`${this.npmProvider ?? ''}nimma/runtime`)), ...this.#program, traversalZones, this.#tree.properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].tree, this.#tree)]), this.#shorthands.properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].shorthands, this.#shorthands)]), builders.exportDefaultDeclaration(builders.functionDeclaration(null, params, builders.blockStatement([NEW_SCOPE_VARIABLE_DECLARATION, builders.tryStatement(builders.blockStatement([...this.#body, this.#traverse.size === 0 ? null : builders.expressionStatement(builders.callExpression(scope['default'].traverse, [builders.arrowFunctionExpression([], builders.blockStatement(Array.from(this.#traverse))), traversalZones === null ? builders.nullLiteral() : traversalZones.declarations[0].id]))].filter(Boolean)), null, builders.blockStatement([builders.expressionStatement(builders.callExpression(scope['default'].destroy, []))]))].filter(Boolean))))].filter(Boolean))); | ||
} | ||
@@ -113,0 +126,0 @@ |
@@ -19,3 +19,4 @@ 'use strict'; | ||
output = 'auto', | ||
npmProvider = null | ||
npmProvider = null, | ||
customShorthands = null | ||
} = {}) { | ||
@@ -29,2 +30,3 @@ this.#fallback = fallback; | ||
this.tree = index['default'](mappedExpressions, { | ||
customShorthands, | ||
format: output === 'auto' ? determineFormat['default']() : output, | ||
@@ -31,0 +33,0 @@ npmProvider |
@@ -14,8 +14,11 @@ import type Fallback from './codegen/fallback'; | ||
declare class Nimma { | ||
public readonly sourceCode: string; | ||
constructor( | ||
expressions: string[], | ||
opts?: { | ||
fallback: Fallback; | ||
customShorthands: Record<string, string> | null; | ||
fallback: Fallback | null; | ||
unsafe: boolean; | ||
output: 'ES2018' | 'ES2021' | 'auto' | ||
output: 'ES2018' | 'ES2021' | 'auto'; | ||
}, | ||
@@ -25,5 +28,4 @@ ): Nimma; | ||
public query(input: Input, callbacks: Record<string, Callback>): void; | ||
public readonly sourceCode: string; | ||
} | ||
export default Nimma; |
@@ -297,3 +297,7 @@ 'use strict'; | ||
var peg$f12 = function () { | ||
var peg$f12 = function (node) { | ||
return node.value; | ||
}; | ||
var peg$f13 = function () { | ||
return { | ||
@@ -304,3 +308,3 @@ type: "KeyExpression" | ||
var peg$f13 = function () { | ||
var peg$f14 = function () { | ||
return { | ||
@@ -311,19 +315,19 @@ type: "ParentExpression" | ||
var peg$f14 = function () { | ||
var peg$f15 = function () { | ||
return true; | ||
}; | ||
var peg$f15 = function () { | ||
var peg$f16 = function () { | ||
return false; | ||
}; | ||
var peg$f16 = function () { | ||
var peg$f17 = function () { | ||
return text().slice(1, -1); | ||
}; | ||
var peg$f17 = function () { | ||
var peg$f18 = function () { | ||
return Number(text()); | ||
}; | ||
var peg$f18 = function (value) { | ||
var peg$f19 = function (value) { | ||
return { | ||
@@ -703,2 +707,6 @@ type: "SliceExpression", | ||
if (s1 === peg$FAILED) { | ||
s1 = peg$parseCustomScriptFilterExpression(); | ||
} | ||
if (s1 !== peg$FAILED) { | ||
@@ -1263,2 +1271,49 @@ peg$savedPos = s0; | ||
function peg$parseCustomScriptFilterExpression() { | ||
var s0, s1, s2, s3, s4; | ||
s0 = peg$currPos; | ||
s1 = peg$currPos; | ||
s2 = peg$currPos; | ||
if (input.charCodeAt(peg$currPos) === 64) { | ||
s3 = peg$c9; | ||
peg$currPos++; | ||
} else { | ||
s3 = peg$FAILED; | ||
if (peg$silentFails === 0) { | ||
peg$fail(peg$e9); | ||
} | ||
} | ||
if (s3 !== peg$FAILED) { | ||
s4 = peg$parseJsonPathPlusFilterFunction(); | ||
if (s4 !== peg$FAILED) { | ||
peg$savedPos = s2; | ||
s2 = peg$f12(s4); | ||
} else { | ||
peg$currPos = s2; | ||
s2 = peg$FAILED; | ||
} | ||
} else { | ||
peg$currPos = s2; | ||
s2 = peg$FAILED; | ||
} | ||
if (s2 !== peg$FAILED) { | ||
s1 = input.substring(s1, peg$currPos); | ||
} else { | ||
s1 = s2; | ||
} | ||
if (s1 !== peg$FAILED) { | ||
peg$savedPos = s0; | ||
s1 = peg$f11(s1); | ||
} | ||
s0 = s1; | ||
return s0; | ||
} | ||
function peg$parseKeyExpression() { | ||
@@ -1281,3 +1336,3 @@ var s0, s1; | ||
peg$savedPos = s0; | ||
s1 = peg$f12(); | ||
s1 = peg$f13(); | ||
} | ||
@@ -1306,3 +1361,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f13(); | ||
s1 = peg$f14(); | ||
} | ||
@@ -1342,3 +1397,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f14(); | ||
s1 = peg$f15(); | ||
} | ||
@@ -1364,3 +1419,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f15(); | ||
s1 = peg$f16(); | ||
} | ||
@@ -1397,3 +1452,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f15(); | ||
s1 = peg$f16(); | ||
} | ||
@@ -1617,3 +1672,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f16(); | ||
s1 = peg$f17(); | ||
} | ||
@@ -1655,3 +1710,3 @@ | ||
peg$savedPos = s0; | ||
s0 = peg$f17(); | ||
s0 = peg$f18(); | ||
} else { | ||
@@ -2229,3 +2284,3 @@ peg$currPos = s0; | ||
peg$savedPos = s0; | ||
s0 = peg$f18(s5); | ||
s0 = peg$f19(s5); | ||
} else { | ||
@@ -2232,0 +2287,0 @@ peg$currPos = s0; |
@@ -14,8 +14,11 @@ import type Fallback from './codegen/fallback'; | ||
declare class Nimma { | ||
public readonly sourceCode: string; | ||
constructor( | ||
expressions: string[], | ||
opts?: { | ||
fallback: Fallback; | ||
customShorthands: Record<string, string> | null; | ||
fallback: Fallback | null; | ||
unsafe: boolean; | ||
output: 'ES2018' | 'ES2021' | 'auto' | ||
output: 'ES2018' | 'ES2021' | 'auto'; | ||
}, | ||
@@ -25,5 +28,4 @@ ): Nimma; | ||
public query(input: Input, callbacks: Record<string, Callback>): void; | ||
public readonly sourceCode: string; | ||
} | ||
export default Nimma; |
@@ -24,3 +24,6 @@ 'use strict'; | ||
if (deep) { | ||
var _iterator$feedback; | ||
const isLastNode = iterator.nextNode === null || iterator.nextNode === 'KeyExpression'; | ||
(_iterator$feedback = iterator.feedback).mutatesPos || (_iterator$feedback.mutatesPos = !isLastNode); | ||
const right = builders.sequenceExpression([builders.assignmentExpression('=', internalScope['default'].pos, isLastNode ? builders.conditionalExpression(builders.binaryExpression('!==', scope['default'].property, builders.literal(value)), builders.numericLiteral(-1), scope['default'].depth) : builders.callExpression(builders.memberExpression(scope['default'].path, builders.identifier('indexOf')), [builders.literal(value), iterator.state.pos === 0 ? internalScope['default'].pos : builders.binaryExpression('+', internalScope['default'].pos, builders.numericLiteral(1))])), builders.binaryExpression('===', internalScope['default'].pos, builders.numericLiteral(-1))]); | ||
@@ -99,7 +102,10 @@ | ||
value | ||
}) { | ||
}, tree) { | ||
var _iterator$feedback2; | ||
const esTree = jsep['default'](value); | ||
assertDefinedIdentifier(esTree); | ||
const node = builders.unaryExpression('!', rewriteESTree(esTree, iterator.state.fixed && iterator.state.pos > 0 && iterator.nextNode !== null ? iterator.state.pos + 1 : iterator.state.inverted && iterator.state.pos !== 0 ? iterator.state.pos - 1 : 0)); | ||
const node = builders.unaryExpression('!', rewriteESTree(tree, esTree, iterator.state.fixed && iterator.state.pos > 0 && iterator.nextNode !== null ? iterator.state.pos + 1 : iterator.state.inverted && iterator.state.pos !== 0 ? iterator.state.pos - 1 : 0)); | ||
if (iterator.feedback.bailed || !deep || iterator.state.inverted) return node; | ||
(_iterator$feedback2 = iterator.feedback).mutatesPos || (_iterator$feedback2.mutatesPos = iterator.nextNode !== null && iterator.nextNode !== 'KeyExpression'); | ||
const assignment = builders.sequenceExpression([builders.assignmentExpression('=', internalScope['default'].pos, builders.conditionalExpression(node, builders.numericLiteral(-1), scope['default'].depth)), builders.binaryExpression('===', internalScope['default'].pos, builders.numericLiteral(-1))]); | ||
@@ -109,3 +115,3 @@ if (iterator.state.pos === 0) return assignment; | ||
} | ||
function rewriteESTree(node, pos) { | ||
function rewriteESTree(tree, node, pos) { | ||
switch (node.type) { | ||
@@ -116,3 +122,3 @@ case 'LogicalExpression': | ||
node.operator = '==='; | ||
node.left = builders.callExpression(builders.memberExpression(node.right, builders.identifier('includes')), [rewriteESTree(node.left, pos)]); | ||
node.left = builders.callExpression(builders.memberExpression(node.right, builders.identifier('includes')), [rewriteESTree(tree, node.left, pos)]); | ||
node.right = builders.booleanLiteral(true); | ||
@@ -126,7 +132,7 @@ } else if (node.operator === '~=') { | ||
node.left = builders.callExpression(builders.memberExpression(builders.regExpLiteral(node.right.value, ''), builders.identifier('test')), [rewriteESTree(node.left, pos)]); | ||
node.left = builders.callExpression(builders.memberExpression(builders.regExpLiteral(node.right.value, ''), builders.identifier('test')), [rewriteESTree(tree, node.left, pos)]); | ||
node.right = builders.booleanLiteral(true); | ||
} else { | ||
node.left = rewriteESTree(node.left, pos); | ||
node.right = rewriteESTree(node.right, pos); | ||
node.left = rewriteESTree(tree, node.left, pos); | ||
node.right = rewriteESTree(tree, node.right, pos); | ||
assertDefinedIdentifier(node.left); | ||
@@ -139,3 +145,3 @@ assertDefinedIdentifier(node.right); | ||
case 'UnaryExpression': | ||
node.argument = rewriteESTree(node.argument, pos); | ||
node.argument = rewriteESTree(tree, node.argument, pos); | ||
assertDefinedIdentifier(node.argument); | ||
@@ -145,5 +151,5 @@ return node; | ||
case 'MemberExpression': | ||
node.object = rewriteESTree(node.object, pos); | ||
node.object = rewriteESTree(tree, node.object, pos); | ||
assertDefinedIdentifier(node.object); | ||
node.property = rewriteESTree(node.property, pos); | ||
node.property = rewriteESTree(tree, node.property, pos); | ||
@@ -158,7 +164,7 @@ if (node.computed) { | ||
if (node.callee.type === 'Identifier' && node.callee.name.startsWith('@')) { | ||
return processAtIdentifier(node.callee.name, pos); | ||
return processAtIdentifier(tree, node.callee.name, pos); | ||
} | ||
node.callee = rewriteESTree(node.callee, pos); | ||
node.arguments = node.arguments.map(argument => rewriteESTree(argument, pos)); | ||
node.callee = rewriteESTree(tree, node.callee, pos); | ||
node.arguments = node.arguments.map(argument => rewriteESTree(tree, argument, pos)); | ||
@@ -174,3 +180,3 @@ if (node.callee.type === 'MemberExpression' && node.callee.object === sandbox['default'].property && node.callee.property.name in String.prototype) { | ||
if (node.name.startsWith('@')) { | ||
return processAtIdentifier(node.name, pos); | ||
return processAtIdentifier(tree, node.name, pos); | ||
} | ||
@@ -192,3 +198,3 @@ | ||
function processAtIdentifier(name, pos) { | ||
function processAtIdentifier(tree, name, pos) { | ||
switch (name) { | ||
@@ -234,2 +240,8 @@ case '@': | ||
default: | ||
if (name.startsWith('@@')) { | ||
const shorthandName = name.slice(2); | ||
tree.attachCustomShorthand(shorthandName); | ||
return builders.callExpression(builders.memberExpression(internalScope['default'].shorthands, builders.identifier(shorthandName)), [scope['default']._]); | ||
} | ||
throw new SyntaxError(`Unsupported shorthand '${name}'`); | ||
@@ -236,0 +248,0 @@ } |
@@ -9,2 +9,3 @@ 'use strict'; | ||
var iterator = require('../iterator.js'); | ||
var index$1 = require('../optimizer/index.js'); | ||
var emitCall = require('../templates/emit-call.js'); | ||
@@ -94,3 +95,3 @@ var fnParams = require('../templates/fn-params.js'); | ||
case 'ScriptFilterExpression': | ||
treeNode = generators.generateFilterScriptExpression(iterator$1, node); | ||
treeNode = generators.generateFilterScriptExpression(iterator$1, node, tree$1); | ||
zone === null || zone === void 0 ? void 0 : zone.resize(); | ||
@@ -135,2 +136,3 @@ break; | ||
index$1['default'](branch, iterator$1); | ||
tree$1.push(builders.blockStatement(branch), 'tree-method'); | ||
@@ -137,0 +139,0 @@ zone === null || zone === void 0 ? void 0 : zone.attach(); |
@@ -19,3 +19,3 @@ 'use strict'; | ||
const condition = builders.unaryExpression('!', generators.rewriteESTree(jsep['default'](nodes[0].value), 0), true); | ||
const condition = builders.unaryExpression('!', generators.rewriteESTree(tree, jsep['default'](nodes[0].value), 0), true); | ||
tree.pushAll([[builders.blockStatement([...(guards.isDeep(nodes[0]) ? [] : [TOP_LEVEL_DEPTH_IF_STATEMENT]), builders.ifStatement(condition, builders.returnStatement()), emitCall['default'](ctx.id, ctx.iterator.modifiers)]), 'tree-method'], [builders.stringLiteral(ctx.id), 'traverse']]); | ||
@@ -22,0 +22,0 @@ |
@@ -22,2 +22,3 @@ 'use strict'; | ||
let hadFlatMemberExpressions = false; | ||
let deepNodes = 1; | ||
@@ -27,3 +28,5 @@ for (; i < nodes.length - 1; i++) { | ||
if (!guards.isDeep(node)) { | ||
if (guards.isDeep(node)) { | ||
deepNodes++; | ||
} else { | ||
hadFlatMemberExpressions || (hadFlatMemberExpressions = guards.isMemberExpression(node) || guards.isWildcardExpression(node)); | ||
@@ -41,3 +44,3 @@ continue; | ||
return guards.isDeep(nodes[nodes.length - 1]) ? hadFlatMemberExpressions || guards.isWildcardExpression(nodes[nodes.length - 1]) : false; | ||
return guards.isDeep(nodes[nodes.length - 1]) ? hadFlatMemberExpressions || guards.isWildcardExpression(nodes[nodes.length - 1]) : deepNodes > 1; | ||
} else { | ||
@@ -207,3 +210,5 @@ deep = true; | ||
Object.assign(this.feedback, feedback); | ||
Object.assign(this.feedback, { ...feedback, | ||
mutatesPos: this.feedback.mutatesPos | ||
}); | ||
} | ||
@@ -210,0 +215,0 @@ |
@@ -9,2 +9,3 @@ 'use strict'; | ||
pos: builders.identifier('pos'), | ||
shorthands: builders.identifier('shorthands'), | ||
tree: builders.identifier('tree') | ||
@@ -11,0 +12,0 @@ }; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); | ||
var jsep = require('../../parser/jsep.js'); | ||
var builders = require('../ast/builders.js'); | ||
@@ -44,2 +45,4 @@ var dump = require('../dump.js'); | ||
var _shorthands = /*#__PURE__*/new WeakMap(); | ||
var _runtimeDependencies = /*#__PURE__*/new WeakMap(); | ||
@@ -53,4 +56,7 @@ | ||
var _availableShorthands = /*#__PURE__*/new WeakMap(); | ||
class ESTree { | ||
constructor({ | ||
customShorthands, | ||
format, | ||
@@ -64,2 +70,7 @@ npmProvider | ||
_shorthands.set(this, { | ||
writable: true, | ||
value: builders.objectExpression([]) | ||
}); | ||
_runtimeDependencies.set(this, { | ||
@@ -85,2 +96,7 @@ writable: true, | ||
_availableShorthands.set(this, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this.format = format; | ||
@@ -90,2 +106,4 @@ this.npmProvider = npmProvider; | ||
this.traversalZones = new traversalZones['default'](); | ||
_rollupPluginBabelHelpers.classPrivateFieldSet(this, _availableShorthands, customShorthands); | ||
} | ||
@@ -103,2 +121,10 @@ | ||
attachCustomShorthand(name) { | ||
if (_rollupPluginBabelHelpers.classPrivateFieldGet(this, _availableShorthands) === null || !(name in _rollupPluginBabelHelpers.classPrivateFieldGet(this, _availableShorthands))) { | ||
throw new ReferenceError(`Shorthand '${name}' is not defined`); | ||
} | ||
_rollupPluginBabelHelpers.classPrivateFieldGet(this, _shorthands).properties.push(builders.objectMethod('method', builders.identifier(name), fnParams['default'], builders.blockStatement([builders.returnStatement(jsep['default'](_rollupPluginBabelHelpers.classPrivateFieldGet(this, _availableShorthands)[name]))]))); | ||
} | ||
getMethodByHash(hash) { | ||
@@ -146,3 +172,3 @@ return _rollupPluginBabelHelpers.classPrivateFieldGet(this, _tree).properties.find(prop => prop.key.value === hash); | ||
const traversalZones = this.traversalZones.root; | ||
return dump['default'](builders.program([builders.importDeclaration([..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _runtimeDependencies)].map(dep => builders.importSpecifier(builders.identifier(dep), builders.identifier(dep))), builders.stringLiteral(`${(_this$npmProvider = this.npmProvider) !== null && _this$npmProvider !== void 0 ? _this$npmProvider : ''}nimma/legacy/runtime`)), ..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _program), traversalZones, _rollupPluginBabelHelpers.classPrivateFieldGet(this, _tree).properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].tree, _rollupPluginBabelHelpers.classPrivateFieldGet(this, _tree))]), builders.exportDefaultDeclaration(builders.functionDeclaration(null, params, builders.blockStatement([NEW_SCOPE_VARIABLE_DECLARATION, builders.tryStatement(builders.blockStatement([..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _body), _rollupPluginBabelHelpers.classPrivateFieldGet(this, _traverse).size === 0 ? null : builders.expressionStatement(builders.callExpression(scope['default'].traverse, [builders.arrowFunctionExpression([], builders.blockStatement(Array.from(_rollupPluginBabelHelpers.classPrivateFieldGet(this, _traverse)))), traversalZones === null ? builders.nullLiteral() : traversalZones.declarations[0].id]))].filter(Boolean)), null, builders.blockStatement([builders.expressionStatement(builders.callExpression(scope['default'].destroy, []))]))].filter(Boolean))))].filter(Boolean))); | ||
return dump['default'](builders.program([builders.importDeclaration([..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _runtimeDependencies)].map(dep => builders.importSpecifier(builders.identifier(dep), builders.identifier(dep))), builders.stringLiteral(`${(_this$npmProvider = this.npmProvider) !== null && _this$npmProvider !== void 0 ? _this$npmProvider : ''}nimma/legacy/runtime`)), ..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _program), traversalZones, _rollupPluginBabelHelpers.classPrivateFieldGet(this, _tree).properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].tree, _rollupPluginBabelHelpers.classPrivateFieldGet(this, _tree))]), _rollupPluginBabelHelpers.classPrivateFieldGet(this, _shorthands).properties.length === 0 ? null : builders.variableDeclaration('const', [builders.variableDeclarator(internalScope['default'].shorthands, _rollupPluginBabelHelpers.classPrivateFieldGet(this, _shorthands))]), builders.exportDefaultDeclaration(builders.functionDeclaration(null, params, builders.blockStatement([NEW_SCOPE_VARIABLE_DECLARATION, builders.tryStatement(builders.blockStatement([..._rollupPluginBabelHelpers.classPrivateFieldGet(this, _body), _rollupPluginBabelHelpers.classPrivateFieldGet(this, _traverse).size === 0 ? null : builders.expressionStatement(builders.callExpression(scope['default'].traverse, [builders.arrowFunctionExpression([], builders.blockStatement(Array.from(_rollupPluginBabelHelpers.classPrivateFieldGet(this, _traverse)))), traversalZones === null ? builders.nullLiteral() : traversalZones.declarations[0].id]))].filter(Boolean)), null, builders.blockStatement([builders.expressionStatement(builders.callExpression(scope['default'].destroy, []))]))].filter(Boolean))))].filter(Boolean))); | ||
} | ||
@@ -149,0 +175,0 @@ |
@@ -22,3 +22,4 @@ 'use strict'; | ||
output = 'auto', | ||
npmProvider = null | ||
npmProvider = null, | ||
customShorthands = null | ||
} = {}) { | ||
@@ -44,2 +45,3 @@ _fallback.set(this, { | ||
this.tree = index['default'](mappedExpressions, { | ||
customShorthands, | ||
format: output === 'auto' ? determineFormat['default']() : output, | ||
@@ -46,0 +48,0 @@ npmProvider |
@@ -14,8 +14,11 @@ import type Fallback from './codegen/fallback'; | ||
declare class Nimma { | ||
public readonly sourceCode: string; | ||
constructor( | ||
expressions: string[], | ||
opts?: { | ||
fallback: Fallback; | ||
customShorthands: Record<string, string> | null; | ||
fallback: Fallback | null; | ||
unsafe: boolean; | ||
output: 'ES2018' | 'ES2021' | 'auto' | ||
output: 'ES2018' | 'ES2021' | 'auto'; | ||
}, | ||
@@ -25,5 +28,4 @@ ): Nimma; | ||
public query(input: Input, callbacks: Record<string, Callback>): void; | ||
public readonly sourceCode: string; | ||
} | ||
export default Nimma; |
@@ -297,3 +297,7 @@ 'use strict'; | ||
var peg$f12 = function () { | ||
var peg$f12 = function (node) { | ||
return node.value; | ||
}; | ||
var peg$f13 = function () { | ||
return { | ||
@@ -304,3 +308,3 @@ type: "KeyExpression" | ||
var peg$f13 = function () { | ||
var peg$f14 = function () { | ||
return { | ||
@@ -311,19 +315,19 @@ type: "ParentExpression" | ||
var peg$f14 = function () { | ||
var peg$f15 = function () { | ||
return true; | ||
}; | ||
var peg$f15 = function () { | ||
var peg$f16 = function () { | ||
return false; | ||
}; | ||
var peg$f16 = function () { | ||
var peg$f17 = function () { | ||
return text().slice(1, -1); | ||
}; | ||
var peg$f17 = function () { | ||
var peg$f18 = function () { | ||
return Number(text()); | ||
}; | ||
var peg$f18 = function (value) { | ||
var peg$f19 = function (value) { | ||
return { | ||
@@ -703,2 +707,6 @@ type: "SliceExpression", | ||
if (s1 === peg$FAILED) { | ||
s1 = peg$parseCustomScriptFilterExpression(); | ||
} | ||
if (s1 !== peg$FAILED) { | ||
@@ -1263,2 +1271,49 @@ peg$savedPos = s0; | ||
function peg$parseCustomScriptFilterExpression() { | ||
var s0, s1, s2, s3, s4; | ||
s0 = peg$currPos; | ||
s1 = peg$currPos; | ||
s2 = peg$currPos; | ||
if (input.charCodeAt(peg$currPos) === 64) { | ||
s3 = peg$c9; | ||
peg$currPos++; | ||
} else { | ||
s3 = peg$FAILED; | ||
if (peg$silentFails === 0) { | ||
peg$fail(peg$e9); | ||
} | ||
} | ||
if (s3 !== peg$FAILED) { | ||
s4 = peg$parseJsonPathPlusFilterFunction(); | ||
if (s4 !== peg$FAILED) { | ||
peg$savedPos = s2; | ||
s2 = peg$f12(s4); | ||
} else { | ||
peg$currPos = s2; | ||
s2 = peg$FAILED; | ||
} | ||
} else { | ||
peg$currPos = s2; | ||
s2 = peg$FAILED; | ||
} | ||
if (s2 !== peg$FAILED) { | ||
s1 = input.substring(s1, peg$currPos); | ||
} else { | ||
s1 = s2; | ||
} | ||
if (s1 !== peg$FAILED) { | ||
peg$savedPos = s0; | ||
s1 = peg$f11(s1); | ||
} | ||
s0 = s1; | ||
return s0; | ||
} | ||
function peg$parseKeyExpression() { | ||
@@ -1281,3 +1336,3 @@ var s0, s1; | ||
peg$savedPos = s0; | ||
s1 = peg$f12(); | ||
s1 = peg$f13(); | ||
} | ||
@@ -1306,3 +1361,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f13(); | ||
s1 = peg$f14(); | ||
} | ||
@@ -1342,3 +1397,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f14(); | ||
s1 = peg$f15(); | ||
} | ||
@@ -1364,3 +1419,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f15(); | ||
s1 = peg$f16(); | ||
} | ||
@@ -1397,3 +1452,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f15(); | ||
s1 = peg$f16(); | ||
} | ||
@@ -1617,3 +1672,3 @@ | ||
peg$savedPos = s0; | ||
s1 = peg$f16(); | ||
s1 = peg$f17(); | ||
} | ||
@@ -1655,3 +1710,3 @@ | ||
peg$savedPos = s0; | ||
s0 = peg$f17(); | ||
s0 = peg$f18(); | ||
} else { | ||
@@ -2229,3 +2284,3 @@ peg$currPos = s0; | ||
peg$savedPos = s0; | ||
s0 = peg$f18(s5); | ||
s0 = peg$f19(s5); | ||
} else { | ||
@@ -2232,0 +2287,0 @@ peg$currPos = s0; |
@@ -14,8 +14,11 @@ import type Fallback from './codegen/fallback'; | ||
declare class Nimma { | ||
public readonly sourceCode: string; | ||
constructor( | ||
expressions: string[], | ||
opts?: { | ||
fallback: Fallback; | ||
customShorthands: Record<string, string> | null; | ||
fallback: Fallback | null; | ||
unsafe: boolean; | ||
output: 'ES2018' | 'ES2021' | 'auto' | ||
output: 'ES2018' | 'ES2021' | 'auto'; | ||
}, | ||
@@ -25,5 +28,4 @@ ): Nimma; | ||
public query(input: Input, callbacks: Record<string, Callback>): void; | ||
public readonly sourceCode: string; | ||
} | ||
export default Nimma; |
{ | ||
"name": "nimma", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"description": "Scalable JSONPath engine.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
538655
217
15977