Skip to content

Commit 22da839

Browse files
committed
feat(linter/plugins): implement languageOptions.parser
1 parent 6a641f9 commit 22da839

File tree

3 files changed

+865
-5
lines changed

3 files changed

+865
-5
lines changed

apps/oxlint/src-js/plugins/context.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
import { ast, initAst, SOURCE_CODE } from "./source_code.js";
3030
import { report } from "./report.js";
3131
import { settings, initSettings } from "./settings.js";
32+
import visitorKeys from "../generated/keys.js";
3233
import { debugAssertIsNonNull } from "../utils/asserts.js";
3334

3435
import type { RuleDetails } from "./load.ts";
3536
import type { Options } from "./options.ts";
3637
import type { Diagnostic } from "./report.ts";
3738
import type { Settings } from "./settings.ts";
3839
import type { SourceCode } from "./source_code.ts";
39-
import type { ModuleKind } from "../generated/types.d.ts";
40+
import type { ModuleKind, Program } from "../generated/types.d.ts";
4041

4142
const { freeze, assign: ObjectAssign, create: ObjectCreate } = Object;
4243

@@ -68,6 +69,68 @@ export function resetFileContext(): void {
6869

6970
// ECMAScript version. This matches ESLint's default.
7071
const ECMA_VERSION = 2026;
72+
const ECMA_VERSION_NUMBER = 17;
73+
74+
// Supported ECMAScript versions. This matches ESLint's default.
75+
const SUPPORTED_ECMA_VERSIONS = freeze([3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]);
76+
77+
// Singleton object for parser's `Syntax` property. Generated lazily.
78+
let Syntax: Record<string, string> | null = null;
79+
80+
// Singleton object for parser.
81+
const PARSER = freeze({
82+
/**
83+
* Parser name.
84+
*/
85+
name: "oxc",
86+
87+
/**
88+
* Parser version.
89+
*/
90+
// TODO: This can be statically defined, but need it be to be updated when we make a new release.
91+
version: "0.0.0",
92+
93+
/**
94+
* Parse code into an AST.
95+
* @param code - Code to parse
96+
* @param options? - Parser options
97+
* @returns AST
98+
*/
99+
// oxlint-disable-next-line no-unused-vars
100+
parse(code: string, options?: Record<string, unknown>): Program {
101+
throw new Error("`context.languageOptions.parser.parse` not implemented yet."); // TODO
102+
},
103+
104+
/**
105+
* Visitor keys for AST nodes.
106+
*/
107+
VisitorKeys: visitorKeys,
108+
109+
/**
110+
* Ast node types.
111+
*/
112+
get Syntax(): Readonly<Record<string, string>> {
113+
// Construct lazily, as it's probably rarely used
114+
if (Syntax === null) {
115+
Syntax = ObjectCreate(null);
116+
for (const key in visitorKeys) {
117+
Syntax![key] = key;
118+
}
119+
freeze(Syntax);
120+
}
121+
return Syntax!;
122+
},
123+
124+
/**
125+
* Latest ECMAScript version supported by parser.
126+
*/
127+
latestEcmaVersion: ECMA_VERSION_NUMBER,
128+
129+
/**
130+
* ECMAScript versions supported by parser.
131+
*/
132+
supportedEcmaVersions: SUPPORTED_ECMA_VERSIONS,
133+
});
71134

72135
// Singleton object for parser options.
73136
// TODO: `sourceType` is the only property ESLint provides. But does TS-ESLint provide any further properties?
@@ -109,9 +172,7 @@ const LANGUAGE_OPTIONS = freeze({
109172
/**
110173
* Parser used to parse the file being linted.
111174
*/
112-
get parser(): Record<string, unknown> {
113-
throw new Error("`context.languageOptions.parser` not implemented yet."); // TODO
114-
},
175+
parser: PARSER,
115176

116177
/**
117178
* Parser options used to parse the file being linted.

0 commit comments

Comments
 (0)