@veryfront/ext-parser-babel 0.1.1046 → 0.1.1048
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/esm/index.d.ts +2 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +49 -3
- package/package.json +2 -2
package/esm/index.d.ts
CHANGED
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
* @module extensions/ext-parser-babel
|
|
15
15
|
*/
|
|
16
16
|
import type { ExtensionFactory } from "veryfront/extensions";
|
|
17
|
-
import type { ASTNode, CodeParser, GenerateOptions, GenerateResult, InjectJsxNodePositionsOptions, ParseOptions, TraverseVisitor } from "veryfront/extensions/parser";
|
|
17
|
+
import type { ASTNode, CodeParser, FunctionDirectiveOptions, GenerateOptions, GenerateResult, InjectJsxNodePositionsOptions, ParseOptions, TraverseVisitor } from "veryfront/extensions/parser";
|
|
18
18
|
declare class BabelCodeParser implements CodeParser {
|
|
19
19
|
parse(options: ParseOptions): Promise<ASTNode>;
|
|
20
20
|
traverse(ast: ASTNode, visitor: TraverseVisitor): void;
|
|
21
21
|
generate(ast: ASTNode, options?: GenerateOptions): Promise<GenerateResult>;
|
|
22
|
+
hasFunctionDirective(options: FunctionDirectiveOptions): Promise<boolean>;
|
|
22
23
|
injectJsxNodePositions(source: string, options: InjectJsxNodePositionsOptions): string;
|
|
23
24
|
}
|
|
24
25
|
declare const extBabel: ExtensionFactory;
|
package/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,eAAe,EACf,cAAc,EACd,6BAA6B,EAC7B,YAAY,EACZ,eAAe,EAChB,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,wBAAwB,EACxB,eAAe,EACf,cAAc,EACd,6BAA6B,EAC7B,YAAY,EACZ,eAAe,EAChB,MAAM,6BAA6B,CAAC;AAsErC,cAAM,eAAgB,YAAW,UAAU;IACzC,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC;IAS9C,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,eAAe,GAAG,IAAI;IAItD,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;IAWpE,oBAAoB,CAAC,OAAO,EAAE,wBAAwB,GAAG,OAAO,CAAC,OAAO,CAAC;IAY/E,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,6BAA6B,GAAG,MAAM;CAGvF;AAED,QAAA,MAAM,QAAQ,EAAE,gBAiBf,CAAC;AAEF,eAAe,QAAQ,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
package/esm/index.js
CHANGED
|
@@ -28,17 +28,50 @@ function resolveDefaultExport(mod) {
|
|
|
28
28
|
}
|
|
29
29
|
const traverse = resolveDefaultExport(traverseModule);
|
|
30
30
|
const generate = resolveDefaultExport(generateModule);
|
|
31
|
+
const FUNCTION_NODE_TYPES = [
|
|
32
|
+
"ArrowFunctionExpression",
|
|
33
|
+
"ClassMethod",
|
|
34
|
+
"ClassPrivateMethod",
|
|
35
|
+
"FunctionDeclaration",
|
|
36
|
+
"FunctionExpression",
|
|
37
|
+
"ObjectMethod",
|
|
38
|
+
];
|
|
39
|
+
function isRecord(value) {
|
|
40
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
function functionHasDirective(node, directive) {
|
|
43
|
+
const body = node.body;
|
|
44
|
+
if (!isRecord(body) || body.type !== "BlockStatement" || !Array.isArray(body.directives)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
return body.directives.some((entry) => isRecord(entry) && isRecord(entry.value) && entry.value.value === directive);
|
|
48
|
+
}
|
|
31
49
|
function pickPlugins(filePath) {
|
|
32
|
-
const
|
|
33
|
-
const
|
|
50
|
+
const normalizedPath = filePath?.toLowerCase() ?? "";
|
|
51
|
+
const isTypeScript = /\.(?:tsx?|[cm]ts)$/.test(normalizedPath);
|
|
52
|
+
const supportsJsx = !filePath || /\.(?:tsx|jsx|js|mjs|cjs)$/.test(normalizedPath);
|
|
53
|
+
const plugins = [
|
|
54
|
+
"classProperties",
|
|
55
|
+
"classPrivateProperties",
|
|
56
|
+
"classPrivateMethods",
|
|
57
|
+
"decorators-legacy",
|
|
58
|
+
"decoratorAutoAccessors",
|
|
59
|
+
"deprecatedImportAssert",
|
|
60
|
+
"dynamicImport",
|
|
61
|
+
"importAttributes",
|
|
62
|
+
"topLevelAwait",
|
|
63
|
+
];
|
|
34
64
|
if (isTypeScript || !filePath)
|
|
35
65
|
plugins.push("typescript");
|
|
66
|
+
if (supportsJsx)
|
|
67
|
+
plugins.push("jsx");
|
|
36
68
|
return plugins;
|
|
37
69
|
}
|
|
38
70
|
class BabelCodeParser {
|
|
39
71
|
parse(options) {
|
|
40
72
|
const ast = parser.parse(options.code, {
|
|
41
|
-
sourceType: "
|
|
73
|
+
sourceType: "unambiguous",
|
|
74
|
+
allowReturnOutsideFunction: options.filePath?.toLowerCase().endsWith(".cjs") === true,
|
|
42
75
|
plugins: pickPlugins(options.filePath),
|
|
43
76
|
});
|
|
44
77
|
return Promise.resolve(ast);
|
|
@@ -56,6 +89,19 @@ class BabelCodeParser {
|
|
|
56
89
|
map: typeof result.map === "string" ? result.map : undefined,
|
|
57
90
|
});
|
|
58
91
|
}
|
|
92
|
+
async hasFunctionDirective(options) {
|
|
93
|
+
const ast = await this.parse(options);
|
|
94
|
+
let found = false;
|
|
95
|
+
const visit = (path) => {
|
|
96
|
+
if (!found && functionHasDirective(path.node, options.directive))
|
|
97
|
+
found = true;
|
|
98
|
+
};
|
|
99
|
+
const visitor = {};
|
|
100
|
+
for (const nodeType of FUNCTION_NODE_TYPES)
|
|
101
|
+
visitor[nodeType] = visit;
|
|
102
|
+
this.traverse(ast, visitor);
|
|
103
|
+
return found;
|
|
104
|
+
}
|
|
59
105
|
injectJsxNodePositions(source, options) {
|
|
60
106
|
return injectNodePositions(source, options);
|
|
61
107
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfront/ext-parser-babel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1048",
|
|
4
4
|
"description": "Veryfront first-party extension package for ext-parser-babel",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"veryfront",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@babel/types": "7.29.0"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"veryfront": "^0.1.
|
|
51
|
+
"veryfront": "^0.1.1048"
|
|
52
52
|
},
|
|
53
53
|
"type": "module",
|
|
54
54
|
"types": "./esm/index.d.ts",
|