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/TODO.md +17 -3
- package/dist/grammar/patterns/statement.d.ts +2 -2
- package/dist/index.browser.js +46 -16
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +46 -16
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +46 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/grammar/Grammar.test.ts +76 -10
- package/src/grammar/Grammar.ts +49 -12
- package/src/grammar/patterns/import.ts +8 -4
- package/src/grammar/patterns/statement.ts +4 -2
package/dist/index.esm.js
CHANGED
|
@@ -1778,7 +1778,7 @@ const statements = new Or("statements", [
|
|
|
1778
1778
|
repeatLiteral,
|
|
1779
1779
|
name$1.clone("alias-literal"),
|
|
1780
1780
|
]);
|
|
1781
|
-
const
|
|
1781
|
+
const assignStatement = new And("assign-statement", [
|
|
1782
1782
|
optionalSpaces$1,
|
|
1783
1783
|
name$1,
|
|
1784
1784
|
optionalSpaces$1,
|
|
@@ -1786,6 +1786,7 @@ const statement = new And("statement", [
|
|
|
1786
1786
|
optionalSpaces$1,
|
|
1787
1787
|
statements
|
|
1788
1788
|
]);
|
|
1789
|
+
const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
|
|
1789
1790
|
|
|
1790
1791
|
const bodyLineContent = new Or("body-line-content", [
|
|
1791
1792
|
comment,
|
|
@@ -1798,18 +1799,21 @@ const bodyLine = new And("body-line", [
|
|
|
1798
1799
|
]);
|
|
1799
1800
|
const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
|
|
1800
1801
|
|
|
1802
|
+
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1803
|
+
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1801
1804
|
const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
|
|
1802
1805
|
const importKeyword = new Literal("import", "import");
|
|
1803
1806
|
const useParamsKeyword = new Literal("use-params", "use params");
|
|
1807
|
+
const asKeyword = new Literal("as", "as");
|
|
1804
1808
|
const fromKeyword = new Literal("from", "from");
|
|
1805
1809
|
const openBracket = new Literal("open-bracket", "{");
|
|
1806
1810
|
const closeBracket = new Literal("close-bracket", "}");
|
|
1807
1811
|
const name = new Regex("import-name", "[^}\\s,]+");
|
|
1808
|
-
const
|
|
1812
|
+
const importNameAlias = name.clone("import-name-alias");
|
|
1813
|
+
const importAlias = new And("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
|
|
1814
|
+
const importedNames = new Repeat("imported-names", new Or("import-names", [importAlias, name]), { divider: importNameDivider });
|
|
1809
1815
|
const paramName = name.clone("param-name");
|
|
1810
1816
|
const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
|
|
1811
|
-
const optionalSpaces = allSpaces.clone("optional-spaces", true);
|
|
1812
|
-
const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
|
|
1813
1817
|
const resource = literal.clone("resource");
|
|
1814
1818
|
const useParams = new And("import-params", [
|
|
1815
1819
|
useParamsKeyword,
|
|
@@ -1826,7 +1830,7 @@ const withParamsStatement = new And("with-params-statement", [
|
|
|
1826
1830
|
optionalLineSpaces,
|
|
1827
1831
|
openBracket,
|
|
1828
1832
|
optionalSpaces,
|
|
1829
|
-
body,
|
|
1833
|
+
body.clone("with-params-body"),
|
|
1830
1834
|
optionalSpaces,
|
|
1831
1835
|
closeBracket
|
|
1832
1836
|
], true);
|
|
@@ -2255,9 +2259,13 @@ class Grammar {
|
|
|
2255
2259
|
return importBlock && importBlock.children.length > 0;
|
|
2256
2260
|
}
|
|
2257
2261
|
_buildPatterns(ast) {
|
|
2258
|
-
ast.
|
|
2262
|
+
const body = ast.find(n => n.name === "body");
|
|
2263
|
+
if (body == null) {
|
|
2264
|
+
return;
|
|
2265
|
+
}
|
|
2266
|
+
body.findAll(n => n.name === "assign-statement" || n.name === "export-name").forEach((n) => {
|
|
2259
2267
|
const typeNode = n.find(n => n.name.includes("literal"));
|
|
2260
|
-
const type = (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2268
|
+
const type = n.name === "export-name" ? "export-name" : (typeNode === null || typeNode === void 0 ? void 0 : typeNode.name) || "unknown";
|
|
2261
2269
|
switch (type) {
|
|
2262
2270
|
case "literal": {
|
|
2263
2271
|
this._buildLiteral(n);
|
|
@@ -2283,6 +2291,11 @@ class Grammar {
|
|
|
2283
2291
|
this._buildAlias(n);
|
|
2284
2292
|
break;
|
|
2285
2293
|
}
|
|
2294
|
+
case "export-name": {
|
|
2295
|
+
const pattern = this._getPattern(n.value);
|
|
2296
|
+
this._parseContext.patternsByName.set(n.value, pattern);
|
|
2297
|
+
break;
|
|
2298
|
+
}
|
|
2286
2299
|
}
|
|
2287
2300
|
});
|
|
2288
2301
|
}
|
|
@@ -2302,16 +2315,33 @@ class Grammar {
|
|
|
2302
2315
|
});
|
|
2303
2316
|
try {
|
|
2304
2317
|
const patterns = yield grammar.parse(grammarFile.expression);
|
|
2305
|
-
const
|
|
2306
|
-
|
|
2307
|
-
if (
|
|
2308
|
-
|
|
2318
|
+
const importStatements = importStatement.findAll(n => n.name === "import-name" || n.name === "import-alias");
|
|
2319
|
+
importStatements.forEach((node) => {
|
|
2320
|
+
if (node.name === "import-name") {
|
|
2321
|
+
const importName = node.value;
|
|
2322
|
+
if (parseContext.importedPatternsByName.has(importName)) {
|
|
2323
|
+
throw new Error(`'${importName}' was already used within another import.`);
|
|
2324
|
+
}
|
|
2325
|
+
const pattern = patterns.get(importName);
|
|
2326
|
+
if (pattern == null) {
|
|
2327
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2328
|
+
}
|
|
2329
|
+
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2309
2330
|
}
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2331
|
+
else {
|
|
2332
|
+
const importNameNode = node.find(n => n.name === "import-name");
|
|
2333
|
+
const importName = importNameNode.value;
|
|
2334
|
+
const aliasNode = node.find(n => n.name === "import-name-alias");
|
|
2335
|
+
const alias = aliasNode.value;
|
|
2336
|
+
if (parseContext.importedPatternsByName.has(alias)) {
|
|
2337
|
+
throw new Error(`'${alias}' was already used within another import.`);
|
|
2338
|
+
}
|
|
2339
|
+
const pattern = patterns.get(importName);
|
|
2340
|
+
if (pattern == null) {
|
|
2341
|
+
throw new Error(`Couldn't find pattern with name: ${importName}, from import: ${resource}.`);
|
|
2342
|
+
}
|
|
2343
|
+
parseContext.importedPatternsByName.set(alias, pattern);
|
|
2313
2344
|
}
|
|
2314
|
-
parseContext.importedPatternsByName.set(importName, pattern);
|
|
2315
2345
|
});
|
|
2316
2346
|
}
|
|
2317
2347
|
catch (e) {
|
|
@@ -2324,7 +2354,7 @@ class Grammar {
|
|
|
2324
2354
|
let params = [];
|
|
2325
2355
|
const paramsStatement = importStatement.find(n => n.name === "with-params-statement");
|
|
2326
2356
|
if (paramsStatement != null) {
|
|
2327
|
-
const statements = paramsStatement.find(n => n.name === "body");
|
|
2357
|
+
const statements = paramsStatement.find(n => n.name === "with-params-body");
|
|
2328
2358
|
if (statements != null) {
|
|
2329
2359
|
const expression = statements.toString();
|
|
2330
2360
|
const importedValues = Array.from(this
|