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
1 change: 1 addition & 0 deletions apps/oxlint/src-js/generated/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const BUFFER_SIZE = 2147483616;
export const BUFFER_ALIGN = 4294967296;
export const DATA_POINTER_POS_32 = 536870902;
export const IS_TS_FLAG_POS = 2147483612;
export const IS_JSX_FLAG_POS = 2147483613;
export const PROGRAM_OFFSET = 0;
export const SOURCE_LEN_OFFSET = 16;
2 changes: 1 addition & 1 deletion apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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`.
let buffer: BufferWithArrays | null = null;
export 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
22 changes: 18 additions & 4 deletions apps/oxlint/src-js/plugins/tokens_parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import { createRequire } from "node:module";
import { filePath } from "./context.ts";
import { getNodeLoc } from "./location.ts";
import { sourceText } from "./source_code.ts";
import { buffer, sourceText } from "./source_code.ts";
import { IS_JSX_FLAG_POS, IS_TS_FLAG_POS } from "../generated/constants.ts";
import { debugAssert, debugAssertIsNonNull } from "../utils/asserts.ts";

import type * as ts from "typescript";
Expand Down Expand Up @@ -35,11 +36,12 @@ const TokenProto = Object.create(Object.prototype, {
/**
* Initialize TS-ESLint tokens for current file.
*
* Caller must ensure `filePath` and `sourceText` are initialized before calling this function.
* Caller must ensure `filePath`, `sourceText`, and `buffer` are initialized before calling this function.
*/
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 @@ -51,6 +53,19 @@ 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
? tsModule.ScriptKind.TSX
: tsModule.ScriptKind.TS
: isJsx
? tsModule.ScriptKind.JSX
: tsModule.ScriptKind.JS;

// Parse source text into TypeScript AST
const tsAst = tsModule.createSourceFile(
filePath,
Expand All @@ -62,8 +77,7 @@ export function parseTokens(): Token[] {
setExternalModuleIndicator: undefined,
},
true, // `setParentNodes`
// TODO: Use `TS` or `TSX` depending on source type
tsModule.ScriptKind.TSX,
scriptKind,
);

// Check that TypeScript hasn't altered source text.
Expand Down
3 changes: 2 additions & 1 deletion apps/oxlint/src/js_plugins/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ unsafe fn parse_raw_impl(

// Write metadata into end of buffer
#[allow(clippy::cast_possible_truncation)]
let metadata = RawTransferMetadata::new(program_offset);
let metadata =
RawTransferMetadata::new(program_offset, source_type.is_typescript(), source_type.is_jsx());
const RAW_METADATA_OFFSET: usize = BUFFER_SIZE - RAW_METADATA_SIZE;
const _: () = assert!(RAW_METADATA_OFFSET.is_multiple_of(BUMP_ALIGN));
// SAFETY: `RAW_METADATA_OFFSET` is less than length of `buffer`.
Expand Down
8 changes: 8 additions & 0 deletions apps/oxlint/test/fixtures/tokens/files/generic_arrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const obj = {
fn: <T>(arg: T): T => {
return arg;
},
};

// A comment after the object
export { obj };
6 changes: 6 additions & 0 deletions apps/oxlint/test/fixtures/tokens/files/jsx_element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Component = () => {
return <div className="test">Hello</div>;
};

// A comment after the component
export { Component };
Loading
Loading