Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apps/oxlint/src-js/plugins/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* and global variables (`filePath`, `settings`, `cwd`).
*/

import { ast, initAst, SOURCE_CODE } from "./source_code.ts";
import { ast, initAst, fileIsJsx, SOURCE_CODE } from "./source_code.ts";
import { report } from "./report.ts";
import { settings, initSettings } from "./settings.ts";
import visitorKeys from "../generated/keys.ts";
Expand Down Expand Up @@ -143,6 +143,13 @@ export const ecmaFeaturesOverride: {

// Singleton object for ECMA features.
const ECMA_FEATURES = Object.freeze({
/**
* `true` if file was parsed as JSX.
*/
get jsx(): boolean {
return fileIsJsx();
},

/**
* `true` if file was parsed with top-level `return` statements allowed.
*/
Expand Down
24 changes: 23 additions & 1 deletion apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
DATA_POINTER_POS_32,
SOURCE_START_OFFSET,
SOURCE_LEN_OFFSET,
IS_JSX_FLAG_POS,
IS_TS_FLAG_POS,
} from "../generated/constants.ts";

// We use the deserializer which removes `ParenthesizedExpression`s from AST,
Expand Down Expand Up @@ -29,7 +31,7 @@ import type { ScopeManager } from "./scope.ts";
const textDecoder = new TextDecoder("utf-8", { ignoreBOM: true });

// Buffer containing AST. Set before linting a file by `setupSourceForFile`.
export let buffer: BufferWithArrays | null = null;
let buffer: BufferWithArrays | null = null;

// Indicates if the original source text has a BOM. Set before linting a file by `setupSourceForFile`.
let hasBOM = false;
Expand Down Expand Up @@ -105,6 +107,26 @@ export function resetSourceAndAst(): void {
resetTokens();
}

/**
* Get whether file is JSX.
* @returns `true` if file is JSX, `false` if not
*/
export function fileIsJsx(): boolean {
debugAssertIsNonNull(buffer);
// Flag is `bool` in Rust, so 0 = false, 1 = true
return buffer[IS_JSX_FLAG_POS] === 1;
}

/**
* Get whether file is TypeScript.
* @returns `true` if file is TypeScript, `false` if not
*/
export function fileIsTs(): boolean {
debugAssertIsNonNull(buffer);
// Flag is `bool` in Rust, so 0 = false, 1 = true
return buffer[IS_TS_FLAG_POS] === 1;
}

// `SourceCode` object.
//
// Only one file is linted at a time, so we can reuse a single object for all files.
Expand Down
16 changes: 5 additions & 11 deletions apps/oxlint/src-js/plugins/tokens_parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import { createRequire } from "node:module";
import { filePath } from "./context.ts";
import { getNodeLoc } from "./location.ts";
import { buffer, sourceText } from "./source_code.ts";
import { IS_JSX_FLAG_POS, IS_TS_FLAG_POS } from "../generated/constants.ts";
import { sourceText, fileIsJsx, fileIsTs } from "./source_code.ts";
import { debugAssert, debugAssertIsNonNull } from "../utils/asserts.ts";

import type * as ts from "typescript";
Expand Down Expand Up @@ -41,7 +40,6 @@ const TokenProto = Object.create(Object.prototype, {
export function parseTokens(): Token[] {
debugAssertIsNonNull(filePath);
debugAssertIsNonNull(sourceText);
debugAssertIsNonNull(buffer);

// Lazy-load TypeScript.
// `./typescript.cjs` is path to the bundle in `dist` directory, as well as relative path in `src-js`,
Expand All @@ -53,16 +51,12 @@ export function parseTokens(): Token[] {
tsSyntaxKind = tsModule.SyntaxKind;
}

// Get source type from flags in buffer.
// Both flags are `bool`s in Rust, so 0 = false, 1 = true.
const isTs = buffer[IS_TS_FLAG_POS] === 1,
isJsx = buffer[IS_JSX_FLAG_POS] === 1;

const scriptKind = isTs
? isJsx
// Determine language from flags in buffer
const scriptKind = fileIsTs()
? fileIsJsx()
? tsModule.ScriptKind.TSX
: tsModule.ScriptKind.TS
: isJsx
: fileIsJsx()
? tsModule.ScriptKind.JSX
: tsModule.ScriptKind.JS;

Expand Down
1 change: 1 addition & 0 deletions apps/oxlint/test/fixtures/languageOptions/files/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x;
21 changes: 16 additions & 5 deletions apps/oxlint/test/fixtures/languageOptions/output.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
x language-options-plugin(lang): languageOptions:
| sourceType: commonjs
| ecmaVersion: 2026
| parserOptions: {"sourceType":"commonjs","ecmaFeatures":{"globalReturn":true,"impliedStrict":false}}
| parserOptions: {"sourceType":"commonjs","ecmaFeatures":{"jsx":true,"globalReturn":true,"impliedStrict":false}}
| globals: {}
| env: {"builtin":true}
,-[files/index.cjs:1:1]
Expand Down Expand Up @@ -794,7 +794,7 @@
x language-options-plugin(lang): languageOptions:
| sourceType: script
| ecmaVersion: 2026
| parserOptions: {"sourceType":"script","ecmaFeatures":{"globalReturn":false,"impliedStrict":false}}
| parserOptions: {"sourceType":"script","ecmaFeatures":{"jsx":true,"globalReturn":false,"impliedStrict":false}}
| globals: {}
| env: {"builtin":true}
,-[files/index.js:1:1]
Expand All @@ -805,16 +805,27 @@
x language-options-plugin(lang): languageOptions:
| sourceType: module
| ecmaVersion: 2026
| parserOptions: {"sourceType":"module","ecmaFeatures":{"globalReturn":false,"impliedStrict":true}}
| parserOptions: {"sourceType":"module","ecmaFeatures":{"jsx":true,"globalReturn":false,"impliedStrict":true}}
| globals: {}
| env: {"builtin":true}
,-[files/index.mjs:1:1]
1 | let x;
: ^
`----

Found 0 warnings and 4 errors.
Finished in Xms on 3 files with 1 rules using X threads.
x language-options-plugin(lang): languageOptions:
| sourceType: script
| ecmaVersion: 2026
| parserOptions: {"sourceType":"script","ecmaFeatures":{"jsx":false,"globalReturn":false,"impliedStrict":false}}
| globals: {}
| env: {"builtin":true}
,-[files/index.ts:1:1]
1 | let x;
: ^
`----

Found 0 warnings and 5 errors.
Finished in Xms on 4 files with 1 rules using X threads.
```

# stderr
Expand Down
Loading