|
28 | 28 |
|
29 | 29 | import { getFixes } from './fix.js'; |
30 | 30 | import { getOffsetFromLineColumn } from './location.js'; |
31 | | -import { SOURCE_CODE } from './source_code.js'; |
| 31 | +import { ast, initAst, SOURCE_CODE } from './source_code.js'; |
32 | 32 | import { settings, initSettings } from './settings.js'; |
33 | 33 |
|
34 | 34 | import type { Fix, FixFn } from './fix.ts'; |
35 | 35 | import type { RuleDetails } from './load.ts'; |
36 | 36 | import type { SourceCode } from './source_code.ts'; |
37 | 37 | import type { Location, Ranged } from './types.ts'; |
| 38 | +import type { ModuleKind } from '../generated/types.d.ts'; |
38 | 39 |
|
39 | 40 | const { hasOwn, keys: ObjectKeys, freeze, assign: ObjectAssign, create: ObjectCreate } = Object; |
40 | 41 |
|
@@ -94,6 +95,70 @@ export function resetFileContext(): void { |
94 | 95 | filePath = null; |
95 | 96 | } |
96 | 97 |
|
| 98 | +// ECMAScript version. This matches ESLint's default. |
| 99 | +const ECMA_VERSION = 2026; |
| 100 | + |
| 101 | +// Singleton object for parser options. |
| 102 | +const PARSER_OPTIONS = freeze({ |
| 103 | + /** |
| 104 | + * Source type of the file being linted. |
| 105 | + */ |
| 106 | + get sourceType(): ModuleKind { |
| 107 | + // TODO: Would be better to get `sourceType` without deserializing whole AST, |
| 108 | + // in case it's used in `create` to return an empty visitor if wrong type. |
| 109 | + // TODO: ESLint also has `commonjs` option. |
| 110 | + if (ast === null) initAst(); |
| 111 | + return ast.sourceType; |
| 112 | + }, |
| 113 | +}); |
| 114 | + |
| 115 | +// Singleton object for language options. |
| 116 | +const LANGUAGE_OPTIONS = freeze({ |
| 117 | + /** |
| 118 | + * Source type of the file being linted. |
| 119 | + */ |
| 120 | + get sourceType(): ModuleKind { |
| 121 | + // TODO: Would be better to get `sourceType` without deserializing whole AST, |
| 122 | + // in case it's used in `create` to return an empty visitor if wrong type. |
| 123 | + // TODO: ESLint also has `commonjs` option. |
| 124 | + if (ast === null) initAst(); |
| 125 | + return ast.sourceType; |
| 126 | + }, |
| 127 | + |
| 128 | + /** |
| 129 | + * ECMAScript version of the file being linted. |
| 130 | + */ |
| 131 | + ecmaVersion: ECMA_VERSION, |
| 132 | + |
| 133 | + /** |
| 134 | + * Parser used to parse the file being linted. |
| 135 | + */ |
| 136 | + get parser(): Record<string, unknown> { |
| 137 | + throw new Error('`context.languageOptions.parser` is not implemented yet.'); // TODO |
| 138 | + }, |
| 139 | + |
| 140 | + /** |
| 141 | + * Parser options used to parse the file being linted. |
| 142 | + */ |
| 143 | + parserOptions: PARSER_OPTIONS, |
| 144 | + |
| 145 | + /** |
| 146 | + * Globals defined for the file being linted. |
| 147 | + */ |
| 148 | + // ESLint has `globals` as `null`, not empty object, if no globals are defined. |
| 149 | + get globals(): Record<string, 'readonly' | 'writable' | 'off'> | null { |
| 150 | + // TODO: Get globals from Rust side. |
| 151 | + // Note: ESLint's type is "writable", whereas Oxlint's is "writeable" (misspelled with extra "e"). |
| 152 | + // Probably we should fix that on Rust side (while still allowing "writeable"). |
| 153 | + return null; |
| 154 | + }, |
| 155 | +}); |
| 156 | + |
| 157 | +/** |
| 158 | + * Language options used when parsing a file. |
| 159 | + */ |
| 160 | +export type LanguageOptions = typeof LANGUAGE_OPTIONS; |
| 161 | + |
97 | 162 | // Singleton object for file-specific properties. |
98 | 163 | // |
99 | 164 | // Only one file is linted at a time, so we reuse a single object for all files. |
@@ -140,9 +205,9 @@ const FILE_CONTEXT = freeze({ |
140 | 205 | /** |
141 | 206 | * Language options used when parsing this file. |
142 | 207 | */ |
143 | | - get languageOptions(): Record<string, unknown> { |
| 208 | + get languageOptions(): LanguageOptions { |
144 | 209 | if (filePath === null) throw new Error('Cannot access `context.languageOptions` in `createOnce`'); |
145 | | - throw new Error('`context.languageOptions` is not implemented yet.'); // TODO |
| 210 | + return LANGUAGE_OPTIONS; |
146 | 211 | }, |
147 | 212 |
|
148 | 213 | /** |
|
0 commit comments