clarity-pattern-parser 11.1.4 → 11.2.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/dist/index.esm.js CHANGED
@@ -2276,11 +2276,22 @@ const asKeyword = new Literal("as", "as");
2276
2276
  const fromKeyword = new Literal("from", "from");
2277
2277
  const openBracket = new Literal("open-bracket", "{");
2278
2278
  const closeBracket = new Literal("close-bracket", "}");
2279
+ const equal = new Literal("equal", "=");
2279
2280
  const importNameAlias = name.clone("import-name-alias");
2280
2281
  const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
2281
2282
  const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
2282
2283
  const paramName = name.clone("param-name");
2283
- const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
2284
+ const defaultParamName = name.clone("default-param-name");
2285
+ const paramNameWithDefault = new Sequence("param-name-with-default-value", [
2286
+ paramName,
2287
+ new Optional("optional-param-default", new Sequence("param-default", [
2288
+ optionalLineSpaces$1,
2289
+ equal,
2290
+ optionalLineSpaces$1,
2291
+ defaultParamName,
2292
+ ])),
2293
+ ]);
2294
+ const paramNames = new Repeat("param-names", paramNameWithDefault, { divider: importNameDivider });
2284
2295
  const resource = literal$1.clone("resource");
2285
2296
  const useParams = new Sequence("import-params", [
2286
2297
  useParamsKeyword,
@@ -2472,6 +2483,9 @@ class Context {
2472
2483
  return this.children[0].startedOnIndex;
2473
2484
  }
2474
2485
  getPatternWithinContext(name) {
2486
+ if (this._name === name || this._referencePatternName === name) {
2487
+ return this;
2488
+ }
2475
2489
  return this._patterns[name] || null;
2476
2490
  }
2477
2491
  getPatternsWithinContext() {
@@ -2483,6 +2497,7 @@ class Context {
2483
2497
  this._name = name;
2484
2498
  this._parent = null;
2485
2499
  this._patterns = {};
2500
+ this._referencePatternName = name;
2486
2501
  const clonedPattern = pattern.clone();
2487
2502
  context.forEach(p => this._patterns[p.name] = p);
2488
2503
  clonedPattern.parent = this;
@@ -2500,6 +2515,7 @@ class Context {
2500
2515
  }
2501
2516
  clone(name = this._name) {
2502
2517
  const clone = new Context(name, this._pattern.clone(name), Object.values(this._patterns));
2518
+ clone._referencePatternName = this._referencePatternName;
2503
2519
  clone._id = this._id;
2504
2520
  return clone;
2505
2521
  }
@@ -3592,61 +3608,94 @@ class Grammar {
3592
3608
  return this._buildPattern(wrappedNode);
3593
3609
  }
3594
3610
  _resolveImports(ast) {
3611
+ return __awaiter(this, void 0, void 0, function* () {
3612
+ const importStatements = ast.findAll(n => {
3613
+ return n.name === "import-from" || n.name === "param-name-with-default-value";
3614
+ });
3615
+ for (const statement of importStatements) {
3616
+ if (statement.name === "import-from") {
3617
+ yield this.processImport(statement);
3618
+ }
3619
+ else {
3620
+ this.processUseParams(statement);
3621
+ }
3622
+ }
3623
+ });
3624
+ }
3625
+ processImport(importStatement) {
3595
3626
  return __awaiter(this, void 0, void 0, function* () {
3596
3627
  const parseContext = this._parseContext;
3597
- const importStatements = ast.findAll(n => n.name === "import-from");
3598
- for (const importStatement of importStatements) {
3599
- const resourceNode = importStatement.find(n => n.name === "resource");
3600
- const params = this._getParams(importStatement);
3601
- const resource = resourceNode.value.slice(1, -1);
3602
- const grammarFile = yield this._resolveImport(resource, this._originResource || null);
3603
- const grammar = new Grammar({
3604
- resolveImport: this._resolveImport,
3605
- originResource: grammarFile.resource,
3606
- params,
3607
- decorators: this._parseContext.decorators
3608
- });
3609
- try {
3610
- const patterns = yield grammar.parse(grammarFile.expression);
3611
- const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
3612
- importStatements.forEach((node) => {
3613
- var _a, _b;
3614
- if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
3615
- return;
3628
+ const resourceNode = importStatement.find(n => n.name === "resource");
3629
+ const params = this._getParams(importStatement);
3630
+ const resource = resourceNode.value.slice(1, -1);
3631
+ const grammarFile = yield this._resolveImport(resource, this._originResource || null);
3632
+ const grammar = new Grammar({
3633
+ resolveImport: this._resolveImport,
3634
+ originResource: grammarFile.resource,
3635
+ params,
3636
+ decorators: this._parseContext.decorators
3637
+ });
3638
+ try {
3639
+ const patterns = yield grammar.parse(grammarFile.expression);
3640
+ const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
3641
+ importStatements.forEach((node) => {
3642
+ var _a, _b;
3643
+ if (node.name === "import-name" && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.name) === "import-alias") {
3644
+ return;
3645
+ }
3646
+ if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
3647
+ const importName = node.value;
3648
+ if (parseContext.importedPatternsByName.has(importName)) {
3649
+ throw new Error(`'${importName}' was already used within another import.`);
3616
3650
  }
3617
- if (node.name === "import-name" && ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.name) !== "import-alias") {
3618
- const importName = node.value;
3619
- if (parseContext.importedPatternsByName.has(importName)) {
3620
- throw new Error(`'${importName}' was already used within another import.`);
3621
- }
3622
- const pattern = patterns[importName];
3623
- if (pattern == null) {
3624
- throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3625
- }
3626
- parseContext.importedPatternsByName.set(importName, pattern);
3651
+ const pattern = patterns[importName];
3652
+ if (pattern == null) {
3653
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3627
3654
  }
3628
- else {
3629
- const importNameNode = node.find(n => n.name === "import-name");
3630
- const importName = importNameNode.value;
3631
- const aliasNode = node.find(n => n.name === "import-name-alias");
3632
- const alias = aliasNode.value;
3633
- if (parseContext.importedPatternsByName.has(alias)) {
3634
- throw new Error(`'${alias}' was already used within another import.`);
3635
- }
3636
- const pattern = patterns[importName];
3637
- if (pattern == null) {
3638
- throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3639
- }
3640
- parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
3655
+ parseContext.importedPatternsByName.set(importName, pattern);
3656
+ }
3657
+ else {
3658
+ const importNameNode = node.find(n => n.name === "import-name");
3659
+ const importName = importNameNode.value;
3660
+ const aliasNode = node.find(n => n.name === "import-name-alias");
3661
+ const alias = aliasNode.value;
3662
+ if (parseContext.importedPatternsByName.has(alias)) {
3663
+ throw new Error(`'${alias}' was already used within another import.`);
3641
3664
  }
3642
- });
3643
- }
3644
- catch (e) {
3645
- throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
3646
- }
3665
+ const pattern = patterns[importName];
3666
+ if (pattern == null) {
3667
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
3668
+ }
3669
+ parseContext.importedPatternsByName.set(alias, pattern.clone(alias));
3670
+ }
3671
+ });
3672
+ }
3673
+ catch (e) {
3674
+ throw new Error(`Failed loading expression from: "${resource}". Error details: "${e.message}"`);
3647
3675
  }
3648
3676
  });
3649
3677
  }
3678
+ processUseParams(paramName) {
3679
+ const defaultValueNode = paramName.find(n => n.name === "param-default");
3680
+ if (defaultValueNode === null) {
3681
+ return;
3682
+ }
3683
+ const nameNode = paramName.find(n => n.name === "param-name");
3684
+ const defaultNameNode = defaultValueNode.find(n => n.name === "default-param-name");
3685
+ if (nameNode == null || defaultNameNode == null) {
3686
+ return;
3687
+ }
3688
+ const name = nameNode.value;
3689
+ const defaultName = defaultNameNode.value;
3690
+ if (this._parseContext.paramsByName.has(name)) {
3691
+ return;
3692
+ }
3693
+ let pattern = this._parseContext.importedPatternsByName.get(defaultName);
3694
+ if (pattern == null) {
3695
+ pattern = new Reference(defaultName);
3696
+ }
3697
+ this._parseContext.importedPatternsByName.set(name, pattern);
3698
+ }
3650
3699
  _applyDecorators(statementNode, pattern) {
3651
3700
  const decorators = this._parseContext.decorators;
3652
3701
  const bodyLine = statementNode.parent;
@@ -3731,7 +3780,7 @@ class Grammar {
3731
3780
  const aliasPattern = this._getPattern(aliasName);
3732
3781
  // This solves the problem for an alias pointing to a reference.
3733
3782
  if (aliasPattern.type === "reference") {
3734
- const reference = new Reference(name, aliasName);
3783
+ const reference = aliasPattern.clone(name);
3735
3784
  this._applyDecorators(statementNode, reference);
3736
3785
  this._parseContext.patternsByName.set(name, reference);
3737
3786
  }