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