bare-script 2.2.16 → 2.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/library.js +3 -5
- package/lib/parser.js +7 -7
- package/lib/runtime.js +12 -7
- package/lib/runtimeAsync.js +10 -5
- package/package.json +1 -1
package/lib/library.js
CHANGED
|
@@ -682,7 +682,7 @@ export const scriptFunctions = {
|
|
|
682
682
|
// $doc: Escape a string for use in a regular expression
|
|
683
683
|
// $arg string: The string to escape
|
|
684
684
|
// $return: The escaped string
|
|
685
|
-
'regexEscape': ([string]) => (typeof string === 'string' ? string.replace(
|
|
685
|
+
'regexEscape': ([string]) => (typeof string === 'string' ? string.replace(rRegexEscape, '\\$&') : null),
|
|
686
686
|
|
|
687
687
|
// $function: regexMatch
|
|
688
688
|
// $group: Regex
|
|
@@ -1055,7 +1055,7 @@ export const scriptFunctions = {
|
|
|
1055
1055
|
|
|
1056
1056
|
|
|
1057
1057
|
// Regex escape regular expression
|
|
1058
|
-
const
|
|
1058
|
+
const rRegexEscape = /[.*+?^${}()|[\]\\]/g;
|
|
1059
1059
|
|
|
1060
1060
|
|
|
1061
1061
|
// Fixed-number trim regular expression
|
|
@@ -1108,8 +1108,6 @@ export const expressionFunctionMap = {
|
|
|
1108
1108
|
'today': 'datetimeToday',
|
|
1109
1109
|
'trim': 'stringTrim',
|
|
1110
1110
|
'upper': 'stringUpper',
|
|
1111
|
-
'urlEncode': 'urlEncode',
|
|
1112
|
-
'urlEncodeComponent': 'urlEncodeComponent',
|
|
1113
1111
|
'year': 'datetimeYear'
|
|
1114
1112
|
};
|
|
1115
1113
|
|
|
@@ -1117,4 +1115,4 @@ export const expressionFunctionMap = {
|
|
|
1117
1115
|
// The built-in expression functions
|
|
1118
1116
|
export const expressionFunctions = Object.fromEntries(Object.entries(expressionFunctionMap).map(
|
|
1119
1117
|
([exprFnName, scriptFnName]) => [exprFnName, scriptFunctions[scriptFnName]]
|
|
1120
|
-
)
|
|
1118
|
+
));
|
package/lib/parser.js
CHANGED
|
@@ -63,6 +63,11 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
63
63
|
for (const [ixLinePart, linePart] of lines.entries()) {
|
|
64
64
|
const statements = (functionDef !== null ? functionDef.function.statements : script.statements);
|
|
65
65
|
|
|
66
|
+
// Comment?
|
|
67
|
+
if (linePart.match(rScriptComment) !== null) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
// Set the line index
|
|
67
72
|
const isContinued = (lineContinuation.length !== 0);
|
|
68
73
|
if (!isContinued) {
|
|
@@ -72,7 +77,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
72
77
|
// Line continuation?
|
|
73
78
|
const linePartNoContinuation = linePart.replace(rScriptContinuation, '');
|
|
74
79
|
if (linePart !== linePartNoContinuation) {
|
|
75
|
-
lineContinuation.push(
|
|
80
|
+
lineContinuation.push(isContinued ? linePartNoContinuation.trim() : linePartNoContinuation.trimEnd());
|
|
76
81
|
continue;
|
|
77
82
|
} else if (isContinued) {
|
|
78
83
|
lineContinuation.push(linePartNoContinuation.trim());
|
|
@@ -87,11 +92,6 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
87
92
|
line = linePart;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
// Comment?
|
|
91
|
-
if (line.match(rScriptComment) !== null) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
95
|
// Assignment?
|
|
96
96
|
const matchAssignment = line.match(rScriptAssignment);
|
|
97
97
|
if (matchAssignment !== null) {
|
|
@@ -709,7 +709,7 @@ export class BareScriptParserError extends Error {
|
|
|
709
709
|
let lineError = line;
|
|
710
710
|
let lineColumn = columnNumber;
|
|
711
711
|
if (line.length > lineLengthMax) {
|
|
712
|
-
const lineLeft = columnNumber - 1 -
|
|
712
|
+
const lineLeft = columnNumber - 1 - lineLengthMax / 2;
|
|
713
713
|
const lineRight = lineLeft + lineLengthMax;
|
|
714
714
|
if (lineLeft < 0) {
|
|
715
715
|
lineError = line.slice(0, lineLengthMax) + lineSuffix;
|
package/lib/runtime.js
CHANGED
|
@@ -176,13 +176,15 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
176
176
|
// Number
|
|
177
177
|
if (exprKey === 'number') {
|
|
178
178
|
return expr.number;
|
|
179
|
+
}
|
|
179
180
|
|
|
180
181
|
// String
|
|
181
|
-
|
|
182
|
+
if (exprKey === 'string') {
|
|
182
183
|
return expr.string;
|
|
184
|
+
}
|
|
183
185
|
|
|
184
186
|
// Variable
|
|
185
|
-
|
|
187
|
+
if (exprKey === 'variable') {
|
|
186
188
|
// Keywords
|
|
187
189
|
if (expr.variable === 'null') {
|
|
188
190
|
return null;
|
|
@@ -198,9 +200,10 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
198
200
|
varValue = (globals !== null ? (globals[expr.variable] ?? null) : null);
|
|
199
201
|
}
|
|
200
202
|
return varValue;
|
|
203
|
+
}
|
|
201
204
|
|
|
202
205
|
// Function
|
|
203
|
-
|
|
206
|
+
if (exprKey === 'function') {
|
|
204
207
|
// "if" built-in function?
|
|
205
208
|
const funcName = expr.function.name;
|
|
206
209
|
if (funcName === 'if') {
|
|
@@ -220,7 +223,7 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
220
223
|
if (typeof funcValue === 'undefined') {
|
|
221
224
|
funcValue = (globals !== null ? globals[funcName] : undefined);
|
|
222
225
|
if (typeof funcValue === 'undefined') {
|
|
223
|
-
funcValue = (builtins ? expressionFunctions[funcName]
|
|
226
|
+
funcValue = (builtins ? (expressionFunctions[funcName] ?? null) : null);
|
|
224
227
|
}
|
|
225
228
|
}
|
|
226
229
|
if (funcValue !== null) {
|
|
@@ -247,9 +250,10 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
247
250
|
}
|
|
248
251
|
|
|
249
252
|
throw new BareScriptRuntimeError(`Undefined function "${funcName}"`);
|
|
253
|
+
}
|
|
250
254
|
|
|
251
255
|
// Binary expression
|
|
252
|
-
|
|
256
|
+
if (exprKey === 'binary') {
|
|
253
257
|
const binOp = expr.binary.op;
|
|
254
258
|
const leftValue = evaluateExpression(expr.binary.left, options, locals, builtins);
|
|
255
259
|
|
|
@@ -287,9 +291,10 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
287
291
|
}
|
|
288
292
|
// else if (binOp === '!=')
|
|
289
293
|
return leftValue !== rightValue;
|
|
294
|
+
}
|
|
290
295
|
|
|
291
296
|
// Unary expression
|
|
292
|
-
|
|
297
|
+
if (exprKey === 'unary') {
|
|
293
298
|
const unaryOp = expr.unary.op;
|
|
294
299
|
const value = evaluateExpression(expr.unary.expr, options, locals, builtins);
|
|
295
300
|
if (unaryOp === '!') {
|
|
@@ -312,7 +317,7 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
312
317
|
*/
|
|
313
318
|
export class BareScriptRuntimeError extends Error {
|
|
314
319
|
/**
|
|
315
|
-
* Create a BareScript
|
|
320
|
+
* Create a BareScript runtime error
|
|
316
321
|
*
|
|
317
322
|
* @param {string} message - The runtime error message
|
|
318
323
|
*/
|
package/lib/runtimeAsync.js
CHANGED
|
@@ -244,13 +244,15 @@ export async function evaluateExpressionAsync(expr, options = null, locals = nul
|
|
|
244
244
|
// Number
|
|
245
245
|
if (exprKey === 'number') {
|
|
246
246
|
return expr.number;
|
|
247
|
+
}
|
|
247
248
|
|
|
248
249
|
// String
|
|
249
|
-
|
|
250
|
+
if (exprKey === 'string') {
|
|
250
251
|
return expr.string;
|
|
252
|
+
}
|
|
251
253
|
|
|
252
254
|
// Variable
|
|
253
|
-
|
|
255
|
+
if (exprKey === 'variable') {
|
|
254
256
|
// Keywords
|
|
255
257
|
if (expr.variable === 'null') {
|
|
256
258
|
return null;
|
|
@@ -266,9 +268,10 @@ export async function evaluateExpressionAsync(expr, options = null, locals = nul
|
|
|
266
268
|
varValue = (globals !== null ? (globals[expr.variable] ?? null) : null);
|
|
267
269
|
}
|
|
268
270
|
return varValue;
|
|
271
|
+
}
|
|
269
272
|
|
|
270
273
|
// Function
|
|
271
|
-
|
|
274
|
+
if (exprKey === 'function') {
|
|
272
275
|
// "if" built-in function?
|
|
273
276
|
const funcName = expr.function.name;
|
|
274
277
|
if (funcName === 'if') {
|
|
@@ -311,9 +314,10 @@ export async function evaluateExpressionAsync(expr, options = null, locals = nul
|
|
|
311
314
|
}
|
|
312
315
|
|
|
313
316
|
throw new BareScriptRuntimeError(`Undefined function "${funcName}"`);
|
|
317
|
+
}
|
|
314
318
|
|
|
315
319
|
// Binary expression
|
|
316
|
-
|
|
320
|
+
if (exprKey === 'binary') {
|
|
317
321
|
const binOp = expr.binary.op;
|
|
318
322
|
const leftValue = await evaluateExpressionAsync(expr.binary.left, options, locals, builtins);
|
|
319
323
|
|
|
@@ -351,9 +355,10 @@ export async function evaluateExpressionAsync(expr, options = null, locals = nul
|
|
|
351
355
|
}
|
|
352
356
|
// else if (binOp === '!=')
|
|
353
357
|
return leftValue !== rightValue;
|
|
358
|
+
}
|
|
354
359
|
|
|
355
360
|
// Unary expression
|
|
356
|
-
|
|
361
|
+
if (exprKey === 'unary') {
|
|
357
362
|
const unaryOp = expr.unary.op;
|
|
358
363
|
const value = await evaluateExpressionAsync(expr.unary.expr, options, locals, builtins);
|
|
359
364
|
if (unaryOp === '!') {
|