Skip to content

Commit

Permalink
feat: oxc parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 31, 2024
1 parent f7be112 commit 9c4e4ad
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
38 changes: 37 additions & 1 deletion composables/language/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type * as Babel from '@babel/parser'
import type * as Swc from '@swc/wasm-web'
import type * as Acorn from 'acorn'
import type * as Ts from 'typescript'
import type * as Oxc from '@oxc-parser/wasm'

// @unocss-include

Expand Down Expand Up @@ -108,6 +109,41 @@ const swc: Parser<typeof Swc, Swc.ParseOptions> = {
getAstLocation: getAstLocation.bind(null, 'swc'),
}

const oxc: Parser<typeof Oxc, Partial<Oxc.ParserOptions>> = {
id: 'oxc',
label: 'oxc',
icon: 'i-vscode-icons:file-type-js-official',
options: {
configurable: true,
defaultValue: {
sourceType: 'module',
sourceFilename: 'foo.ts',
},
editorLanguage: 'json',
},
init: () =>
import(
// @ts-expect-error
'https://cdn.jsdelivr.net/npm/@oxc-parser/wasm@latest/oxc_parser_wasm.js'
).then(async (mod: typeof Oxc) => {
// debugger
await mod.default()
return mod
}),
version: () =>
fetch('https://cdn.jsdelivr.net/npm/@oxc-parser/wasm@latest/package.json')
.then((r) => r.json())
.then((raw) => `@swc/wasm-web@${raw.version}`),
parse(code, options) {
const { program, errors } = this.parseSync(code, { ...options })
return { program, errors }
},
editorLanguage(options) {
return options.sourceFilename?.endsWith('.ts') ? 'typescript' : 'javascript'
},
getAstLocation: getAstLocation.bind(null, 'babel'),
}

const acorn: Parser<typeof Acorn, Acorn.Options> = {
id: 'acorn',
label: 'Acorn',
Expand Down Expand Up @@ -211,5 +247,5 @@ const tsEslint: Parser<typeof TsEslint, TsEslint.ParserOptions> = {
export const javascript: LanguageOption = {
label: 'JavaScript',
icon: 'i-vscode-icons:file-type-js-official',
parsers: [babel, swc, acorn, ts, espree, tsEslint],
parsers: [babel, swc, oxc, acorn, ts, espree, tsEslint],
}
2 changes: 1 addition & 1 deletion composables/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const showRightLayout = useLocalStorage('show-right-layout', true)
export const hideEmptyKeys = useLocalStorage(`${PREFIX}hide-empty-keys`, true)
export const hideLocationData = useLocalStorage(
`${PREFIX}hide-location-data`,
true,
false,
)
export const hideKeys = useLocalStorage<string[]>(`${PREFIX}hide-keys`, [])
export const autoFocus = useLocalStorage<boolean>(`${PREFIX}auto-focus`, true)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@babel/parser": "^7.23.9",
"@iconify-json/ri": "^1.1.19",
"@iconify-json/vscode-icons": "^1.1.33",
"@oxc-parser/wasm": "^0.0.4",
"@swc/wasm-web": "1.3.107",
"@sxzz/eslint-config": "^3.7.6",
"@sxzz/prettier-config": "^2.0.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c4e4ad

Please sign in to comment.