@zvenigora/ng-eval-core 0.14.1 → 0.15.0
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/CHANGELOG.md +108 -0
- package/README.md +43 -7
- package/fesm2022/zvenigora-ng-eval-core.mjs +715 -476
- package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
- package/package.json +1 -1
- package/types/zvenigora-ng-eval-core.d.ts +92 -31
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
2
|
import { InjectionToken, inject, makeEnvironmentProviders, provideEnvironmentInitializer, Injectable } from '@angular/core';
|
|
3
3
|
import { sha256 } from 'js-sha256';
|
|
4
|
-
import { tokTypes, Parser, parse as parse$1, defaultOptions, version } from 'acorn';
|
|
5
4
|
import * as walk from 'acorn-walk';
|
|
5
|
+
import { defaultOptions, tokTypes, Parser, parse as parse$1, version } from 'acorn';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Represents a registry that stores key-value pairs.
|
|
@@ -3268,28 +3268,57 @@ const importExpressionVisitor = (node, st) => {
|
|
|
3268
3268
|
throw new Error('Function call blocked for security reasons: import');
|
|
3269
3269
|
};
|
|
3270
3270
|
|
|
3271
|
-
//
|
|
3272
|
-
const
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
*
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3271
|
+
// The refusal names `await` and the rule (`docs/backlog-retired.md` A24).
|
|
3272
|
+
const AWAIT_POSITION_MESSAGE = 'Unsupported await position: an await is evaluated only as the result of the expression - the whole expression, an arrow function\'s expression body, or the last statement of a program or of an arrow function\'s block body.';
|
|
3273
|
+
const asAwait = (node) => {
|
|
3274
|
+
const inner = unwrapParentheses(node);
|
|
3275
|
+
return inner.type === 'AwaitExpression' ? inner : undefined;
|
|
3276
|
+
};
|
|
3277
|
+
const lastStatementAwait = (body) => {
|
|
3278
|
+
const last = body[body.length - 1];
|
|
3279
|
+
return last?.type === 'ExpressionStatement' ? asAwait(last.expression) : undefined;
|
|
3280
|
+
};
|
|
3281
|
+
/**
|
|
3282
|
+
* The one `await` a walk rooted at `root` accepts, the one whose value is the
|
|
3283
|
+
* walk's result: the root itself; or the expression of the last statement of
|
|
3284
|
+
* a `Program`, or of an arrow function's `BlockStatement` body when
|
|
3285
|
+
* `arrowBody` is set. Read off the root one level deep, so nothing is
|
|
3286
|
+
* traversed, and computed per walk, so nothing is stored: the same node is
|
|
3287
|
+
* accepted as one walk's root and refused as another's operand
|
|
3288
|
+
* (`docs/backlog-retired.md` A24).
|
|
3289
|
+
*
|
|
3290
|
+
* Under `preserveParens` the root and the last statement's expression are
|
|
3291
|
+
* unwrapped first, as the assignment and update visitors unwrap their
|
|
3292
|
+
* targets, so `(await p)` is accepted where `await p` is. Only those two
|
|
3293
|
+
* places are unwrapped: a parenthesised operand, `((await p)) * 10`, is still
|
|
3294
|
+
* an operand, and the walk refuses it.
|
|
3295
|
+
*
|
|
3296
|
+
* @param root The node the walk starts at.
|
|
3297
|
+
* @param arrowBody Whether the walk is an arrow function's body.
|
|
3298
|
+
* @returns The accepted `await`, or `undefined`.
|
|
3299
|
+
*/
|
|
3300
|
+
const acceptedAwait = (root, arrowBody) => {
|
|
3301
|
+
const rootAwait = asAwait(root);
|
|
3302
|
+
if (rootAwait) {
|
|
3303
|
+
return rootAwait;
|
|
3304
|
+
}
|
|
3305
|
+
if (!arrowBody && root.type === 'Program') {
|
|
3306
|
+
return lastStatementAwait(root.body);
|
|
3307
|
+
}
|
|
3308
|
+
if (arrowBody && root.type === 'BlockStatement') {
|
|
3309
|
+
return lastStatementAwait(root.body);
|
|
3289
3310
|
}
|
|
3290
|
-
return
|
|
3311
|
+
return undefined;
|
|
3291
3312
|
};
|
|
3292
|
-
const awaitVisitor = (node, st, callback) => {
|
|
3313
|
+
const awaitVisitor = (node, st, callback, accepted = false) => {
|
|
3314
|
+
// Above `beforeVisitor`, as the `var` kind check is: the refusal reads the
|
|
3315
|
+
// node's position and nothing else, so a refused `await` fires no hook and
|
|
3316
|
+
// leaves no trace entry. The default table's entry refuses every `await`;
|
|
3317
|
+
// a walk that has an accepted one passes `accepted` for it alone
|
|
3318
|
+
// (`functions/evaluate.ts`).
|
|
3319
|
+
if (!accepted) {
|
|
3320
|
+
throw new Error(AWAIT_POSITION_MESSAGE);
|
|
3321
|
+
}
|
|
3293
3322
|
beforeVisitor(node, st);
|
|
3294
3323
|
// Outside any `Promise` executor, and uncaught here, as in every other
|
|
3295
3324
|
// visitor: a synchronous throw from the operand propagates synchronously, and
|
|
@@ -3299,30 +3328,10 @@ const awaitVisitor = (node, st, callback) => {
|
|
|
3299
3328
|
// (`docs/backlog-retired.md` A1).
|
|
3300
3329
|
callback(node.argument, st);
|
|
3301
3330
|
const value = popVisitorResult(node, st);
|
|
3302
|
-
//
|
|
3303
|
-
|
|
3304
|
-
//
|
|
3305
|
-
|
|
3306
|
-
const timeout = (contextObj && typeof contextObj['__awaitTimeout'] === 'number'
|
|
3307
|
-
? contextObj['__awaitTimeout']
|
|
3308
|
-
: DEFAULT_AWAIT_TIMEOUT);
|
|
3309
|
-
// Race between the actual promise and timeout
|
|
3310
|
-
const promise = Promise.race([
|
|
3311
|
-
valuePromise,
|
|
3312
|
-
createTimeoutPromise(timeout)
|
|
3313
|
-
])
|
|
3314
|
-
.catch((error) => {
|
|
3315
|
-
// Enhance error information with context
|
|
3316
|
-
const enhancedError = error instanceof Error
|
|
3317
|
-
? error
|
|
3318
|
-
: new Error(`Await expression failed: ${String(error)}`);
|
|
3319
|
-
// Add source location information if available
|
|
3320
|
-
if (node.start !== undefined && node.end !== undefined) {
|
|
3321
|
-
enhancedError.message += ` at position ${node.start}-${node.end}`;
|
|
3322
|
-
}
|
|
3323
|
-
throw enhancedError;
|
|
3324
|
-
});
|
|
3325
|
-
pushVisitorResult(node, st, promise);
|
|
3331
|
+
// Passed through unchanged: nothing races it, wraps it, reads a timeout
|
|
3332
|
+
// for it or rewrites its rejection (`docs/backlog-retired.md` A25). A
|
|
3333
|
+
// promise that reaches the result is resolved there by `evaluateAsync`.
|
|
3334
|
+
pushVisitorResult(node, st, value);
|
|
3326
3335
|
afterVisitor(node, st);
|
|
3327
3336
|
};
|
|
3328
3337
|
|
|
@@ -3530,16 +3539,56 @@ const templateLiteralVisitor = (node, st, callback) => {
|
|
|
3530
3539
|
};
|
|
3531
3540
|
|
|
3532
3541
|
/**
|
|
3533
|
-
*
|
|
3534
|
-
*
|
|
3542
|
+
* Each template site's template object, by its `TemplateLiteral` node.
|
|
3543
|
+
*
|
|
3544
|
+
* A module-level weak collection keyed by an AST node and never cleared, as
|
|
3545
|
+
* `dangerous-values.ts`' `exemptNodes` is, and keyed by an object as
|
|
3546
|
+
* `property-lookup-cache.ts` is: an entry lives exactly as long as its node,
|
|
3547
|
+
* and nothing one node holds can be read for another. What it does not do,
|
|
3548
|
+
* beside that cache: it has no clear and no statistics, and is never
|
|
3549
|
+
* invalidated, since a node's strings do not change - for an AST its caller
|
|
3550
|
+
* leaves alone; one that rewrites a node's `quasis` after a first evaluation
|
|
3551
|
+
* keeps the old object, as `exemptNodes` keeps its marks. The object is shared
|
|
3552
|
+
* by every walk of the node, over any state and context, and through a
|
|
3553
|
+
* `ParserService`'s parse cache by every consumer of that service - in an
|
|
3554
|
+
* application, the root singleton - parsing the same text with the same
|
|
3555
|
+
* options. That is safe because the object is frozen and holds only the
|
|
3556
|
+
* template's own strings.
|
|
3557
|
+
*/
|
|
3558
|
+
const templateObjects = new WeakMap();
|
|
3559
|
+
/**
|
|
3560
|
+
* The site's template object, as JavaScript builds it: the cooked strings,
|
|
3561
|
+
* an invalid escape's cooked string `undefined`, with a non-enumerable `raw`
|
|
3562
|
+
* of the uncooked strings, both frozen, and the same object every time the
|
|
3563
|
+
* site runs (`docs/backlog-retired.md` A38). Up to 0.14.1 it was a plain array
|
|
3564
|
+
* of the cooked strings, built on every evaluation, with no `raw`.
|
|
3565
|
+
*
|
|
3566
|
+
* Not added to `st.createdObjects`: the walk did not create it for the
|
|
3567
|
+
* expression to hold, as `EvalMemberWrite.createdByEvaluation` defines
|
|
3568
|
+
* created, so a write policy is asked about it as about any other object.
|
|
3569
|
+
*/
|
|
3570
|
+
const templateObject = (quasi) => {
|
|
3571
|
+
let strings = templateObjects.get(quasi);
|
|
3572
|
+
if (!strings) {
|
|
3573
|
+
const cooked = quasi.quasis.map((element) => element.value.cooked ?? undefined);
|
|
3574
|
+
const raw = Object.freeze(quasi.quasis.map((element) => element.value.raw));
|
|
3575
|
+
// Not writable, enumerable or configurable, as JavaScript defines it.
|
|
3576
|
+
Object.defineProperty(cooked, 'raw', { value: raw });
|
|
3577
|
+
strings = Object.freeze(cooked);
|
|
3578
|
+
templateObjects.set(quasi, strings);
|
|
3579
|
+
}
|
|
3580
|
+
return strings;
|
|
3581
|
+
};
|
|
3582
|
+
/**
|
|
3583
|
+
* The tag's arguments: the template object, then the value of each of its
|
|
3584
|
+
* expressions, evaluated in order.
|
|
3535
3585
|
*/
|
|
3536
3586
|
const templateArguments = (node, st, callback) => {
|
|
3537
3587
|
const values = node.quasi.expressions.map((expression) => {
|
|
3538
3588
|
callback(expression, st);
|
|
3539
3589
|
return popVisitorResult(node, st);
|
|
3540
3590
|
});
|
|
3541
|
-
|
|
3542
|
-
return [strings, ...values];
|
|
3591
|
+
return [templateObject(node.quasi), ...values];
|
|
3543
3592
|
};
|
|
3544
3593
|
/**
|
|
3545
3594
|
* Calls the tag as a call calls its callee (`call-expression.ts`): past the
|
|
@@ -3963,6 +4012,53 @@ const evaluateRestElement = (pattern, st, callback, args) => {
|
|
|
3963
4012
|
// return object;
|
|
3964
4013
|
// }
|
|
3965
4014
|
|
|
4015
|
+
/**
|
|
4016
|
+
* The statement types `dispatchStatement` walks, and the one place they are
|
|
4017
|
+
* listed.
|
|
4018
|
+
*
|
|
4019
|
+
* `VariableDeclaration` covers `var` as well, and cannot not: this table reads
|
|
4020
|
+
* the node *type*, and `var x = 1` is a `VariableDeclaration` exactly as
|
|
4021
|
+
* `let x = 1` is. Listing it here is therefore what stops `var` reaching the
|
|
4022
|
+
* dispatcher's throw; its rejection is `declarationKindRefusal`, below, which
|
|
4023
|
+
* the visitor calls because it can read `kind`.
|
|
4024
|
+
*
|
|
4025
|
+
* `ForStatement` is the classic three-part form *only*. `ForOfStatement`,
|
|
4026
|
+
* `ForInStatement`, `WhileStatement` and `DoWhileStatement` are distinct node
|
|
4027
|
+
* types and stay out of the table, so they are refused unchanged - unlike
|
|
4028
|
+
* `var`, which shares a type with `let` and is refused one level down.
|
|
4029
|
+
*/
|
|
4030
|
+
const SUPPORTED_STATEMENT_TYPES = new Set([
|
|
4031
|
+
'ExpressionStatement',
|
|
4032
|
+
'EmptyStatement',
|
|
4033
|
+
'BlockStatement',
|
|
4034
|
+
'VariableDeclaration',
|
|
4035
|
+
'IfStatement',
|
|
4036
|
+
'ForStatement',
|
|
4037
|
+
]);
|
|
4038
|
+
/** The `VariableDeclaration` kinds the declaration visitor binds. */
|
|
4039
|
+
const SUPPORTED_DECLARATION_KINDS = new Set(['let', 'const']);
|
|
4040
|
+
/**
|
|
4041
|
+
* The message evaluation throws for a statement whose type it does not walk,
|
|
4042
|
+
* or `undefined` for one it does.
|
|
4043
|
+
*
|
|
4044
|
+
* `dispatchStatement` throws it, and `findUnsupportedStatement` reports it, so
|
|
4045
|
+
* the evaluator and the export cannot disagree about a type (`docs/backlog.md`
|
|
4046
|
+
* D13).
|
|
4047
|
+
*/
|
|
4048
|
+
const statementTypeRefusal = (statement) => SUPPORTED_STATEMENT_TYPES.has(statement.type)
|
|
4049
|
+
? undefined
|
|
4050
|
+
: `Unsupported statement type: ${statement.type}`;
|
|
4051
|
+
/**
|
|
4052
|
+
* The message evaluation throws for a declaration whose kind it does not bind,
|
|
4053
|
+
* `var` among them, or `undefined` for `let` and `const`.
|
|
4054
|
+
*
|
|
4055
|
+
* `variableDeclarationVisitor` throws it above its `beforeVisitor`, and
|
|
4056
|
+
* `findUnsupportedStatement` reports it.
|
|
4057
|
+
*/
|
|
4058
|
+
const declarationKindRefusal = (declaration) => SUPPORTED_DECLARATION_KINDS.has(declaration.kind)
|
|
4059
|
+
? undefined
|
|
4060
|
+
: `Unsupported variable declaration kind: ${declaration.kind}`;
|
|
4061
|
+
|
|
3966
4062
|
/**
|
|
3967
4063
|
* The scope a declaration binds into: the innermost one on the stack.
|
|
3968
4064
|
*
|
|
@@ -4046,7 +4142,7 @@ const bindDeclarator = (declarator, value, kind, scope, st, callback) => {
|
|
|
4046
4142
|
* **`var` is rejected here, not by the statement dispatcher.** The dispatcher
|
|
4047
4143
|
* switches on node *type*, and `var x = 1` is a `VariableDeclaration` exactly as
|
|
4048
4144
|
* `let x = 1` is - so registering this visitor is what stopped `var` reaching the
|
|
4049
|
-
*
|
|
4145
|
+
* dispatcher's throw. Function-scoped hoisting is a second scoping model beside
|
|
4050
4146
|
* block scoping and is out of scope for this phase, so the rejection had to move
|
|
4051
4147
|
* rather than disappear.
|
|
4052
4148
|
*
|
|
@@ -4062,8 +4158,11 @@ const variableDeclarationVisitor = (node, st, callback) => {
|
|
|
4062
4158
|
// from opening a frame it never closes. The others are covered the way every
|
|
4063
4159
|
// throwing visitor in this library is: `evaluate`'s `catch` unwinds the open
|
|
4064
4160
|
// nodes to its walk base.
|
|
4065
|
-
|
|
4066
|
-
|
|
4161
|
+
// The kinds are `statement-support.ts`' table, which
|
|
4162
|
+
// `findUnsupportedStatement` reads too.
|
|
4163
|
+
const refusal = declarationKindRefusal(node);
|
|
4164
|
+
if (refusal !== undefined) {
|
|
4165
|
+
throw new Error(refusal);
|
|
4067
4166
|
}
|
|
4068
4167
|
beforeVisitor(node, st);
|
|
4069
4168
|
const scope = bindingScope(st);
|
|
@@ -4345,238 +4444,13 @@ const updateExpressionVisitor = (node, st, callback) => {
|
|
|
4345
4444
|
afterVisitor(node, st);
|
|
4346
4445
|
};
|
|
4347
4446
|
|
|
4348
|
-
/**
|
|
4349
|
-
* @description
|
|
4350
|
-
* The colon-joined name grammar of `docs/colon-identifiers/phase-9-plan.md`
|
|
4351
|
-
* § 3.4, as an acorn plugin (§ 3.5). `parse` uses the parser built here when
|
|
4352
|
-
* `colonIdentifiers` is set, and acorn's own `parse` otherwise. No source text
|
|
4353
|
-
* is rewritten, so every position acorn reports is a position in what the
|
|
4354
|
-
* author wrote. Not re-exported from any barrel.
|
|
4355
|
-
*
|
|
4356
|
-
* Of acorn's documented API this module uses `Parser.extend`, `tokTypes` and
|
|
4357
|
-
* the `input` member. Everything else it touches is an acorn internal,
|
|
4358
|
-
* undeclared in acorn's `.d.ts`, and this is the complete list — what § 8 q3
|
|
4359
|
-
* checks at the `acorn` peer floor:
|
|
4360
|
-
*
|
|
4361
|
-
* - members read: `pos`, `start`, `type`, `value`, `keywords`;
|
|
4362
|
-
* - members written: `pos`, `containsEsc`;
|
|
4363
|
-
* - methods overridden, each calling acorn's own through `super`: `readWord`,
|
|
4364
|
-
* `finishToken`, `parsePropertyName`, `parseStatement`, `unexpected`;
|
|
4365
|
-
* - methods called: `finishToken`, `raise`;
|
|
4366
|
-
* - what the overrides assume of acorn's tokenizer: `readToken` is the only
|
|
4367
|
-
* caller of `readWord`, with `pos` at the token's first character; every
|
|
4368
|
-
* token is finished through `finishToken`, in source order, before the next
|
|
4369
|
-
* is read — except that a `/` read as `slash` is finished again as
|
|
4370
|
-
* `regexp`, and neither type is counted here; `?.`, `??` and `??=` are never
|
|
4371
|
-
* `question` tokens; the `}` that closes a `${` is finished as `braceR`; the
|
|
4372
|
-
* constructor reads no token;
|
|
4373
|
-
* - what they assume of acorn's parser: every key of an object literal, an
|
|
4374
|
-
* object pattern or a class body is read through `parsePropertyName`, with
|
|
4375
|
-
* the key as the current token; every statement through `parseStatement`,
|
|
4376
|
-
* with its first token as the current token; a colon the grammar cannot
|
|
4377
|
-
* place reaches `unexpected`, from `expect` or `semicolon`, with `type` and
|
|
4378
|
-
* `start` still at that colon.
|
|
4379
|
-
*
|
|
4380
|
-
* The token types it compares by identity, from `tokTypes`: `name`, `colon`,
|
|
4381
|
-
* `question`, `comma`, `semi`, `parenL`, `parenR`, `bracketL`, `bracketR`,
|
|
4382
|
-
* `braceL`, `braceR`, `dollarBraceL`.
|
|
4383
|
-
*/
|
|
4384
|
-
const COLON = 58;
|
|
4385
|
-
/** `[A-Za-z_$]`: the first character of a name that may join. */
|
|
4386
|
-
const isFirstChar = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 || code === 36;
|
|
4387
|
-
/** `[A-Za-z0-9_$]`: any later character, and a segment's first. */
|
|
4388
|
-
const isSegmentChar = (code) => isFirstChar(code) || (code >= 48 && code <= 57);
|
|
4389
|
-
/** Whether a `:segment` starts at `pos`: a colon with a segment character after it. */
|
|
4390
|
-
const joinsAt = (input, pos) => input.charCodeAt(pos) === COLON && isSegmentChar(input.charCodeAt(pos + 1));
|
|
4391
|
-
const colonIdentifiers = (Base) => {
|
|
4392
|
-
class ColonParser extends Base {
|
|
4393
|
-
constructor() {
|
|
4394
|
-
super(...arguments);
|
|
4395
|
-
/** Never empty: the top level is never closed. */
|
|
4396
|
-
this.levels = [{ pending: 0 }];
|
|
4397
|
-
/**
|
|
4398
|
-
* The level the current token was read at: for a `(`, `[`, `{` or `${`,
|
|
4399
|
-
* the one around the level it opened.
|
|
4400
|
-
*/
|
|
4401
|
-
this.tokenLevel = this.levels[0];
|
|
4402
|
-
}
|
|
4403
|
-
get level() {
|
|
4404
|
-
return this.levels[this.levels.length - 1];
|
|
4405
|
-
}
|
|
4406
|
-
/**
|
|
4407
|
-
* Reads a colon-joined name as one `name` token when one starts at `pos`
|
|
4408
|
-
* and no `?` at this level is waiting for its `:` (§ 3.4, 1). In every
|
|
4409
|
-
* other case acorn's own `readWord`, so a word that does not join is read
|
|
4410
|
-
* exactly as acorn reads it.
|
|
4411
|
-
*/
|
|
4412
|
-
readWord() {
|
|
4413
|
-
const end = this.colonNameEnd();
|
|
4414
|
-
if (end === undefined) {
|
|
4415
|
-
return super.readWord();
|
|
4416
|
-
}
|
|
4417
|
-
const name = this.input.slice(this.pos, end);
|
|
4418
|
-
if (this.level.pending > 0) {
|
|
4419
|
-
this.level.suppressed = name;
|
|
4420
|
-
return super.readWord();
|
|
4421
|
-
}
|
|
4422
|
-
this.pos = end;
|
|
4423
|
-
this.containsEsc = false;
|
|
4424
|
-
this.finishToken(tokTypes.name, name);
|
|
4425
|
-
}
|
|
4426
|
-
/**
|
|
4427
|
-
* Where the colon name that starts at `pos` ends — its longest match — or
|
|
4428
|
-
* `undefined` when none does: the first segment is not ASCII, is a keyword
|
|
4429
|
-
* of the parse's `ecmaVersion`, or has no `:segment` directly after it.
|
|
4430
|
-
*/
|
|
4431
|
-
colonNameEnd() {
|
|
4432
|
-
const input = this.input;
|
|
4433
|
-
let end = this.pos;
|
|
4434
|
-
if (!isFirstChar(input.charCodeAt(end))) {
|
|
4435
|
-
return undefined;
|
|
4436
|
-
}
|
|
4437
|
-
do {
|
|
4438
|
-
end++;
|
|
4439
|
-
} while (isSegmentChar(input.charCodeAt(end)));
|
|
4440
|
-
if (!joinsAt(input, end) || this.keywords.test(input.slice(this.pos, end))) {
|
|
4441
|
-
return undefined;
|
|
4442
|
-
}
|
|
4443
|
-
do {
|
|
4444
|
-
end += 2;
|
|
4445
|
-
while (isSegmentChar(input.charCodeAt(end))) {
|
|
4446
|
-
end++;
|
|
4447
|
-
}
|
|
4448
|
-
} while (joinsAt(input, end));
|
|
4449
|
-
return end;
|
|
4450
|
-
}
|
|
4451
|
-
/**
|
|
4452
|
-
* Keeps the levels after acorn has finished the token, recording the one
|
|
4453
|
-
* the token was read at: `(`, `[`, `{` and `${` open a level and `)`, `]`
|
|
4454
|
-
* and `}` close it; a `?` is counted at its level and a `:` there
|
|
4455
|
-
* uncounts it. `?.` and `??` are other token types, and count for nothing.
|
|
4456
|
-
*/
|
|
4457
|
-
finishToken(type, value) {
|
|
4458
|
-
super.finishToken(type, value);
|
|
4459
|
-
const level = this.level;
|
|
4460
|
-
this.tokenLevel = level;
|
|
4461
|
-
switch (type) {
|
|
4462
|
-
case tokTypes.parenL:
|
|
4463
|
-
case tokTypes.bracketL:
|
|
4464
|
-
case tokTypes.braceL:
|
|
4465
|
-
case tokTypes.dollarBraceL:
|
|
4466
|
-
this.levels.push({ pending: 0 });
|
|
4467
|
-
break;
|
|
4468
|
-
case tokTypes.parenR:
|
|
4469
|
-
case tokTypes.bracketR:
|
|
4470
|
-
case tokTypes.braceR:
|
|
4471
|
-
if (this.levels.length > 1) {
|
|
4472
|
-
this.levels.pop();
|
|
4473
|
-
}
|
|
4474
|
-
break;
|
|
4475
|
-
case tokTypes.question:
|
|
4476
|
-
level.pending++;
|
|
4477
|
-
break;
|
|
4478
|
-
case tokTypes.colon:
|
|
4479
|
-
if (level.pending > 0) {
|
|
4480
|
-
level.pending--;
|
|
4481
|
-
}
|
|
4482
|
-
break;
|
|
4483
|
-
case tokTypes.comma:
|
|
4484
|
-
case tokTypes.semi:
|
|
4485
|
-
level.suppressed = undefined;
|
|
4486
|
-
break;
|
|
4487
|
-
}
|
|
4488
|
-
}
|
|
4489
|
-
/**
|
|
4490
|
-
* Refuses a colon name as an object key, in a literal or a pattern alike
|
|
4491
|
-
* (§ 3.4, 2): `{Q1:1}` is `{Q1: 1}` to JavaScript and a shorthand to the
|
|
4492
|
-
* grammar, and keeping either would answer some author wrongly. Only a
|
|
4493
|
-
* joined token is a `name` holding a colon.
|
|
4494
|
-
*/
|
|
4495
|
-
parsePropertyName(prop) {
|
|
4496
|
-
const name = this.value;
|
|
4497
|
-
if (this.type === tokTypes.name && typeof name === 'string' && name.includes(':')) {
|
|
4498
|
-
const first = name.slice(0, name.indexOf(':'));
|
|
4499
|
-
this.raise(this.start, `Colon-joined name '${name}' cannot be an object key: ` +
|
|
4500
|
-
`write '${first}: ...' with a space for a property, or quote the key`);
|
|
4501
|
-
}
|
|
4502
|
-
return super.parsePropertyName(prop);
|
|
4503
|
-
}
|
|
4504
|
-
/**
|
|
4505
|
-
* Forgets the name not joined at the level a statement starts at: the
|
|
4506
|
-
* statement boundary ended the conditional whose consequent lost it. A
|
|
4507
|
-
* semicolon acorn inserts at a line break is such a boundary, and no `;`
|
|
4508
|
-
* token marks it.
|
|
4509
|
-
*/
|
|
4510
|
-
parseStatement(...args) {
|
|
4511
|
-
this.tokenLevel.suppressed = undefined;
|
|
4512
|
-
return super.parseStatement(...args);
|
|
4513
|
-
}
|
|
4514
|
-
/**
|
|
4515
|
-
* acorn's own error, with the hint of § 3.4, 1 when the unexpected token
|
|
4516
|
-
* is a `:` at a level that did not join a name since its last `,`, `;` or
|
|
4517
|
-
* statement start: the colon the name lost to the conditional. Any other
|
|
4518
|
-
* error is left as acorn words it.
|
|
4519
|
-
*/
|
|
4520
|
-
unexpected(pos) {
|
|
4521
|
-
const suppressed = this.tokenLevel.suppressed;
|
|
4522
|
-
if (suppressed !== undefined && this.type === tokTypes.colon &&
|
|
4523
|
-
(pos === undefined || pos === this.start)) {
|
|
4524
|
-
return this.raise(this.start, 'Unexpected token: a colon-joined name in the ' +
|
|
4525
|
-
`consequent of a conditional needs parentheses: (${suppressed})`);
|
|
4526
|
-
}
|
|
4527
|
-
return super.unexpected(pos);
|
|
4528
|
-
}
|
|
4529
|
-
}
|
|
4530
|
-
return ColonParser;
|
|
4531
|
-
};
|
|
4532
|
-
/**
|
|
4533
|
-
* acorn's `Parser` with the grammar, built once, at module load. `extend`
|
|
4534
|
-
* returns a subclass, so acorn's own `Parser`, which `acorn.parse` uses, is
|
|
4535
|
-
* left as it was.
|
|
4536
|
-
*/
|
|
4537
|
-
const ColonIdentifiersParser = Parser.extend(colonIdentifiers);
|
|
4538
|
-
|
|
4539
|
-
/**
|
|
4540
|
-
* @description
|
|
4541
|
-
* Parses expression and returns ES6/ES2020 AST.
|
|
4542
|
-
* With `colonIdentifiers` set the parser is the colon-joined name grammar's;
|
|
4543
|
-
* otherwise it is acorn's own `parse`.
|
|
4544
|
-
* @param expr expression to parse
|
|
4545
|
-
* @param options parser options
|
|
4546
|
-
*/
|
|
4547
|
-
const parse = (expr, options) => {
|
|
4548
|
-
options = { ecmaVersion: 2020, ...options };
|
|
4549
|
-
const acornOptions = { ecmaVersion: 2020, ...options };
|
|
4550
|
-
const program = options.colonIdentifiers
|
|
4551
|
-
? ColonIdentifiersParser.parse(expr, acornOptions)
|
|
4552
|
-
: parse$1(expr, acornOptions);
|
|
4553
|
-
if (!options.extractExpressions) {
|
|
4554
|
-
return program;
|
|
4555
|
-
}
|
|
4556
|
-
return extractExpression(program);
|
|
4557
|
-
};
|
|
4558
|
-
/**
|
|
4559
|
-
* @description
|
|
4560
|
-
* Walks through the AST and extracts first expression.
|
|
4561
|
-
* @param expr expression to parse
|
|
4562
|
-
*/
|
|
4563
|
-
const extractExpression = (program) => {
|
|
4564
|
-
if (program.body.length === 1) {
|
|
4565
|
-
const expression = program.body[0];
|
|
4566
|
-
if (expression.type === 'ExpressionStatement') {
|
|
4567
|
-
return expression.expression;
|
|
4568
|
-
}
|
|
4569
|
-
}
|
|
4570
|
-
return undefined;
|
|
4571
|
-
};
|
|
4572
|
-
|
|
4573
4447
|
/**
|
|
4574
4448
|
* Applies § 3.1's rule 4: the empty-completion sentinel never leaves an
|
|
4575
4449
|
* evaluation's return value.
|
|
4576
4450
|
*
|
|
4577
4451
|
* It is converted here, at the walk boundary, rather than in the `Program`
|
|
4578
|
-
* visitor. `arrow-function-expression.ts`
|
|
4579
|
-
*
|
|
4452
|
+
* visitor. `arrow-function-expression.ts` walks a `BlockStatement` for a
|
|
4453
|
+
* block-bodied arrow, so a conversion sited in `Program`
|
|
4580
4454
|
* would let `x => { }` hand the sentinel straight to a consumer. Converting
|
|
4581
4455
|
* where the walk ends covers both entry points by construction.
|
|
4582
4456
|
*/
|
|
@@ -4615,7 +4489,7 @@ const completionValue = (value) => value === EMPTY_COMPLETION ? undefined : valu
|
|
|
4615
4489
|
* mutation into a throw. `getDefaultVisitors()` still returns a **fresh,
|
|
4616
4490
|
* mutable** object to anyone who calls it, so no caller loses anything.
|
|
4617
4491
|
* - **Lazily** because a module-level `const` deadlocks the flattened bundle:
|
|
4618
|
-
* `arrow-function-expression.ts` imports `
|
|
4492
|
+
* `arrow-function-expression.ts` imports `walkWithoutResult`, so this module is
|
|
4619
4493
|
* initialised first and `getDefaultVisitors()` at module scope reads that
|
|
4620
4494
|
* visitor before its binding exists. Measured, not guessed - the built
|
|
4621
4495
|
* package threw `Cannot access 'arrowFunctionExpressionVisitor' before
|
|
@@ -4624,66 +4498,121 @@ const completionValue = (value) => value === EMPTY_COMPLETION ? undefined : valu
|
|
|
4624
4498
|
let defaultVisitors;
|
|
4625
4499
|
const mergedVisitors = () => (defaultVisitors ??= Object.freeze(walk.make(getDefaultVisitors())));
|
|
4626
4500
|
/**
|
|
4627
|
-
*
|
|
4628
|
-
*
|
|
4629
|
-
*
|
|
4501
|
+
* The table a walk rooted at `root` dispatches through: the shared one, whose
|
|
4502
|
+
* `AwaitExpression` entry refuses every `await`, or - when the root has an
|
|
4503
|
+
* accepted `await` (`acceptedAwait`) - a fresh object over it whose own entry
|
|
4504
|
+
* accepts that node alone (`docs/backlog-retired.md` A24).
|
|
4505
|
+
*
|
|
4506
|
+
* The answer travels with the walk, not on the state or the node: the arrow
|
|
4507
|
+
* function's nested walk builds its own from its own root, so an enclosing
|
|
4508
|
+
* walk's accepted `await` is never in scope where it is not one. The entry is
|
|
4509
|
+
* defined, not assigned: `walk.make` would assign a `funcs` key onto an object
|
|
4510
|
+
* over the frozen table, which throws in strict mode. A walk with no accepted
|
|
4511
|
+
* `await` takes the shared table and pays nothing; one that has one builds a
|
|
4512
|
+
* single object, uncached.
|
|
4513
|
+
*/
|
|
4514
|
+
const visitorsFor = (root, arrowBody) => {
|
|
4515
|
+
const accepted = acceptedAwait(root, arrowBody);
|
|
4516
|
+
return accepted
|
|
4517
|
+
? Object.create(mergedVisitors(), {
|
|
4518
|
+
AwaitExpression: {
|
|
4519
|
+
value: (n, s, c) => awaitVisitor(n, s, c, n === accepted),
|
|
4520
|
+
},
|
|
4521
|
+
})
|
|
4522
|
+
: mergedVisitors();
|
|
4523
|
+
};
|
|
4524
|
+
/**
|
|
4525
|
+
* Walks the node on the state and returns its completion value, recording no
|
|
4526
|
+
* outcome: everything {@link evaluate} does except `state.result`'s `start`,
|
|
4527
|
+
* `stop`, `setSuccess` and `setFailure`.
|
|
4528
|
+
*
|
|
4529
|
+
* **The arrow-function body's walk** (`arrow-function-expression.ts`). That
|
|
4530
|
+
* body runs on the walk's own state, during the walk, after it, many times or
|
|
4531
|
+
* never, and up to 0.14.1 it ran through `evaluate` - so every call of an arrow
|
|
4532
|
+
* wrote its outcome, value and start time over the enclosing walk's result, and
|
|
4533
|
+
* a closure called after the walk wrote over the finished one
|
|
4534
|
+
* (`docs/backlog-retired.md` A27). The result now describes the entry-point walk alone.
|
|
4535
|
+
*
|
|
4536
|
+
* Not exported from the package: `functions/public-api.ts` does not list it.
|
|
4537
|
+
*
|
|
4538
|
+
* @param node The node to walk.
|
|
4630
4539
|
* @param state The evaluation state.
|
|
4631
|
-
* @
|
|
4632
|
-
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
4637
|
-
|
|
4638
|
-
|
|
4639
|
-
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
state
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4540
|
+
* @param arrowBody Whether the node is an arrow function's body, which
|
|
4541
|
+
* decides the `await` it accepts (`visitorsFor`).
|
|
4542
|
+
* @returns The node's completion value.
|
|
4543
|
+
*/
|
|
4544
|
+
const walkWithoutResult = (node, state, arrowBody = false) => {
|
|
4545
|
+
// The mark is recorded on the state as well as held here, so that
|
|
4546
|
+
// `EvalHooks.exit` - called from a visitor, which has no route to this
|
|
4547
|
+
// local - can tell a node open in this walk from one open only in an
|
|
4548
|
+
// enclosing walk. Pushed under the same guard the mark is read under, and
|
|
4549
|
+
// the local `hooked` keeps the pair symmetric if a hook is registered
|
|
4550
|
+
// mid-walk.
|
|
4551
|
+
const hooked = state.hasHooks;
|
|
4552
|
+
const mark = hooked ? state.hooks.pushWalkBase(state) : 0;
|
|
4553
|
+
// Unguarded, unlike the walk base above: the iteration budget refills on
|
|
4554
|
+
// the outermost entry whether or not hooks are registered. See
|
|
4555
|
+
// `EvalState.walkDepth` on why these are two fields and not one.
|
|
4556
|
+
state.enterWalk();
|
|
4557
|
+
try {
|
|
4558
|
+
walk.recursive(node, state, {}, visitorsFor(node, arrowBody));
|
|
4559
|
+
return completionValue(popVisitorResult(node, state));
|
|
4560
|
+
}
|
|
4561
|
+
catch (error) {
|
|
4562
|
+
// A visitor that throws skips its afterVisitor, so close whatever this
|
|
4563
|
+
// walk left open. Unwinding to the mark rather than to the bottom is what
|
|
4564
|
+
// stops a nested walk - the arrow-function body runs on this same
|
|
4565
|
+
// state - from draining the enclosing walk's open nodes.
|
|
4566
|
+
// Reads `state.hasHooks` rather than the `hooked` local captured above,
|
|
4567
|
+
// and the difference is deliberate but narrow: if a hook was registered
|
|
4568
|
+
// *during* this walk then `hooked` is false, no base was pushed, `mark`
|
|
4569
|
+
// is 0, and this unwinds the whole stack - including an enclosing walk's
|
|
4570
|
+
// frames, which is the boundary crossing the walk base exists to stop.
|
|
4571
|
+
// Left as it was rather than tightened here, because narrowing it would
|
|
4572
|
+
// change what a mid-walk registration observes on an already-published
|
|
4573
|
+
// path, which is not this step's to decide.
|
|
4574
|
+
if (state.hasHooks) {
|
|
4575
|
+
state.hooks.unwindTo(mark, error, state);
|
|
4576
|
+
}
|
|
4577
|
+
throw error;
|
|
4578
|
+
}
|
|
4579
|
+
finally {
|
|
4580
|
+
// Both in a `finally`: a walk that threw still has to restore the
|
|
4581
|
+
// enclosing walk's bound and depth, or every later evaluation on this
|
|
4582
|
+
// state carries this one's nesting.
|
|
4583
|
+
state.exitWalk();
|
|
4584
|
+
if (hooked) {
|
|
4585
|
+
state.hooks.popWalkBase(state);
|
|
4586
|
+
}
|
|
4587
|
+
}
|
|
4588
|
+
};
|
|
4589
|
+
/**
|
|
4590
|
+
* Evaluates the given node and returns the result.
|
|
4591
|
+
*
|
|
4592
|
+
* Records the walk's outcome on `state.result`. An arrow function's body,
|
|
4593
|
+
* walked on the same state whenever the function is called, records nothing
|
|
4594
|
+
* there.
|
|
4595
|
+
*
|
|
4596
|
+
* @param node The node to evaluate.
|
|
4597
|
+
* @param state The evaluation state.
|
|
4598
|
+
* @returns The result of the evaluation.
|
|
4599
|
+
*/
|
|
4600
|
+
const evaluate = (node, state) => {
|
|
4601
|
+
// The `if (node)` guard comes first on purpose: callers reach the early
|
|
4602
|
+
// return below with a bare state that has no result or hooks to read.
|
|
4603
|
+
if (node) {
|
|
4604
|
+
state.result.start();
|
|
4605
|
+
try {
|
|
4606
|
+
const value = walkWithoutResult(node, state);
|
|
4607
|
+
state.result.stop();
|
|
4655
4608
|
state.result.setSuccess(value);
|
|
4656
4609
|
return value;
|
|
4657
4610
|
}
|
|
4658
4611
|
catch (error) {
|
|
4659
4612
|
state.result.stop();
|
|
4660
|
-
// A visitor that throws skips its afterVisitor, so close whatever this
|
|
4661
|
-
// walk left open. Unwinding to the mark rather than to the bottom is what
|
|
4662
|
-
// stops a nested evaluate - the arrow-function body runs on this same
|
|
4663
|
-
// state - from draining the enclosing walk's open nodes.
|
|
4664
|
-
// Reads `state.hasHooks` rather than the `hooked` local captured above,
|
|
4665
|
-
// and the difference is deliberate but narrow: if a hook was registered
|
|
4666
|
-
// *during* this walk then `hooked` is false, no base was pushed, `mark`
|
|
4667
|
-
// is 0, and this unwinds the whole stack - including an enclosing walk's
|
|
4668
|
-
// frames, which is the boundary crossing the walk base exists to stop.
|
|
4669
|
-
// Left as it was rather than tightened here, because narrowing it would
|
|
4670
|
-
// change what a mid-walk registration observes on an already-published
|
|
4671
|
-
// path, which is not this step's to decide.
|
|
4672
|
-
if (state.hasHooks) {
|
|
4673
|
-
state.hooks.unwindTo(mark, error, state);
|
|
4674
|
-
}
|
|
4675
4613
|
state.result.setFailure(error);
|
|
4676
4614
|
throw error;
|
|
4677
4615
|
}
|
|
4678
|
-
finally {
|
|
4679
|
-
// Both in a `finally`: a walk that threw still has to restore the
|
|
4680
|
-
// enclosing walk's bound and depth, or every later evaluation on this
|
|
4681
|
-
// state carries this one's nesting.
|
|
4682
|
-
state.exitWalk();
|
|
4683
|
-
if (hooked) {
|
|
4684
|
-
state.hooks.popWalkBase(state);
|
|
4685
|
-
}
|
|
4686
|
-
}
|
|
4687
4616
|
}
|
|
4688
4617
|
return undefined;
|
|
4689
4618
|
};
|
|
@@ -4751,7 +4680,8 @@ const evaluateAsync = async (ast, state) => {
|
|
|
4751
4680
|
}
|
|
4752
4681
|
state.result.start();
|
|
4753
4682
|
// Captured after the `!ast` early return above, for the same reason the sync
|
|
4754
|
-
// entry point captures
|
|
4683
|
+
// entry point captures only once it has a node, in `walkWithoutResult`. Do
|
|
4684
|
+
// not hoist.
|
|
4755
4685
|
const hooked = state.hasHooks;
|
|
4756
4686
|
const mark = hooked ? state.hooks.pushWalkBase(state) : 0;
|
|
4757
4687
|
state.enterWalk();
|
|
@@ -4764,7 +4694,7 @@ const evaluateAsync = async (ast, state) => {
|
|
|
4764
4694
|
// evaluation that interleaved on this state looking like a nested walk -
|
|
4765
4695
|
// and § 3.4's budget refills only on the outermost entry.
|
|
4766
4696
|
try {
|
|
4767
|
-
walk.recursive(ast, state, {},
|
|
4697
|
+
walk.recursive(ast, state, {}, visitorsFor(ast, false));
|
|
4768
4698
|
value = completionValue(popVisitorResult(ast, state));
|
|
4769
4699
|
}
|
|
4770
4700
|
finally {
|
|
@@ -4789,7 +4719,6 @@ const evaluateAsync = async (ast, state) => {
|
|
|
4789
4719
|
return value;
|
|
4790
4720
|
}
|
|
4791
4721
|
catch (error) {
|
|
4792
|
-
state.result.stop();
|
|
4793
4722
|
// The walk here is synchronous too, so it strands open nodes on a throw
|
|
4794
4723
|
// exactly as the sync entry point does. This catch also receives the
|
|
4795
4724
|
// rethrow from the awaitAllPromises handler above; in that case the walk
|
|
@@ -4798,70 +4727,14 @@ const evaluateAsync = async (ast, state) => {
|
|
|
4798
4727
|
if (state.hasHooks) {
|
|
4799
4728
|
state.hooks.unwindTo(mark, error, state);
|
|
4800
4729
|
}
|
|
4730
|
+
// After the unwind, as on the sync path, so a hook that fires during the
|
|
4731
|
+
// cleanup reads `duration` `undefined` there too: the walk has not ended.
|
|
4732
|
+
state.result.stop();
|
|
4801
4733
|
state.result.setFailure(error);
|
|
4802
4734
|
throw error;
|
|
4803
4735
|
}
|
|
4804
4736
|
};
|
|
4805
4737
|
|
|
4806
|
-
/**
|
|
4807
|
-
* Compiles the given node and returns a function that can be used to evaluate it.
|
|
4808
|
-
*
|
|
4809
|
-
* @param node The node to compile.
|
|
4810
|
-
* @returns A function that takes an `EvalState` and returns the result of evaluating the node.
|
|
4811
|
-
*/
|
|
4812
|
-
const compile = (node) => {
|
|
4813
|
-
return evaluate.bind(null, node);
|
|
4814
|
-
};
|
|
4815
|
-
/**
|
|
4816
|
-
* Compiles the given node asynchronously.
|
|
4817
|
-
*
|
|
4818
|
-
* @param node - The node to compile.
|
|
4819
|
-
* @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
|
|
4820
|
-
*/
|
|
4821
|
-
const compileAsync = (node) => {
|
|
4822
|
-
return evaluateAsync.bind(null, node);
|
|
4823
|
-
};
|
|
4824
|
-
/**
|
|
4825
|
-
* Calls the provided function with the given state and returns the result.
|
|
4826
|
-
*
|
|
4827
|
-
* @param fn The function to be called.
|
|
4828
|
-
* @param state The state to be passed to the function.
|
|
4829
|
-
* @returns The result of calling the function.
|
|
4830
|
-
*/
|
|
4831
|
-
const call = (fn, state) => {
|
|
4832
|
-
const value = fn(state);
|
|
4833
|
-
return value;
|
|
4834
|
-
};
|
|
4835
|
-
/**
|
|
4836
|
-
* Calls an asynchronous function with the given state and returns a promise that resolves to the result.
|
|
4837
|
-
* @param fn The asynchronous function to call.
|
|
4838
|
-
* @param state The state to pass to the function.
|
|
4839
|
-
* @returns A promise that resolves to the result of the function.
|
|
4840
|
-
*/
|
|
4841
|
-
const callAsync = (fn, state) => {
|
|
4842
|
-
const promise = fn(state);
|
|
4843
|
-
return promise;
|
|
4844
|
-
};
|
|
4845
|
-
|
|
4846
|
-
/**
|
|
4847
|
-
* Finds all nodes of a specific type in the given AST.
|
|
4848
|
-
*
|
|
4849
|
-
* @param ast The AST to search in.
|
|
4850
|
-
* @param searchType The type of nodes to search for.
|
|
4851
|
-
* @returns An array of nodes of the specified type if found, otherwise undefined.
|
|
4852
|
-
*/
|
|
4853
|
-
const extract = (node, searchType) => {
|
|
4854
|
-
if (node) {
|
|
4855
|
-
const state = [];
|
|
4856
|
-
const visitors = {
|
|
4857
|
-
[searchType]: (node) => { state.push(node); return; }
|
|
4858
|
-
};
|
|
4859
|
-
walk.simple(node, visitors);
|
|
4860
|
-
return state.length > 0 ? state : undefined;
|
|
4861
|
-
}
|
|
4862
|
-
return undefined;
|
|
4863
|
-
};
|
|
4864
|
-
|
|
4865
4738
|
const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
4866
4739
|
beforeVisitor(node, st);
|
|
4867
4740
|
const fn = (...arrowArgs) => {
|
|
@@ -4869,7 +4742,7 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
4869
4742
|
// The scope stack lives on EvalContext, not on the per-walk EvalState, and
|
|
4870
4743
|
// one EvalContext can back any number of evaluations - so a scope this
|
|
4871
4744
|
// closure fails to pop outlives the walk and is read first by every later
|
|
4872
|
-
// evaluation on the same context. The body is re-entrant
|
|
4745
|
+
// evaluation on the same context. The body is a re-entrant walk, which
|
|
4873
4746
|
// rethrows, so the pop needs `finally`.
|
|
4874
4747
|
//
|
|
4875
4748
|
// The `try` opens *after* the push, never around it. `contextPush` skips
|
|
@@ -4880,12 +4753,18 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
4880
4753
|
// Both take the method from the context's class (`context-method.ts`).
|
|
4881
4754
|
// This site is the one an expression could reach twice over: a planted
|
|
4882
4755
|
// own `push` would be invoked here with the real context as its `this`,
|
|
4883
|
-
// and - since this closure's body is a re-entrant
|
|
4756
|
+
// and - since this closure's body is a re-entrant walk that pushes
|
|
4884
4757
|
// again - a plant that evaluates anything recurses until the stack is
|
|
4885
4758
|
// gone, leaving a reused context unusable (`docs/backlog.md` A42).
|
|
4886
4759
|
contextPush(st.context, newContext, st.options);
|
|
4887
4760
|
try {
|
|
4888
|
-
|
|
4761
|
+
// `walkWithoutResult`, not `evaluate`: the body's walk records nothing
|
|
4762
|
+
// on `st.result`, which describes the entry-point walk alone - during
|
|
4763
|
+
// that walk and after it, however often this closure is called
|
|
4764
|
+
// (`docs/backlog-retired.md` A27). As an arrow's body, it accepts the
|
|
4765
|
+
// `await` of an expression body or of a block body's last statement
|
|
4766
|
+
// (`docs/backlog-retired.md` A24).
|
|
4767
|
+
return walkWithoutResult(node.body, st, true);
|
|
4889
4768
|
}
|
|
4890
4769
|
finally {
|
|
4891
4770
|
contextPop(st.context);
|
|
@@ -4907,13 +4786,15 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
4907
4786
|
* statement list, `IfStatement` a branch, and `ForStatement` an `init` and a
|
|
4908
4787
|
* body.
|
|
4909
4788
|
*
|
|
4910
|
-
* **It is explicit, and its
|
|
4911
|
-
* not implement raises rather than falling through to
|
|
4912
|
-
* walker, which would visit the subtree as an expression and
|
|
4913
|
-
* pushed - `while (false) { 1 }` used to evaluate to `1` that
|
|
4914
|
-
* visitors in this library already push nothing on one path of an
|
|
4915
|
-
* chain over node types (`docs/backlog.md` A2); a
|
|
4916
|
-
* throws is how this family gains no fourth member.
|
|
4789
|
+
* **It is explicit, and a type outside its table throws.** A node type this
|
|
4790
|
+
* library does not implement raises rather than falling through to
|
|
4791
|
+
* `acorn-walk`'s base walker, which would visit the subtree as an expression and
|
|
4792
|
+
* strand whatever it pushed - `while (false) { 1 }` used to evaluate to `1` that
|
|
4793
|
+
* way. Three visitors in this library already push nothing on one path of an
|
|
4794
|
+
* `if`/`else if` chain over node types (`docs/backlog.md` A2); a table whose
|
|
4795
|
+
* miss throws is how this family gains no fourth member. The table and its
|
|
4796
|
+
* message are `statementTypeRefusal`'s, in `statement-support.ts`, since D13:
|
|
4797
|
+
* this was a `switch` whose `default` threw until then.
|
|
4917
4798
|
*
|
|
4918
4799
|
* **Why this module exists.** It lived in `program.ts` for steps 1 to 5, on the
|
|
4919
4800
|
* rule that a shared helper earns its own file at its *third* importer -
|
|
@@ -4932,29 +4813,14 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
4932
4813
|
* for a doc link is an unused binding the lint config rejects.
|
|
4933
4814
|
*/
|
|
4934
4815
|
const dispatchStatement = (statement, st, callback) => {
|
|
4935
|
-
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
// which can read `kind`.
|
|
4941
|
-
//
|
|
4942
|
-
// `ForStatement` is the classic three-part form *only*. `ForOfStatement`,
|
|
4943
|
-
// `ForInStatement`, `WhileStatement` and `DoWhileStatement` are distinct
|
|
4944
|
-
// node types and stay out of the phase (§ 2), so they reach the `default`
|
|
4945
|
-
// below unchanged - unlike `var`, which shares a type with `let` and had to
|
|
4946
|
-
// be rejected one level down.
|
|
4947
|
-
case 'ExpressionStatement':
|
|
4948
|
-
case 'EmptyStatement':
|
|
4949
|
-
case 'BlockStatement':
|
|
4950
|
-
case 'VariableDeclaration':
|
|
4951
|
-
case 'IfStatement':
|
|
4952
|
-
case 'ForStatement':
|
|
4953
|
-
callback(statement, st);
|
|
4954
|
-
return popVisitorResult(statement, st);
|
|
4955
|
-
default:
|
|
4956
|
-
throw new Error(`Unsupported statement type: ${statement.type}`);
|
|
4816
|
+
// The types walked are `statement-support.ts`' table, which
|
|
4817
|
+
// `findUnsupportedStatement` reads too, so the two cannot disagree.
|
|
4818
|
+
const refusal = statementTypeRefusal(statement);
|
|
4819
|
+
if (refusal !== undefined) {
|
|
4820
|
+
throw new Error(refusal);
|
|
4957
4821
|
}
|
|
4822
|
+
callback(statement, st);
|
|
4823
|
+
return popVisitorResult(statement, st);
|
|
4958
4824
|
};
|
|
4959
4825
|
|
|
4960
4826
|
/**
|
|
@@ -4966,9 +4832,9 @@ const dispatchStatement = (statement, st, callback) => {
|
|
|
4966
4832
|
* completion-value semantics, and pushes exactly one value however many
|
|
4967
4833
|
* statements the body holds. A program whose statements all produced nothing -
|
|
4968
4834
|
* `;`, or a source that is only whitespace - pushes {@link EMPTY_COMPLETION},
|
|
4969
|
-
* which the walk boundary in `evaluate` converts to `undefined`. The
|
|
4970
|
-
* is deliberately not done here: `arrow-function-expression.ts`
|
|
4971
|
-
*
|
|
4835
|
+
* which the walk boundary in `evaluate.ts` converts to `undefined`. The
|
|
4836
|
+
* conversion is deliberately not done here: `arrow-function-expression.ts`
|
|
4837
|
+
* walks a `BlockStatement` for a block-bodied arrow, so a `Program`-side
|
|
4972
4838
|
* conversion would leave that entry point handing the sentinel to a consumer.
|
|
4973
4839
|
*
|
|
4974
4840
|
* **The program-level scope is required.** It is the only scope a top-level
|
|
@@ -5032,10 +4898,10 @@ const expressionStatementVisitor = (node, st, callback) => {
|
|
|
5032
4898
|
* {@link EMPTY_COMPLETION} and an enclosing statement list keeps whatever
|
|
5033
4899
|
* preceded it.
|
|
5034
4900
|
*
|
|
5035
|
-
* One line of behaviour, and it exists because the statement dispatcher
|
|
5036
|
-
*
|
|
5037
|
-
* "unsupported statement type", which is a divergence from
|
|
5038
|
-
* chose.
|
|
4901
|
+
* One line of behaviour, and it exists because the statement dispatcher
|
|
4902
|
+
* throws for a type outside its table: without it `a;;b` and `if (x) ;` would
|
|
4903
|
+
* start raising "unsupported statement type", which is a divergence from
|
|
4904
|
+
* JavaScript nobody chose.
|
|
5039
4905
|
*/
|
|
5040
4906
|
const emptyStatementVisitor = (node, st) => {
|
|
5041
4907
|
beforeVisitor(node, st);
|
|
@@ -5052,10 +4918,10 @@ const emptyStatementVisitor = (node, st) => {
|
|
|
5052
4918
|
* statements the body holds. A block whose statements all produced nothing -
|
|
5053
4919
|
* `{ }`, or `{ ; }` - pushes {@link EMPTY_COMPLETION} for its enclosing
|
|
5054
4920
|
* statement list to skip over. The sentinel is converted to `undefined` at the
|
|
5055
|
-
* walk boundary in `evaluate`, which is why this visitor may return it to a
|
|
5056
|
-
* caller that is itself a walk root: `arrow-function-expression.ts`
|
|
5057
|
-
*
|
|
5058
|
-
*
|
|
4921
|
+
* walk boundary in `evaluate.ts`, which is why this visitor may return it to a
|
|
4922
|
+
* caller that is itself a walk root: `arrow-function-expression.ts` walks this
|
|
4923
|
+
* node for a block-bodied arrow, so `x => { }` reaches a consumer through the
|
|
4924
|
+
* same conversion a `Program` does.
|
|
5059
4925
|
*
|
|
5060
4926
|
* **The block scope is unobservable in this step, and is pushed anyway.**
|
|
5061
4927
|
* Nothing binds into it until `VariableDeclaration` lands, and an empty scope
|
|
@@ -5155,7 +5021,7 @@ const blockStatementVisitor = (node, st, callback) => {
|
|
|
5155
5021
|
* declaration is not a legal bare statement body; `if (a) var x = 1` does parse,
|
|
5156
5022
|
* reaches the dispatcher as a `VariableDeclaration`, and is rejected on `kind`
|
|
5157
5023
|
* inside `variableDeclarationVisitor`; and `if (a) function f() { }` parses
|
|
5158
|
-
* under Annex B and is rejected by `dispatchStatement
|
|
5024
|
+
* under Annex B and is rejected by `dispatchStatement`, whose table omits it.
|
|
5159
5025
|
*
|
|
5160
5026
|
* **The fourth is closed by the dispatcher and not by anything here**, which is
|
|
5161
5027
|
* the one a later phase can reopen: a phase that registers `FunctionDeclaration`
|
|
@@ -5186,12 +5052,12 @@ const ifStatementVisitor = (node, st, callback) => {
|
|
|
5186
5052
|
* § 1.1's row measured **`NaN`** with three stranded values before Phase 2: the
|
|
5187
5053
|
* base walker handed the declarator's `id` to `identifierVisitor`, so `i`
|
|
5188
5054
|
* resolved against the context rather than binding, and `i < 3` compared
|
|
5189
|
-
* `undefined`. Since step 1 the row throws from `dispatchStatement
|
|
5190
|
-
* this visitor is what returns `2`.
|
|
5055
|
+
* `undefined`. Since step 1 the row throws from `dispatchStatement`, which
|
|
5056
|
+
* refuses a type outside its table; this visitor is what returns `2`.
|
|
5191
5057
|
*
|
|
5192
5058
|
* `for…of`, `for…in`, `while` and `do…while` stay out of the phase (§ 2) and
|
|
5193
|
-
* keep throwing - they are separate node types
|
|
5194
|
-
*
|
|
5059
|
+
* keep throwing - they are separate node types, outside the dispatcher's table,
|
|
5060
|
+
* and are refused unchanged.
|
|
5195
5061
|
*
|
|
5196
5062
|
* **One scope for the loop, not one per iteration.** A draft of the plan
|
|
5197
5063
|
* specified a fresh scope per iteration, seeded from the head and copied back
|
|
@@ -5211,10 +5077,10 @@ const ifStatementVisitor = (node, st, callback) => {
|
|
|
5211
5077
|
* through `dispatchStatement`; `for (i = 0; …)` parses it as an `Expression` and
|
|
5212
5078
|
* takes a raw `callback`. Both pop exactly one - the declaration's `EMPTY`, or
|
|
5213
5079
|
* the expression's value - so § 3.1's arithmetic is identical for the two and
|
|
5214
|
-
* the split is invisible to it. What depends on the split is the
|
|
5215
|
-
*
|
|
5216
|
-
* statement type this library does not implement is rejected
|
|
5217
|
-
* to the base walker. `body` is a `Statement` and dispatches for the same
|
|
5080
|
+
* the split is invisible to it. What depends on the split is the refusal of a
|
|
5081
|
+
* type outside the table: only the `dispatchStatement` route has one, so an
|
|
5082
|
+
* `init` that is a statement type this library does not implement is rejected
|
|
5083
|
+
* rather than handed to the base walker. `body` is a `Statement` and dispatches for the same
|
|
5218
5084
|
* reason; `test` and `update` are expressions and do not.
|
|
5219
5085
|
*
|
|
5220
5086
|
* Arithmetic (§ 3.1): pops 1 for `init` when present, then per iteration 1 for
|
|
@@ -5856,6 +5722,22 @@ class EvalTrace extends Array {
|
|
|
5856
5722
|
|
|
5857
5723
|
/**
|
|
5858
5724
|
* Represents the result of an evaluation.
|
|
5725
|
+
*
|
|
5726
|
+
* **The outcome describes the last entry-point walk on the state**: its
|
|
5727
|
+
* {@link value}, {@link error}, {@link errorMessage}, {@link isSuccess},
|
|
5728
|
+
* {@link isError}, {@link isUndefined} and {@link duration}. Each entry point
|
|
5729
|
+
* resets the outcome when its walk starts and writes it when the walk ends - for
|
|
5730
|
+
* `evalAsync` and its kin, once the result's promises have settled, so between
|
|
5731
|
+
* return and settlement a walk that did not throw reports neither success nor
|
|
5732
|
+
* failure. An arrow function's body runs a walk of its own on the same state,
|
|
5733
|
+
* and it changes nothing here: not while the enclosing walk runs, and not when
|
|
5734
|
+
* a function the walk built is called later. Up to 0.14.1 it overwrote all of it
|
|
5735
|
+
* (`docs/backlog-retired.md` A27). Two `evalAsync` runs interleaved on one
|
|
5736
|
+
* state share one outcome: the later start resets it, and the last to settle
|
|
5737
|
+
* writes it.
|
|
5738
|
+
*
|
|
5739
|
+
* The {@link trace} is not part of the outcome: it spans every evaluation run on
|
|
5740
|
+
* the state, and only {@link clearTrace} resets it.
|
|
5859
5741
|
*/
|
|
5860
5742
|
class EvalResult {
|
|
5861
5743
|
/**
|
|
@@ -5865,37 +5747,45 @@ class EvalResult {
|
|
|
5865
5747
|
return this._stack;
|
|
5866
5748
|
}
|
|
5867
5749
|
/**
|
|
5868
|
-
* Gets the evaluated value
|
|
5750
|
+
* Gets the evaluated value: the last entry-point walk's, and `undefined`
|
|
5751
|
+
* once that walk failed or while it is pending.
|
|
5869
5752
|
*/
|
|
5870
5753
|
get value() {
|
|
5871
5754
|
return this._value;
|
|
5872
5755
|
}
|
|
5873
5756
|
/**
|
|
5874
|
-
* Gets the error
|
|
5757
|
+
* Gets the error the last entry-point walk failed with, and `undefined` once
|
|
5758
|
+
* it succeeded or while it is pending. An error an arrow function's body
|
|
5759
|
+
* threw and something in the walk caught is not the walk's.
|
|
5875
5760
|
*/
|
|
5876
5761
|
get error() {
|
|
5877
5762
|
return this._error;
|
|
5878
5763
|
}
|
|
5879
5764
|
/**
|
|
5880
|
-
* Gets
|
|
5765
|
+
* Gets {@link error}'s message when it is an `Error`, and `undefined`
|
|
5766
|
+
* otherwise.
|
|
5881
5767
|
*/
|
|
5882
5768
|
get errorMessage() {
|
|
5883
5769
|
return this._errorMessage;
|
|
5884
5770
|
}
|
|
5885
5771
|
/**
|
|
5886
|
-
* Gets whether the
|
|
5772
|
+
* Gets whether the last entry-point walk failed: `undefined` while it is
|
|
5773
|
+
* pending.
|
|
5887
5774
|
*/
|
|
5888
5775
|
get isError() {
|
|
5889
5776
|
return this._isError;
|
|
5890
5777
|
}
|
|
5891
5778
|
/**
|
|
5892
|
-
* Gets whether the
|
|
5779
|
+
* Gets whether the last entry-point walk succeeded: `undefined` while it is
|
|
5780
|
+
* pending.
|
|
5893
5781
|
*/
|
|
5894
5782
|
get isSuccess() {
|
|
5895
5783
|
return this._isSuccess;
|
|
5896
5784
|
}
|
|
5897
5785
|
/**
|
|
5898
|
-
* Gets whether
|
|
5786
|
+
* Gets whether {@link value} is `undefined`, once the last entry-point walk
|
|
5787
|
+
* has ended - including after a failure, which clears the value. `undefined`
|
|
5788
|
+
* while it is pending.
|
|
5899
5789
|
*/
|
|
5900
5790
|
get isUndefined() {
|
|
5901
5791
|
return this._isUndefined;
|
|
@@ -5946,7 +5836,8 @@ class EvalResult {
|
|
|
5946
5836
|
return this._context.options;
|
|
5947
5837
|
}
|
|
5948
5838
|
/**
|
|
5949
|
-
* Gets the duration of the
|
|
5839
|
+
* Gets the duration of the last entry-point walk, an arrow function's body
|
|
5840
|
+
* called inside it included: `undefined` while it is pending.
|
|
5950
5841
|
* @returns The duration in milliseconds.
|
|
5951
5842
|
*/
|
|
5952
5843
|
get duration() {
|
|
@@ -6045,10 +5936,24 @@ class EvalResult {
|
|
|
6045
5936
|
this._traceTruncated = false;
|
|
6046
5937
|
}
|
|
6047
5938
|
/**
|
|
6048
|
-
* Starts the evaluation process
|
|
5939
|
+
* Starts the evaluation process, and resets the outcome: {@link value},
|
|
5940
|
+
* {@link error}, {@link errorMessage}, {@link isSuccess}, {@link isError},
|
|
5941
|
+
* {@link isUndefined} and {@link duration} read `undefined` until the walk
|
|
5942
|
+
* ends. Leaves the
|
|
5943
|
+
* {@link trace}, {@link tracePushCount} and {@link traceTruncated} alone,
|
|
5944
|
+
* since the trace spans every evaluation on the state (`docs/backlog.md`
|
|
5945
|
+
* A15). Up to 0.14.1 it reset nothing, so a reused state reported the last
|
|
5946
|
+
* run's error until something overwrote it (A27).
|
|
6049
5947
|
*/
|
|
6050
5948
|
start() {
|
|
6051
5949
|
this._startDate = performance.now();
|
|
5950
|
+
this._endDate = undefined;
|
|
5951
|
+
this._value = undefined;
|
|
5952
|
+
this._error = undefined;
|
|
5953
|
+
this._errorMessage = undefined;
|
|
5954
|
+
this._isSuccess = undefined;
|
|
5955
|
+
this._isError = undefined;
|
|
5956
|
+
this._isUndefined = undefined;
|
|
6052
5957
|
}
|
|
6053
5958
|
/**
|
|
6054
5959
|
* Stops the evaluation and records the end date.
|
|
@@ -6058,29 +5963,33 @@ class EvalResult {
|
|
|
6058
5963
|
return this.duration;
|
|
6059
5964
|
}
|
|
6060
5965
|
/**
|
|
6061
|
-
* Sets the success value of the evaluation result
|
|
5966
|
+
* Sets the success value of the evaluation result, writing every outcome
|
|
5967
|
+
* field: the error and its message are cleared.
|
|
6062
5968
|
*
|
|
6063
5969
|
* @param value The value to set as the success value.
|
|
6064
5970
|
* @returns void
|
|
6065
5971
|
*/
|
|
6066
5972
|
setSuccess(value) {
|
|
6067
5973
|
this._value = value;
|
|
5974
|
+
this._error = undefined;
|
|
5975
|
+
this._errorMessage = undefined;
|
|
6068
5976
|
this._isSuccess = true;
|
|
6069
5977
|
this._isError = false;
|
|
6070
|
-
this._isUndefined =
|
|
5978
|
+
this._isUndefined = value === undefined;
|
|
6071
5979
|
}
|
|
6072
5980
|
/**
|
|
6073
|
-
* Sets the failure state of the evaluation result
|
|
5981
|
+
* Sets the failure state of the evaluation result, writing every outcome
|
|
5982
|
+
* field: the value is cleared, and the message is the error's when it is an
|
|
5983
|
+
* `Error`.
|
|
6074
5984
|
* @param error The error object associated with the failure.
|
|
6075
5985
|
*/
|
|
6076
5986
|
setFailure(error) {
|
|
5987
|
+
this._value = undefined;
|
|
6077
5988
|
this._error = error;
|
|
6078
|
-
|
|
6079
|
-
this._errorMessage = error.message;
|
|
6080
|
-
}
|
|
5989
|
+
this._errorMessage = error instanceof Error ? error.message : undefined;
|
|
6081
5990
|
this._isSuccess = false;
|
|
6082
5991
|
this._isError = true;
|
|
6083
|
-
this._isUndefined =
|
|
5992
|
+
this._isUndefined = true;
|
|
6084
5993
|
}
|
|
6085
5994
|
}
|
|
6086
5995
|
|
|
@@ -6380,7 +6289,7 @@ class EvalHooks {
|
|
|
6380
6289
|
* **"Open" means open in *this* walk.** Both routes below are bounded by the
|
|
6381
6290
|
* current walk base ({@link EvalHookBookkeeping.walkBases}), so a node open
|
|
6382
6291
|
* only in an enclosing walk falls into case 3 by either route - which is the
|
|
6383
|
-
* behaviour case 3 already specifies for "not open at all".
|
|
6292
|
+
* behaviour case 3 already specifies for "not open at all". The walk is
|
|
6384
6293
|
* re-entered with the same state from an arrow-function body, so without the
|
|
6385
6294
|
* bound a nested walk closing a node it never opened would flush the
|
|
6386
6295
|
* enclosing walk's frames under `completed: false` events a consumer reads as
|
|
@@ -6437,7 +6346,7 @@ class EvalHooks {
|
|
|
6437
6346
|
}
|
|
6438
6347
|
/**
|
|
6439
6348
|
* How many nodes the state has open. Captured as a mark before a walk, since
|
|
6440
|
-
*
|
|
6349
|
+
* the walk is re-entered with the same state from an arrow-function body.
|
|
6441
6350
|
*
|
|
6442
6351
|
* This is a method rather than a getter because the stack it measures lives
|
|
6443
6352
|
* on the state, not on this registry.
|
|
@@ -6447,10 +6356,11 @@ class EvalHooks {
|
|
|
6447
6356
|
}
|
|
6448
6357
|
/**
|
|
6449
6358
|
* Records that a walk is starting at the state's current open depth, and
|
|
6450
|
-
* returns that depth. Called by
|
|
6451
|
-
*
|
|
6359
|
+
* returns that depth. Called on entry by every walk - `evaluate`'s, an
|
|
6360
|
+
* arrow-function body's and `evaluateAsync`'s - under a `hasHooks` guard,
|
|
6361
|
+
* and paired with {@link popWalkBase} in a `finally`.
|
|
6452
6362
|
*
|
|
6453
|
-
* The returned value is the same mark those
|
|
6363
|
+
* The returned value is the same mark those walks hand to
|
|
6454
6364
|
* {@link unwindTo}, so they capture it once rather than reading the depth
|
|
6455
6365
|
* twice.
|
|
6456
6366
|
*/
|
|
@@ -6461,9 +6371,9 @@ class EvalHooks {
|
|
|
6461
6371
|
return base;
|
|
6462
6372
|
}
|
|
6463
6373
|
/**
|
|
6464
|
-
* Drops the innermost walk base. Called from the `finally` of the
|
|
6465
|
-
*
|
|
6466
|
-
*
|
|
6374
|
+
* Drops the innermost walk base. Called from the `finally` of the walk that
|
|
6375
|
+
* pushed it, so a walk that threw still restores the enclosing walk's
|
|
6376
|
+
* bound.
|
|
6467
6377
|
*/
|
|
6468
6378
|
popWalkBase(state) {
|
|
6469
6379
|
state.hookBookkeeping.walkBases.pop();
|
|
@@ -6482,12 +6392,12 @@ class EvalHooks {
|
|
|
6482
6392
|
}
|
|
6483
6393
|
/**
|
|
6484
6394
|
* Fires 'after' for every node the state has open above `mark`, innermost
|
|
6485
|
-
* first, marked incomplete. Invoked from the catch
|
|
6395
|
+
* first, marked incomplete. Invoked from the catch of every walk, so that a
|
|
6486
6396
|
* visitor which throws still leaves every 'before' matched by exactly one
|
|
6487
6397
|
* 'after'.
|
|
6488
6398
|
*
|
|
6489
6399
|
* Unwinding to a mark rather than to the bottom is what keeps a nested
|
|
6490
|
-
*
|
|
6400
|
+
* walk from draining the enclosing walk's open nodes when a host
|
|
6491
6401
|
* function swallows the nested throw.
|
|
6492
6402
|
*
|
|
6493
6403
|
* Idempotent, so the sync and async entry points can both call it.
|
|
@@ -6835,10 +6745,9 @@ class EvalState {
|
|
|
6835
6745
|
{ open: [], errors: [], timings: new Map(), walkBases: [] });
|
|
6836
6746
|
}
|
|
6837
6747
|
/**
|
|
6838
|
-
* How many
|
|
6839
|
-
*
|
|
6840
|
-
*
|
|
6841
|
-
* `evaluate` on this same state.
|
|
6748
|
+
* How many walks on this state are currently on the JavaScript stack: 0
|
|
6749
|
+
* between evaluations, 1 during an ordinary walk, and more while a nested
|
|
6750
|
+
* walk runs - an arrow-function body is a re-entrant walk on this same state.
|
|
6842
6751
|
*
|
|
6843
6752
|
* Maintained by {@link enterWalk} / {@link exitWalk}, which the entry points
|
|
6844
6753
|
* pair around their walk, the exit in a `finally` so a throw still restores
|
|
@@ -6873,7 +6782,7 @@ class EvalState {
|
|
|
6873
6782
|
enterWalk() {
|
|
6874
6783
|
const depth = ++this._walkDepth;
|
|
6875
6784
|
// § 3.4's refill, and the 0 -> 1 test is the whole of it. A nested walk -
|
|
6876
|
-
// an arrow-function body, which re-enters
|
|
6785
|
+
// an arrow-function body, which re-enters the walk on this same state -
|
|
6877
6786
|
// shares whatever the enclosing walk has left, so an escaped closure cannot
|
|
6878
6787
|
// buy itself a fresh budget by being called again. The options are re-read
|
|
6879
6788
|
// per outermost walk rather than in the constructor because they are a
|
|
@@ -7258,6 +7167,336 @@ class BaseEval {
|
|
|
7258
7167
|
}
|
|
7259
7168
|
}
|
|
7260
7169
|
|
|
7170
|
+
/**
|
|
7171
|
+
* @description
|
|
7172
|
+
* The colon-joined name grammar of `docs/colon-identifiers/phase-9-plan.md`
|
|
7173
|
+
* § 3.4, as an acorn plugin (§ 3.5). `parse` uses the parser built here when
|
|
7174
|
+
* `colonIdentifiers` is set, and acorn's own `parse` otherwise. No source text
|
|
7175
|
+
* is rewritten, so every position acorn reports is a position in what the
|
|
7176
|
+
* author wrote. Not re-exported from any barrel.
|
|
7177
|
+
*
|
|
7178
|
+
* Of acorn's documented API this module uses `Parser.extend`, `tokTypes` and
|
|
7179
|
+
* the `input` member. Everything else it touches is an acorn internal,
|
|
7180
|
+
* undeclared in acorn's `.d.ts`, and this is the complete list — what § 8 q3
|
|
7181
|
+
* checks at the `acorn` peer floor:
|
|
7182
|
+
*
|
|
7183
|
+
* - members read: `pos`, `start`, `type`, `value`, `keywords`;
|
|
7184
|
+
* - members written: `pos`, `containsEsc`;
|
|
7185
|
+
* - methods overridden, each calling acorn's own through `super`: `readWord`,
|
|
7186
|
+
* `finishToken`, `parsePropertyName`, `parseStatement`, `unexpected`;
|
|
7187
|
+
* - methods called: `finishToken`, `raise`;
|
|
7188
|
+
* - what the overrides assume of acorn's tokenizer: `readToken` is the only
|
|
7189
|
+
* caller of `readWord`, with `pos` at the token's first character; every
|
|
7190
|
+
* token is finished through `finishToken`, in source order, before the next
|
|
7191
|
+
* is read — except that a `/` read as `slash` is finished again as
|
|
7192
|
+
* `regexp`, and neither type is counted here; `?.`, `??` and `??=` are never
|
|
7193
|
+
* `question` tokens; the `}` that closes a `${` is finished as `braceR`; the
|
|
7194
|
+
* constructor reads no token;
|
|
7195
|
+
* - what they assume of acorn's parser: every key of an object literal, an
|
|
7196
|
+
* object pattern or a class body is read through `parsePropertyName`, with
|
|
7197
|
+
* the key as the current token; every statement through `parseStatement`,
|
|
7198
|
+
* with its first token as the current token; a colon the grammar cannot
|
|
7199
|
+
* place reaches `unexpected`, from `expect` or `semicolon`, with `type` and
|
|
7200
|
+
* `start` still at that colon.
|
|
7201
|
+
*
|
|
7202
|
+
* The token types it compares by identity, from `tokTypes`: `name`, `colon`,
|
|
7203
|
+
* `question`, `comma`, `semi`, `parenL`, `parenR`, `bracketL`, `bracketR`,
|
|
7204
|
+
* `braceL`, `braceR`, `dollarBraceL`.
|
|
7205
|
+
*/
|
|
7206
|
+
const COLON = 58;
|
|
7207
|
+
/** `[A-Za-z_$]`: the first character of a name that may join. */
|
|
7208
|
+
const isFirstChar = (code) => (code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 || code === 36;
|
|
7209
|
+
/** `[A-Za-z0-9_$]`: any later character, and a segment's first. */
|
|
7210
|
+
const isSegmentChar = (code) => isFirstChar(code) || (code >= 48 && code <= 57);
|
|
7211
|
+
/** Whether a `:segment` starts at `pos`: a colon with a segment character after it. */
|
|
7212
|
+
const joinsAt = (input, pos) => input.charCodeAt(pos) === COLON && isSegmentChar(input.charCodeAt(pos + 1));
|
|
7213
|
+
const colonIdentifiers = (Base) => {
|
|
7214
|
+
class ColonParser extends Base {
|
|
7215
|
+
constructor() {
|
|
7216
|
+
super(...arguments);
|
|
7217
|
+
/** Never empty: the top level is never closed. */
|
|
7218
|
+
this.levels = [{ pending: 0 }];
|
|
7219
|
+
/**
|
|
7220
|
+
* The level the current token was read at: for a `(`, `[`, `{` or `${`,
|
|
7221
|
+
* the one around the level it opened.
|
|
7222
|
+
*/
|
|
7223
|
+
this.tokenLevel = this.levels[0];
|
|
7224
|
+
}
|
|
7225
|
+
get level() {
|
|
7226
|
+
return this.levels[this.levels.length - 1];
|
|
7227
|
+
}
|
|
7228
|
+
/**
|
|
7229
|
+
* Reads a colon-joined name as one `name` token when one starts at `pos`
|
|
7230
|
+
* and no `?` at this level is waiting for its `:` (§ 3.4, 1). In every
|
|
7231
|
+
* other case acorn's own `readWord`, so a word that does not join is read
|
|
7232
|
+
* exactly as acorn reads it.
|
|
7233
|
+
*/
|
|
7234
|
+
readWord() {
|
|
7235
|
+
const end = this.colonNameEnd();
|
|
7236
|
+
if (end === undefined) {
|
|
7237
|
+
return super.readWord();
|
|
7238
|
+
}
|
|
7239
|
+
const name = this.input.slice(this.pos, end);
|
|
7240
|
+
if (this.level.pending > 0) {
|
|
7241
|
+
this.level.suppressed = name;
|
|
7242
|
+
return super.readWord();
|
|
7243
|
+
}
|
|
7244
|
+
this.pos = end;
|
|
7245
|
+
this.containsEsc = false;
|
|
7246
|
+
this.finishToken(tokTypes.name, name);
|
|
7247
|
+
}
|
|
7248
|
+
/**
|
|
7249
|
+
* Where the colon name that starts at `pos` ends — its longest match — or
|
|
7250
|
+
* `undefined` when none does: the first segment is not ASCII, is a keyword
|
|
7251
|
+
* of the parse's `ecmaVersion`, or has no `:segment` directly after it.
|
|
7252
|
+
*/
|
|
7253
|
+
colonNameEnd() {
|
|
7254
|
+
const input = this.input;
|
|
7255
|
+
let end = this.pos;
|
|
7256
|
+
if (!isFirstChar(input.charCodeAt(end))) {
|
|
7257
|
+
return undefined;
|
|
7258
|
+
}
|
|
7259
|
+
do {
|
|
7260
|
+
end++;
|
|
7261
|
+
} while (isSegmentChar(input.charCodeAt(end)));
|
|
7262
|
+
if (!joinsAt(input, end) || this.keywords.test(input.slice(this.pos, end))) {
|
|
7263
|
+
return undefined;
|
|
7264
|
+
}
|
|
7265
|
+
do {
|
|
7266
|
+
end += 2;
|
|
7267
|
+
while (isSegmentChar(input.charCodeAt(end))) {
|
|
7268
|
+
end++;
|
|
7269
|
+
}
|
|
7270
|
+
} while (joinsAt(input, end));
|
|
7271
|
+
return end;
|
|
7272
|
+
}
|
|
7273
|
+
/**
|
|
7274
|
+
* Keeps the levels after acorn has finished the token, recording the one
|
|
7275
|
+
* the token was read at: `(`, `[`, `{` and `${` open a level and `)`, `]`
|
|
7276
|
+
* and `}` close it; a `?` is counted at its level and a `:` there
|
|
7277
|
+
* uncounts it. `?.` and `??` are other token types, and count for nothing.
|
|
7278
|
+
*/
|
|
7279
|
+
finishToken(type, value) {
|
|
7280
|
+
super.finishToken(type, value);
|
|
7281
|
+
const level = this.level;
|
|
7282
|
+
this.tokenLevel = level;
|
|
7283
|
+
switch (type) {
|
|
7284
|
+
case tokTypes.parenL:
|
|
7285
|
+
case tokTypes.bracketL:
|
|
7286
|
+
case tokTypes.braceL:
|
|
7287
|
+
case tokTypes.dollarBraceL:
|
|
7288
|
+
this.levels.push({ pending: 0 });
|
|
7289
|
+
break;
|
|
7290
|
+
case tokTypes.parenR:
|
|
7291
|
+
case tokTypes.bracketR:
|
|
7292
|
+
case tokTypes.braceR:
|
|
7293
|
+
if (this.levels.length > 1) {
|
|
7294
|
+
this.levels.pop();
|
|
7295
|
+
}
|
|
7296
|
+
break;
|
|
7297
|
+
case tokTypes.question:
|
|
7298
|
+
level.pending++;
|
|
7299
|
+
break;
|
|
7300
|
+
case tokTypes.colon:
|
|
7301
|
+
if (level.pending > 0) {
|
|
7302
|
+
level.pending--;
|
|
7303
|
+
}
|
|
7304
|
+
break;
|
|
7305
|
+
case tokTypes.comma:
|
|
7306
|
+
case tokTypes.semi:
|
|
7307
|
+
level.suppressed = undefined;
|
|
7308
|
+
break;
|
|
7309
|
+
}
|
|
7310
|
+
}
|
|
7311
|
+
/**
|
|
7312
|
+
* Refuses a colon name as an object key, in a literal or a pattern alike
|
|
7313
|
+
* (§ 3.4, 2): `{Q1:1}` is `{Q1: 1}` to JavaScript and a shorthand to the
|
|
7314
|
+
* grammar, and keeping either would answer some author wrongly. Only a
|
|
7315
|
+
* joined token is a `name` holding a colon.
|
|
7316
|
+
*/
|
|
7317
|
+
parsePropertyName(prop) {
|
|
7318
|
+
const name = this.value;
|
|
7319
|
+
if (this.type === tokTypes.name && typeof name === 'string' && name.includes(':')) {
|
|
7320
|
+
const first = name.slice(0, name.indexOf(':'));
|
|
7321
|
+
this.raise(this.start, `Colon-joined name '${name}' cannot be an object key: ` +
|
|
7322
|
+
`write '${first}: ...' with a space for a property, or quote the key`);
|
|
7323
|
+
}
|
|
7324
|
+
return super.parsePropertyName(prop);
|
|
7325
|
+
}
|
|
7326
|
+
/**
|
|
7327
|
+
* Forgets the name not joined at the level a statement starts at: the
|
|
7328
|
+
* statement boundary ended the conditional whose consequent lost it. A
|
|
7329
|
+
* semicolon acorn inserts at a line break is such a boundary, and no `;`
|
|
7330
|
+
* token marks it.
|
|
7331
|
+
*/
|
|
7332
|
+
parseStatement(...args) {
|
|
7333
|
+
this.tokenLevel.suppressed = undefined;
|
|
7334
|
+
return super.parseStatement(...args);
|
|
7335
|
+
}
|
|
7336
|
+
/**
|
|
7337
|
+
* acorn's own error, with the hint of § 3.4, 1 when the unexpected token
|
|
7338
|
+
* is a `:` at a level that did not join a name since its last `,`, `;` or
|
|
7339
|
+
* statement start: the colon the name lost to the conditional. Any other
|
|
7340
|
+
* error is left as acorn words it.
|
|
7341
|
+
*/
|
|
7342
|
+
unexpected(pos) {
|
|
7343
|
+
const suppressed = this.tokenLevel.suppressed;
|
|
7344
|
+
if (suppressed !== undefined && this.type === tokTypes.colon &&
|
|
7345
|
+
(pos === undefined || pos === this.start)) {
|
|
7346
|
+
return this.raise(this.start, 'Unexpected token: a colon-joined name in the ' +
|
|
7347
|
+
`consequent of a conditional needs parentheses: (${suppressed})`);
|
|
7348
|
+
}
|
|
7349
|
+
return super.unexpected(pos);
|
|
7350
|
+
}
|
|
7351
|
+
}
|
|
7352
|
+
return ColonParser;
|
|
7353
|
+
};
|
|
7354
|
+
/**
|
|
7355
|
+
* acorn's `Parser` with the grammar, built once, at module load. `extend`
|
|
7356
|
+
* returns a subclass, so acorn's own `Parser`, which `acorn.parse` uses, is
|
|
7357
|
+
* left as it was.
|
|
7358
|
+
*/
|
|
7359
|
+
const ColonIdentifiersParser = Parser.extend(colonIdentifiers);
|
|
7360
|
+
|
|
7361
|
+
/**
|
|
7362
|
+
* @description
|
|
7363
|
+
* Parses expression and returns ES6/ES2020 AST.
|
|
7364
|
+
* With `colonIdentifiers` set the parser is the colon-joined name grammar's;
|
|
7365
|
+
* otherwise it is acorn's own `parse`.
|
|
7366
|
+
* @param expr expression to parse
|
|
7367
|
+
* @param options parser options
|
|
7368
|
+
*/
|
|
7369
|
+
const parse = (expr, options) => {
|
|
7370
|
+
options = { ecmaVersion: 2020, ...options };
|
|
7371
|
+
const acornOptions = { ecmaVersion: 2020, ...options };
|
|
7372
|
+
const program = options.colonIdentifiers
|
|
7373
|
+
? ColonIdentifiersParser.parse(expr, acornOptions)
|
|
7374
|
+
: parse$1(expr, acornOptions);
|
|
7375
|
+
if (!options.extractExpressions) {
|
|
7376
|
+
return program;
|
|
7377
|
+
}
|
|
7378
|
+
return extractExpression(program);
|
|
7379
|
+
};
|
|
7380
|
+
/**
|
|
7381
|
+
* @description
|
|
7382
|
+
* Walks through the AST and extracts first expression.
|
|
7383
|
+
* @param expr expression to parse
|
|
7384
|
+
*/
|
|
7385
|
+
const extractExpression = (program) => {
|
|
7386
|
+
if (program.body.length === 1) {
|
|
7387
|
+
const expression = program.body[0];
|
|
7388
|
+
if (expression.type === 'ExpressionStatement') {
|
|
7389
|
+
return expression.expression;
|
|
7390
|
+
}
|
|
7391
|
+
}
|
|
7392
|
+
return undefined;
|
|
7393
|
+
};
|
|
7394
|
+
|
|
7395
|
+
/**
|
|
7396
|
+
* Compiles the given node and returns a function that can be used to evaluate it.
|
|
7397
|
+
*
|
|
7398
|
+
* @param node The node to compile.
|
|
7399
|
+
* @returns A function that takes an `EvalState` and returns the result of evaluating the node.
|
|
7400
|
+
*/
|
|
7401
|
+
const compile = (node) => {
|
|
7402
|
+
return evaluate.bind(null, node);
|
|
7403
|
+
};
|
|
7404
|
+
/**
|
|
7405
|
+
* Compiles the given node asynchronously.
|
|
7406
|
+
*
|
|
7407
|
+
* @param node - The node to compile.
|
|
7408
|
+
* @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
|
|
7409
|
+
*/
|
|
7410
|
+
const compileAsync = (node) => {
|
|
7411
|
+
return evaluateAsync.bind(null, node);
|
|
7412
|
+
};
|
|
7413
|
+
/**
|
|
7414
|
+
* Calls the provided function with the given state and returns the result.
|
|
7415
|
+
*
|
|
7416
|
+
* @param fn The function to be called.
|
|
7417
|
+
* @param state The state to be passed to the function.
|
|
7418
|
+
* @returns The result of calling the function.
|
|
7419
|
+
*/
|
|
7420
|
+
const call = (fn, state) => {
|
|
7421
|
+
const value = fn(state);
|
|
7422
|
+
return value;
|
|
7423
|
+
};
|
|
7424
|
+
/**
|
|
7425
|
+
* Calls an asynchronous function with the given state and returns a promise that resolves to the result.
|
|
7426
|
+
* @param fn The asynchronous function to call.
|
|
7427
|
+
* @param state The state to pass to the function.
|
|
7428
|
+
* @returns A promise that resolves to the result of the function.
|
|
7429
|
+
*/
|
|
7430
|
+
const callAsync = (fn, state) => {
|
|
7431
|
+
const promise = fn(state);
|
|
7432
|
+
return promise;
|
|
7433
|
+
};
|
|
7434
|
+
|
|
7435
|
+
/**
|
|
7436
|
+
* Finds all nodes of a specific type in the given AST.
|
|
7437
|
+
*
|
|
7438
|
+
* @param ast The AST to search in.
|
|
7439
|
+
* @param searchType The type of nodes to search for.
|
|
7440
|
+
* @returns An array of nodes of the specified type if found, otherwise undefined.
|
|
7441
|
+
*/
|
|
7442
|
+
const extract = (node, searchType) => {
|
|
7443
|
+
if (node) {
|
|
7444
|
+
const state = [];
|
|
7445
|
+
const visitors = {
|
|
7446
|
+
[searchType]: (node) => { state.push(node); return; }
|
|
7447
|
+
};
|
|
7448
|
+
walk.simple(node, visitors);
|
|
7449
|
+
return state.length > 0 ? state : undefined;
|
|
7450
|
+
}
|
|
7451
|
+
return undefined;
|
|
7452
|
+
};
|
|
7453
|
+
|
|
7454
|
+
/**
|
|
7455
|
+
* Whether a node is a statement or a module declaration. Every member of
|
|
7456
|
+
* acorn's `Statement` and `ModuleDeclaration` unions is named `…Statement` or
|
|
7457
|
+
* `…Declaration`, and no other node type is: a declarator is a
|
|
7458
|
+
* `VariableDeclarator`.
|
|
7459
|
+
*/
|
|
7460
|
+
const isStatement = (node) => /(?:Statement|Declaration)$/.test(node.type);
|
|
7461
|
+
/** The message evaluation throws for this statement, or `undefined`. */
|
|
7462
|
+
const refusalOf = (statement) => statementTypeRefusal(statement)
|
|
7463
|
+
?? (statement.type === 'VariableDeclaration' ? declarationKindRefusal(statement) : undefined);
|
|
7464
|
+
/**
|
|
7465
|
+
* Finds the first statement in a tree that evaluation would refuse.
|
|
7466
|
+
*
|
|
7467
|
+
* Evaluation refuses a statement only when the walk reaches it, so one in an
|
|
7468
|
+
* arrow function's body that is never called, or in a branch not taken, throws
|
|
7469
|
+
* nothing. This reads the whole tree instead - function bodies, untaken
|
|
7470
|
+
* branches and class bodies included - against the same table the evaluator
|
|
7471
|
+
* uses, and returns the message evaluation throws for that statement.
|
|
7472
|
+
*
|
|
7473
|
+
* "First" is source order, the lowest `start`, so a refused statement inside
|
|
7474
|
+
* another refused one is reported after it, as evaluation would meet the outer
|
|
7475
|
+
* one first.
|
|
7476
|
+
*
|
|
7477
|
+
* @param node The AST to search, as `parse` returns it.
|
|
7478
|
+
* @returns The statement and its message, or `undefined` when the tree holds
|
|
7479
|
+
* none, or `node` is `undefined`.
|
|
7480
|
+
*/
|
|
7481
|
+
const findUnsupportedStatement = (node) => {
|
|
7482
|
+
if (!node) {
|
|
7483
|
+
return undefined;
|
|
7484
|
+
}
|
|
7485
|
+
let first;
|
|
7486
|
+
// `walk.full` calls back in post-order, an enclosing statement after the
|
|
7487
|
+
// ones inside it, so the first one visited is not the first in the source.
|
|
7488
|
+
walk.full(node, (visited) => {
|
|
7489
|
+
if (!isStatement(visited)) {
|
|
7490
|
+
return;
|
|
7491
|
+
}
|
|
7492
|
+
const message = refusalOf(visited);
|
|
7493
|
+
if (message !== undefined && (first === undefined || visited.start < first.statement.start)) {
|
|
7494
|
+
first = { statement: visited, message };
|
|
7495
|
+
}
|
|
7496
|
+
});
|
|
7497
|
+
return first;
|
|
7498
|
+
};
|
|
7499
|
+
|
|
7261
7500
|
/**
|
|
7262
7501
|
* The options a cached AST cannot honour: acorn calls each callback as it
|
|
7263
7502
|
* parses, and appends to a caller's `program` node. A parse carrying any of
|
|
@@ -8005,5 +8244,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.3", ngImpor
|
|
|
8005
8244
|
* Generated bundle index. Do not edit.
|
|
8006
8245
|
*/
|
|
8007
8246
|
|
|
8008
|
-
export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextValue, parse, provideColonIdentifiers };
|
|
8247
|
+
export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, findUnsupportedStatement, fromContext, getContextValue, parse, provideColonIdentifiers };
|
|
8009
8248
|
//# sourceMappingURL=zvenigora-ng-eval-core.mjs.map
|