@veryfront/ext-parser-babel 0.1.1254-rc.17215 → 0.1.1254-rc.17243

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/README.md CHANGED
@@ -36,6 +36,17 @@ loading Babel traversal, code generation, JSX transformation, extension
36
36
  lifecycle, or debug dependencies. It does not read environment variables or
37
37
  request filesystem/network permissions.
38
38
 
39
+ `parse(options)` accepts two parser-only grammar controls:
40
+
41
+ - `syntax` selects `"javascript"` or `"typescript"`. It defaults to
42
+ `"typescript"` for compatibility with `CodeParser`.
43
+ - `decoratorMode` selects only the current decorator grammar with `"current"`,
44
+ or accepts the current and legacy grammars with `"compatible"`. It defaults
45
+ to `"compatible"`.
46
+
47
+ The file extension continues to select JSX parsing. Markdown paths are treated
48
+ as JSX-capable inputs because this entry receives their compiled output.
49
+
39
50
  ## Configuration
40
51
 
41
52
  No factory options. The extension takes no config.
@@ -12,13 +12,19 @@ import type { ASTNode, ParseOptions } from "veryfront/extensions/parser";
12
12
  /** The parse-only subset shared with the full `CodeParser` contract. */
13
13
  export interface BabelParseOnlyParserContract {
14
14
  /** Parse source code into a Babel-compatible abstract syntax tree. */
15
- parse(options: ParseOptions): Promise<ASTNode>;
15
+ parse(options: BabelParseOnlyOptions): Promise<ASTNode>;
16
+ }
17
+ export interface BabelParseOnlyOptions extends ParseOptions {
18
+ /** Decorator grammar to parse, or both for backward compatibility. */
19
+ readonly decoratorMode?: "current" | "compatible";
20
+ /** Source grammar to parse. Defaults to TypeScript for CodeParser compatibility. */
21
+ readonly syntax?: "javascript" | "typescript";
16
22
  }
17
23
  /**
18
24
  * Babel-backed parser with the same parse behavior as {@link BabelCodeParser},
19
25
  * without loading traversal, generation, or extension runtime dependencies.
20
26
  */
21
27
  export declare class BabelParseOnlyParser implements BabelParseOnlyParserContract {
22
- parse(options: ParseOptions): Promise<ASTNode>;
28
+ parse(options: BabelParseOnlyOptions): Promise<ASTNode>;
23
29
  }
24
30
  //# sourceMappingURL=parser-only.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parser-only.d.ts","sourceRoot":"","sources":["../src/parser-only.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEzE,wEAAwE;AACxE,MAAM,WAAW,4BAA4B;IAC3C,sEAAsE;IACtE,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAChD;AAqDD;;;GAGG;AACH,qBAAa,oBAAqB,YAAW,4BAA4B;IACvE,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC;CA4B/C"}
