clarity-pattern-parser 8.4.10 → 8.4.12

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
@@ -1782,7 +1782,7 @@ const statements = new Or("statements", [
1782
1782
  repeatLiteral,
1783
1783
  name$1.clone("alias-literal"),
1784
1784
  ]);
1785
- const statement = new And("statement", [
1785
+ const assignStatement = new And("assign-statement", [
1786
1786
  optionalSpaces$1,
1787
1787
  name$1,
1788
1788
  optionalSpaces$1,
@@ -1790,6 +1790,7 @@ const statement = new And("statement", [
1790
1790
  optionalSpaces$1,
1791
1791
  statements
1792
1792
  ]);
1793
+ const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
1793
1794
 
1794
1795
  const bodyLineContent = new Or("body-line-content", [
1795
1796
  comment,
@@ -1802,18 +1803,21 @@ const bodyLine = new And("body-line", [
1802
1803
  ]);
1803
1804
  const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
1804
1805
 
1806
+ const optionalSpaces = allSpaces.clone("optional-spaces", true);
1807
+ const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
1805
1808
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
1806
1809
  const importKeyword = new Literal("import", "import");
1807
1810
  const useParamsKeyword = new Literal("use-params", "use params");
1811
+ const asKeyword = new Literal("as", "as");
1808
1812
  const fromKeyword = new Literal("from", "from");
1809
1813
  const openBracket = new Literal("open-bracket", "{");
1810
1814
  const closeBracket = new Literal("close-bracket", "}");
1811
1815
  const name = new Regex("import-name", "[^}\\s,]+");
1812
- const importedNames = new Repeat("imported-names", name, { divider: importNameDivider });
1816
+ const importNameAlias = name.clone("import-name-alias");
1817
+ const importAlias = new And("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
1818
+ const importedNames = new Repeat("imported-names", new Or("import-names", [importAlias, name]), { divider: importNameDivider });
1813
1819
  const paramName = name.clone("param-name");
1814
1820
  const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
1815
- const optionalSpaces = allSpaces.clone("optional-spaces", true);
1816
- const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
1817
1821
  const resource = literal.clone("resource");
1818
1822
  const useParams = new And("import-params", [
1819
1823
  useParamsKeyword,
@@ -1830,7 +1834,7 @@ const withParamsStatement = new And("with-params-statement", [
1830
1834
  optionalLineSpaces,
1831
1835
  openBracket,
1832
1836
  optionalSpaces,
1833
- body,
1837
+ body.clone("with-params-body"),
1834
1838
  optionalSpaces,
1835
1839
  closeBracket
1836
1840
  ], true);
@@ -2259,9 +2263,13 @@ class Grammar {
2259
2263
  return importBlock && importBlock.children.length > 0;
2260
2264
  }
2261
2265
  _buildPatterns(ast) {
2262
- ast.findAll(n => n.name === "statement").forEach((n) => {
2266
+ const body = ast.find(n => n.name === "body");
2267
+ if (body == null) {
2268
+ return;
2269
+ }
2270
+ body.findAll(n => n.name === "assign-statement" || n.name === "export-name").forEach((n) => {
2263
2271
  const typeNode = n.find(n => n.name.includes("literal"));
2264
- const type = (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
2272
+ const type = n.name === "export-name" ? "export-name" : (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
2265
2273
  switch (type) {
2266
2274
  case "literal": {
2267
2275
  this._buildLiteral(n);
@@ -2287,6 +2295,11 @@ class Grammar {
2287
2295
  this._buildAlias(n);
2288
2296
  break;
2289
2297
  }
2298
+ case "export-name": {
2299
+ const pattern = this._getPattern(n.value);
2300
+ this._parseContext.patternsByName.set(n.value, pattern);
2301
+ break;
2302
+ }
2290
2303
  }
2291
2304
  });
2292
2305
  }
@@ -2306,16 +2319,33 @@ class Grammar {
2306
2319
  });
2307
2320
  try {
2308
2321
  const patterns = yield grammar.parse(grammarFile.expression);
2309
- const importNames = importStatement.findAll(n => n.name === "import-name").map(n => n.value);
2310
- importNames.forEach((importName) => {
2311
- if (parseContext.importedPatternsByName.has(importName)) {
2312
- throw new Error(`'${importName}' was already used within another import.`);
2322
+ const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
2323
+ importStatements.forEach((node) => {
2324
+ if (node.name === "import-name") {
2325
+ const importName = node.value;
2326
+ if (parseContext.importedPatternsByName.has(importName)) {
2327
+ throw new Error(`'${importName}' was already used within another import.`);
2328
+ }
2329
+ const pattern = patterns.get(importName);
2330
+ if (pattern == null) {
2331
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
2332
+ }
2333
+ parseContext.importedPatternsByName.set(importName, pattern);
2313
2334
  }
2314
- const pattern = patterns.get(importName);
2315
- if (pattern == null) {
2316
- throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
2335
+ else {
2336
+ const importNameNode = node.find(n => n.name === "import-name");
2337
+ const importName = importNameNode.value;
2338
+ const aliasNode = node.find(n => n.name === "import-name-alias");
2339
+ const alias = aliasNode.value;
2340
+ if (parseContext.importedPatternsByName.has(alias)) {
2341
+ throw new Error(`'${alias}' was already used within another import.`);
2342
+ }
2343
+ const pattern = patterns.get(importName);
2344
+ if (pattern == null) {
2345
+ throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
2346
+ }
2347
+ parseContext.importedPatternsByName.set(alias, pattern);
2317
2348
  }
2318
- parseContext.importedPatternsByName.set(importName, pattern);
2319
2349
  });
2320
2350
  }
2321
2351
  catch (e) {
@@ -2328,7 +2358,7 @@ class Grammar {
2328
2358
  let params = [];
2329
2359
  const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
2330
2360
  if (paramsStatement != null) {
2331
- const statements = paramsStatement.find(n => n.name === "body");
2361
+ const statements = paramsStatement.find(n => n.name === "with-params-body");
2332
2362
  if (statements != null) {
2333
2363
  const expression = statements.toString();
2334
2364
  const importedValues = Array.from(this