goatlint-parser 0.125.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/README.md +167 -0
- package/package.json +129 -0
- package/src-js/bindings.js +601 -0
- package/src-js/generated/constants.js +95 -0
- package/src-js/generated/deserialize/js.js +5839 -0
- package/src-js/generated/deserialize/js_range.js +6380 -0
- package/src-js/generated/deserialize/ts.js +6131 -0
- package/src-js/generated/deserialize/ts_range.js +6700 -0
- package/src-js/generated/lazy/constructors.js +13864 -0
- package/src-js/generated/lazy/type_ids.js +191 -0
- package/src-js/generated/lazy/walk.js +5802 -0
- package/src-js/generated/visit/keys.js +220 -0
- package/src-js/generated/visit/type_ids.js +177 -0
- package/src-js/generated/visit/visitor.d.ts +387 -0
- package/src-js/generated/visit/walk.js +2455 -0
- package/src-js/index.d.ts +312 -0
- package/src-js/index.js +108 -0
- package/src-js/raw-transfer/common.js +276 -0
- package/src-js/raw-transfer/eager.js +254 -0
- package/src-js/raw-transfer/lazy-common.js +11 -0
- package/src-js/raw-transfer/lazy.js +153 -0
- package/src-js/raw-transfer/node-array.js +365 -0
- package/src-js/raw-transfer/supported.js +52 -0
- package/src-js/raw-transfer/visitor.js +127 -0
- package/src-js/visit/index.js +41 -0
- package/src-js/visit/visitor.js +405 -0
- package/src-js/wasm.js +11 -0
- package/src-js/webcontainer-fallback.cjs +21 -0
- package/src-js/wrap.js +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Oxc Parser
|
|
2
|
+
|
|
3
|
+
See [usage instructions](https://goatlint.dev/docs/guide/usage/parser).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### Supports WASM
|
|
8
|
+
|
|
9
|
+
See https://stackblitz.com/edit/oxc-parser for usage example.
|
|
10
|
+
|
|
11
|
+
### ESTree
|
|
12
|
+
|
|
13
|
+
When parsing JS or JSX files, the AST returned is fully conformant with the
|
|
14
|
+
[ESTree standard](https://github.com/estree/estree), the same as produced by
|
|
15
|
+
[Acorn](https://npmx.dev/package/acorn).
|
|
16
|
+
|
|
17
|
+
When parsing TypeScript, the AST conforms to [@typescript-eslint/typescript-estree](https://npmx.dev/package/@typescript-eslint/typescript-estree)'s TS-ESTree format.
|
|
18
|
+
|
|
19
|
+
If you need all ASTs in the same with-TS-properties format, use the `astType: 'ts'` option.
|
|
20
|
+
|
|
21
|
+
The only differences between Oxc's AST and ESTree / TS-ESTree are:
|
|
22
|
+
|
|
23
|
+
- Support for Stage 3 [decorators](https://github.com/tc39/proposal-decorators).
|
|
24
|
+
- Support for Stage 3 ECMA features [`import defer`](https://github.com/tc39/proposal-defer-import-eval)
|
|
25
|
+
and [`import source`](https://github.com/tc39/proposal-source-phase-imports).
|
|
26
|
+
- In TS-ESTree AST, `import.defer(...)` and `import.source(...)` are represented as an `ImportExpression`
|
|
27
|
+
with `'defer'` or `'source'` in `phase` field (as in ESTree spec), where TS-ESLint represents these
|
|
28
|
+
as a `CallExpression` with `MetaProperty` as its `callee`.
|
|
29
|
+
- Addition of a non-standard `hashbang` field to `Program`.
|
|
30
|
+
|
|
31
|
+
That aside, the AST should completely align with Acorn's ESTree AST or TS-ESLint's TS-ESTree.
|
|
32
|
+
Any deviation would be considered a bug.
|
|
33
|
+
|
|
34
|
+
### AST Types
|
|
35
|
+
|
|
36
|
+
[@goat-project/types](https://npmx.dev/package/@goat-project/types) can be used. For example:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Statement } from "@goat-project/types";
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Visitor
|
|
43
|
+
|
|
44
|
+
An AST visitor is provided. See example below.
|
|
45
|
+
|
|
46
|
+
This package also exports visitor keys which can be used with any other ESTree walker.
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { visitorKeys } from "oxc-parser";
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Fast Mode
|
|
53
|
+
|
|
54
|
+
By default, Oxc parser does not produce semantic errors where symbols and scopes are needed.
|
|
55
|
+
|
|
56
|
+
To enable semantic errors, apply the option `showSemanticErrors: true`.
|
|
57
|
+
|
|
58
|
+
For example,
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
let foo;
|
|
62
|
+
let foo;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Does not produce any errors when `showSemanticErrors` is `false`, which is the default behavior.
|
|
66
|
+
|
|
67
|
+
Fast mode is best suited for parser plugins, where other parts of your build pipeline has already checked for errors.
|
|
68
|
+
|
|
69
|
+
Please note that turning off fast mode incurs a small performance overhead.
|
|
70
|
+
|
|
71
|
+
### Returns ESM information.
|
|
72
|
+
|
|
73
|
+
It is likely that you are writing a parser plugin that requires ESM information.
|
|
74
|
+
|
|
75
|
+
To avoid walking the AST again, Oxc Parser returns ESM information directly.
|
|
76
|
+
|
|
77
|
+
This information can be used to rewrite import and exports with the help of [`magic-string`](https://npmx.dev/package/magic-string),
|
|
78
|
+
without any AST manipulations.
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
export interface EcmaScriptModule {
|
|
82
|
+
/**
|
|
83
|
+
* Has ESM syntax.
|
|
84
|
+
*
|
|
85
|
+
* i.e. `import` and `export` statements, and `import.meta`.
|
|
86
|
+
*
|
|
87
|
+
* Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
|
|
88
|
+
*/
|
|
89
|
+
hasModuleSyntax: boolean;
|
|
90
|
+
/** Import statements */
|
|
91
|
+
staticImports: Array<StaticImport>;
|
|
92
|
+
/** Export statements */
|
|
93
|
+
staticExports: Array<StaticExport>;
|
|
94
|
+
/** Dynamic import expressions */
|
|
95
|
+
dynamicImports: Array<DynamicImport>;
|
|
96
|
+
/** Span positions of `import.meta` */
|
|
97
|
+
importMetas: Array<Span>;
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## API
|
|
102
|
+
|
|
103
|
+
### Functions
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
// Synchronous parsing
|
|
107
|
+
parseSync(filename: string, sourceText: string, options?: ParserOptions): ParseResult
|
|
108
|
+
|
|
109
|
+
// Asynchronous parsing
|
|
110
|
+
parse(filename: string, sourceText: string, options?: ParserOptions): Promise<ParseResult>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Use `parseSync` for synchronous parsing. Use `parse` for asynchronous parsing, which can be beneficial in I/O-bound or concurrent scenarios, though it adds async overhead.
|
|
114
|
+
|
|
115
|
+
### Example
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
import { parseSync, Visitor } from "oxc-parser";
|
|
119
|
+
|
|
120
|
+
const code = "const url: String = /* 🤨 */ import.meta.url;";
|
|
121
|
+
|
|
122
|
+
// File extension is used to determine which dialect to parse source as.
|
|
123
|
+
const filename = "test.tsx";
|
|
124
|
+
|
|
125
|
+
const result = parseSync(filename, code);
|
|
126
|
+
// Or use async version: const result = await parse(filename, code);
|
|
127
|
+
|
|
128
|
+
// An array of errors, if any.
|
|
129
|
+
console.log(result.errors);
|
|
130
|
+
|
|
131
|
+
// AST and comments.
|
|
132
|
+
console.log(result.program, result.comments);
|
|
133
|
+
|
|
134
|
+
// ESM information - imports, exports, `import.meta`s.
|
|
135
|
+
console.log(result.module);
|
|
136
|
+
|
|
137
|
+
// Visit the AST
|
|
138
|
+
const visitations = [];
|
|
139
|
+
|
|
140
|
+
const visitor = new Visitor({
|
|
141
|
+
VariableDeclaration(decl) {
|
|
142
|
+
visitations.push(`enter ${decl.kind}`);
|
|
143
|
+
},
|
|
144
|
+
"VariableDeclaration:exit"(decl) {
|
|
145
|
+
visitations.push(`exit ${decl.kind}`);
|
|
146
|
+
},
|
|
147
|
+
Identifier(ident) {
|
|
148
|
+
visitations.push(ident.name);
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
visitor.visit(result.program);
|
|
153
|
+
|
|
154
|
+
// Logs: [ 'enter const', 'url', 'String', 'import', 'meta', 'url', 'exit const' ]
|
|
155
|
+
console.log(visitations);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Options
|
|
159
|
+
|
|
160
|
+
All options are optional.
|
|
161
|
+
|
|
162
|
+
- `lang`: `'js'` | `'jsx'` | `'ts'` | `'tsx'`. Set language of source. If omitted, language is deduced from file extension.
|
|
163
|
+
- `sourceType`: `'script'` | `'module'` | `'unambiguous'`. Set source type. Defaults to `'module'`.
|
|
164
|
+
- `astType`: `'js'` | `'ts'`. Set to `'ts'` if you want ASTs of plain JS/JSX files to contain TypeScript-specific properties.
|
|
165
|
+
- `range`: `true` | `false`. If `true`, AST nodes contain a `range` field. Defaults to `false`.
|
|
166
|
+
- `preserveParens`: `true` | `false`. If `true`, parenthesized expressions are represented by (non-standard) `ParenthesizedExpression` and `TSParenthesizedType` AST nodes. Defaults to `true`.
|
|
167
|
+
- `showSemanticErrors`: `true` | `false`. If `true`, check file for semantic errors which parser does not otherwise emit e.g. `let x; let x;`. Has a small performance cost. Defaults to `false`.
|
package/package.json
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "goatlint-parser",
|
|
3
|
+
"version": "0.125.0",
|
|
4
|
+
"description": "Oxc Parser Node API",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ast",
|
|
7
|
+
"estree",
|
|
8
|
+
"javascript",
|
|
9
|
+
"goat",
|
|
10
|
+
"parser",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://goatlint.dev/docs/guide/usage/parser",
|
|
14
|
+
"bugs": "https://github.com/goat-project/oxc/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Boshen and oxc contributors",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/goat-project/oxc.git",
|
|
20
|
+
"directory": "napi/parser"
|
|
21
|
+
},
|
|
22
|
+
"funding": {
|
|
23
|
+
"url": "https://github.com/sponsors/Boshen"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src-js/bindings.js",
|
|
27
|
+
"src-js/index.d.ts",
|
|
28
|
+
"src-js/index.js",
|
|
29
|
+
"src-js/wasm.js",
|
|
30
|
+
"src-js/webcontainer-fallback.cjs",
|
|
31
|
+
"src-js/wrap.js",
|
|
32
|
+
"src-js/generated/constants.js",
|
|
33
|
+
"src-js/raw-transfer/common.js",
|
|
34
|
+
"src-js/raw-transfer/eager.js",
|
|
35
|
+
"src-js/raw-transfer/lazy-common.js",
|
|
36
|
+
"src-js/raw-transfer/lazy.js",
|
|
37
|
+
"src-js/raw-transfer/node-array.js",
|
|
38
|
+
"src-js/raw-transfer/supported.js",
|
|
39
|
+
"src-js/raw-transfer/visitor.js",
|
|
40
|
+
"src-js/visit/index.js",
|
|
41
|
+
"src-js/visit/visitor.js",
|
|
42
|
+
"src-js/generated/deserialize/js.js",
|
|
43
|
+
"src-js/generated/deserialize/js_range.js",
|
|
44
|
+
"src-js/generated/deserialize/ts.js",
|
|
45
|
+
"src-js/generated/deserialize/ts_range.js",
|
|
46
|
+
"src-js/generated/lazy/constructors.js",
|
|
47
|
+
"src-js/generated/lazy/type_ids.js",
|
|
48
|
+
"src-js/generated/lazy/walk.js",
|
|
49
|
+
"src-js/generated/visit/keys.js",
|
|
50
|
+
"src-js/generated/visit/type_ids.js",
|
|
51
|
+
"src-js/generated/visit/visitor.d.ts",
|
|
52
|
+
"src-js/generated/visit/walk.js"
|
|
53
|
+
],
|
|
54
|
+
"type": "module",
|
|
55
|
+
"main": "src-js/index.js",
|
|
56
|
+
"browser": "src-js/wasm.js",
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"registry": "https://registry.npmjs.org/"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build-dev": "napi build --esm --platform --js bindings.js --dts index.d.ts --output-dir src-js",
|
|
63
|
+
"build-test": "pnpm run build-dev --profile coverage",
|
|
64
|
+
"build": "pnpm run build-dev --features allocator --release",
|
|
65
|
+
"postbuild": "publint",
|
|
66
|
+
"postbuild-dev": "node scripts/patch.js",
|
|
67
|
+
"build-wasi": "napi build --target wasm32-wasip1-threads",
|
|
68
|
+
"build-wasm": "pnpm run build-wasm-dev --release",
|
|
69
|
+
"build-wasm-dev": "pnpm run build-dev --target wasm32-wasip1-threads",
|
|
70
|
+
"build-npm-dir": "rm -rf npm-dir && napi create-npm-dirs --npm-dir npm-dir && pnpm napi artifacts --npm-dir npm-dir --output-dir src-js",
|
|
71
|
+
"build-browser-bundle": "node scripts/build-browser-bundle.js",
|
|
72
|
+
"test": "pnpm run test-node run",
|
|
73
|
+
"test-node": "vitest --dir ./test",
|
|
74
|
+
"test-browser": "vitest -c vitest.config.browser.ts",
|
|
75
|
+
"bench": "vitest bench --run ./bench.bench.js"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"goatlint-types": "^0.125.0"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@codspeed/vitest-plugin": "^5.0.0",
|
|
82
|
+
"@napi-rs/cli": "catalog:",
|
|
83
|
+
"@napi-rs/wasm-runtime": "catalog:",
|
|
84
|
+
"@types/node": "catalog:",
|
|
85
|
+
"@typescript-eslint/visitor-keys": "^8.54.0",
|
|
86
|
+
"@vitest/browser": "4.1.2",
|
|
87
|
+
"@vitest/browser-playwright": "4.1.2",
|
|
88
|
+
"esbuild": "^0.28.0",
|
|
89
|
+
"playwright": "^1.51.0",
|
|
90
|
+
"publint": "catalog:",
|
|
91
|
+
"tinypool": "^2.0.0",
|
|
92
|
+
"vitest": "catalog:"
|
|
93
|
+
},
|
|
94
|
+
"napi": {
|
|
95
|
+
"binaryName": "parser",
|
|
96
|
+
"dtsHeaderFile": "src-js/header.d.ts",
|
|
97
|
+
"packageName": "@goatlint-parser/binding",
|
|
98
|
+
"targets": [
|
|
99
|
+
"aarch64-apple-darwin",
|
|
100
|
+
"aarch64-linux-android",
|
|
101
|
+
"aarch64-pc-windows-msvc",
|
|
102
|
+
"aarch64-unknown-linux-gnu",
|
|
103
|
+
"aarch64-unknown-linux-musl",
|
|
104
|
+
"aarch64-unknown-linux-ohos",
|
|
105
|
+
"armv7-linux-androideabi",
|
|
106
|
+
"armv7-unknown-linux-gnueabihf",
|
|
107
|
+
"armv7-unknown-linux-musleabihf",
|
|
108
|
+
"i686-pc-windows-msvc",
|
|
109
|
+
"powerpc64le-unknown-linux-gnu",
|
|
110
|
+
"riscv64gc-unknown-linux-gnu",
|
|
111
|
+
"riscv64gc-unknown-linux-musl",
|
|
112
|
+
"s390x-unknown-linux-gnu",
|
|
113
|
+
"wasm32-wasip1-threads",
|
|
114
|
+
"x86_64-apple-darwin",
|
|
115
|
+
"x86_64-pc-windows-msvc",
|
|
116
|
+
"x86_64-unknown-freebsd",
|
|
117
|
+
"x86_64-unknown-linux-gnu",
|
|
118
|
+
"x86_64-unknown-linux-musl"
|
|
119
|
+
],
|
|
120
|
+
"wasm": {
|
|
121
|
+
"browser": {
|
|
122
|
+
"fs": false
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"engines": {
|
|
127
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
128
|
+
}
|
|
129
|
+
}
|