1
+ {"version":3,"file":"parser-only.d.ts","sourceRoot":"","sources":["../src/parser-only.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEzE,wEAAwE;AACxE,MAAM,WAAW,4BAA4B;IAC3C,sEAAsE;IACtE,KAAK,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACzD;AAED,MAAM,WAAW,qBAAsB,SAAQ,YAAY;IACzD,sEAAsE;IACtE,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,GAAG,YAAY,CAAC;IAClD,oFAAoF;IACpF,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC;CAC/C;AAmED;;;GAGG;AACH,qBAAa,oBAAqB,YAAW,4BAA4B;IACjE,KAAK,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC;CAqC9D"}
@@ -19,12 +19,12 @@ import * as parser from "@babel/parser";
19
19
  * value, and `.ts` keeps `<T>x` a type assertion because only Markdown paths
20
20
  * are rewritten.
21
21
  */
22
- function parseablePath(filePath) {
22
+ function parseablePath(filePath, syntax) {
23
23
  if (filePath === undefined)
24
24
  return undefined;
25
- return filePath.replace(/\.mdx?$/i, ".tsx");
25
+ return filePath.replace(/\.mdx?$/i, syntax === "typescript" ? ".tsx" : ".jsx");
26
26
  }
27
- function pickPlugins(filePath) {
27
+ function pickPlugins(filePath, syntax) {
28
28
  const normalizedPath = filePath?.toLowerCase() ?? "";
29
29
  const supportsJsx = !filePath ||
30
30
  /\.(?:tsx|jsx|js|mjs|cjs)$/.test(normalizedPath);
@@ -38,18 +38,18 @@ function pickPlugins(filePath) {
38
38
  "dynamicImport",
39
39
  "importAttributes",
40
40
  "topLevelAwait",
41
- // Hosted configs are authored in TypeScript but can arrive named `.js`, so
42
- // the extension cannot decide the dialect. TypeScript is a superset, so
43
- // enabling it always only widens what parses.
44
- "typescript",
45
41
  ];
42
+ // Hosted configs are authored in TypeScript but can arrive named `.js`, so
43
+ // the CodeParser-compatible default cannot infer the dialect from the path.
44
+ if (syntax === "typescript")
45
+ plugins.push("typescript");
46
46
  // JSX stays extension-driven so `.ts` keeps `<T>x` as a type assertion.
47
47
  if (supportsJsx)
48
48
  plugins.push("jsx");
49
49
  return plugins;
50
50
  }
51
- function pickLegacyDecoratorPlugins(filePath) {
52
- return pickPlugins(filePath).map((plugin) => plugin === "decorators" ? "decorators-legacy" : plugin);
51
+ function pickLegacyDecoratorPlugins(filePath, syntax) {
52
+ return pickPlugins(filePath, syntax).map((plugin) => plugin === "decorators" ? "decorators-legacy" : plugin);
53
53
  }
54
54
  function isBabelSyntaxError(error) {
55
55
  if (typeof error !== "object" || error === null)
@@ -57,30 +57,39 @@ function isBabelSyntaxError(error) {
57
57
  const code = Object.getOwnPropertyDescriptor(error, "code")?.value;
58
58
  return code === "BABEL_PARSER_SYNTAX_ERROR";
59
59
  }
60
+ function isAstNode(value) {
61
+ if (typeof value !== "object" || value === null)
62
+ return false;
63
+ return typeof Object.getOwnPropertyDescriptor(value, "type")?.value ===
64
+ "string";
65
+ }
60
66
  /**
61
67
  * Babel-backed parser with the same parse behavior as {@link BabelCodeParser},
62
68
  * without loading traversal, generation, or extension runtime dependencies.
63
69
  */
64
70
  export class BabelParseOnlyParser {
65
- parse(options) {
71
+ async parse(options) {
66
72
  const filePath = options.filePath?.toLowerCase() ?? "";
73
+ const decoratorMode = options.decoratorMode ?? "compatible";
74
+ const syntax = options.syntax ?? "typescript";
67
75
  const parseOptions = {
68
76
  sourceType: "unambiguous",
69
77
  allowReturnOutsideFunction: options.allowReturnOutsideFunction === true ||
70
78
  /\.(?:cjs|js)$/.test(filePath),
71
- plugins: pickPlugins(parseablePath(options.filePath)),
79
+ plugins: pickPlugins(parseablePath(options.filePath, syntax), syntax),
72
80
  };
73
81
  const ast = (() => {
74
82
  try {
75
83
  return parser.parse(options.code, parseOptions);
76
84
  }
77
85
  catch (error) {
78
- if (!isBabelSyntaxError(error))
86
+ if (!isBabelSyntaxError(error) || decoratorMode !== "compatible") {
79
87
  throw error;
88
+ }
80
89
  try {
81
90
  return parser.parse(options.code, {
82
91
  ...parseOptions,
83
- plugins: pickLegacyDecoratorPlugins(parseablePath(options.filePath)),
92
+ plugins: pickLegacyDecoratorPlugins(parseablePath(options.filePath, syntax), syntax),
84
93
  });
85
94
  }
86
95
  catch {
@@ -90,7 +99,9 @@ export class BabelParseOnlyParser {
90
99
  }
91
100
  }
92
101
  })();
93
- const node = ast;
94
- return Promise.resolve(node);
102
+ if (!isAstNode(ast)) {
103
+ throw new TypeError("Babel returned an invalid parser result");
104
+ }
105
+ return ast;
95
106
  }
96
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veryfront/ext-parser-babel",
3
- "version": "0.1.1254-rc.17215",
3
+ "version": "0.1.1254-rc.17243",
4
4
  "description": "Veryfront first-party extension package for ext-parser-babel",
5
5
  "keywords": [
6
6
  "veryfront",
@@ -54,7 +54,7 @@
54
54
  "@babel/types": "7.29.0"
55
55
  },
56
56
  "peerDependencies": {
57
- "veryfront": "^0.1.1254-rc.17215"
57
+ "veryfront": "^0.1.1254-rc.17243"
58
58
  },
59
59
  "type": "module",
60
60
  "types": "./esm/index.d.ts",
@@ -64,5 +64,5 @@
64
64
  "NOTICE",
65
65
  "README.md"
66
66
  ],
67
- "gitHead": "16dfed52fe89babf92f7f6918ddd30145c5e066d"
67
+ "gitHead": "03b04d4034a6a248de5f4277b293a35fea2c7855"
68
68
  }