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
18 changes: 10 additions & 8 deletions apps/oxlint/src-js/plugins/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ import type { Settings } from "./settings.ts";
import type { SourceCode } from "./source_code.ts";
import type { ModuleKind, Program } from "../generated/types.d.ts";

// Cached current working directory
let cwd: string | null = null;

// Absolute path of file being linted.
// When `null`, indicates that no file is currently being linted (in `createOnce`, or between linting files).
export let filePath: string | null = null;

// Current working directory for file being linted.
// When `null`, indicates that no file is currently being linted (in `createOnce`, or between linting files).
let cwd: string | null = null;

/**
* Set up context for linting a file.
* @param filePathInput - Absolute path of file being linted
* @param cwdInput - Current working directory for file being linted
*/
export function setupFileContext(filePathInput: string): void {
export function setupFileContext(filePathInput: string, cwdInput: string): void {
filePath = filePathInput;
cwd = cwdInput;
}

/**
Expand All @@ -65,6 +68,7 @@ export function setupFileContext(filePathInput: string): void {
*/
export function resetFileContext(): void {
filePath = null;
cwd = null;
}

// ECMAScript version. This matches ESLint's default.
Expand Down Expand Up @@ -363,8 +367,7 @@ const FILE_CONTEXT = Object.freeze({
*/
get cwd(): string {
// Note: If we change this implementation, also change `getCwd` method below
if (filePath === null) throw new Error("Cannot access `context.cwd` in `createOnce`");
if (cwd === null) cwd = process.cwd();
if (cwd === null) throw new Error("Cannot access `context.cwd` in `createOnce`");
return cwd;
},

Expand All @@ -374,8 +377,7 @@ const FILE_CONTEXT = Object.freeze({
* @deprecated Use `context.cwd` property instead.
*/
getCwd(): string {
if (filePath === null) throw new Error("Cannot call `context.getCwd` in `createOnce`");
if (cwd === null) cwd = process.cwd();
if (cwd === null) throw new Error("Cannot call `context.getCwd` in `createOnce`");
return cwd;
},

Expand Down
7 changes: 5 additions & 2 deletions apps/oxlint/src-js/plugins/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { walkProgram, ancestors } from "../generated/walk.js";

import type { AfterHook, BufferWithArrays } from "./types.ts";

// CWD. Currently the same for all files, but that will change in the future.
const CWD = process.cwd();

Comment thread
overlookmotel marked this conversation as resolved.
// Buffers cache.
//
// All buffers sent from Rust are stored in this array, indexed by `bufferId` (also sent from Rust).
Expand Down Expand Up @@ -141,8 +144,8 @@ export function lintFileImpl(
"`ruleIds` and `optionsIds` should be same length",
);

// Pass file path to context module, so `Context`s know what file is being linted
setupFileContext(filePath);
// Pass file path and CWD to context module, so `Context`s know what file is being linted
setupFileContext(filePath, CWD);

// Pass buffer to source code module, so it can decode source text and deserialize AST on demand.
//
Expand Down
11 changes: 7 additions & 4 deletions apps/oxlint/test/isSpaceBetween.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";
import { join as pathJoin } from "node:path";
import { describe, it, expect, beforeEach } from "vitest";
import { parse as parseRaw } from "../src-js/package/parse.ts";
import { setupFileContext, resetFileContext } from "../src-js/plugins/context.ts";
Expand All @@ -18,14 +19,16 @@ import type { Program } from "../src-js/generated/types.d.ts";
/**
* Parse source text into AST using Oxc parser.
* Set up global state, as if was linting the provided file.
* @param path - File path
* @param filename - Filename
* @param sourceText - Source text
* @param options - Parse options
* @returns AST
*/
function parse(path: string, sourceText: string, options?: ParseOptions): Program {
// Set file path
setupFileContext(path);
function parse(filename: string, sourceText: string, options?: ParseOptions): Program {
// Set file path and CWD
const cwd = import.meta.dirname;
const path = pathJoin(cwd, filename);
setupFileContext(path, cwd);

// Parse source, writing source text and AST into buffer
parseRaw(path, sourceText, options);
Expand Down
9 changes: 6 additions & 3 deletions apps/oxlint/test/tokens.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join as pathJoin } from "node:path";
import { beforeEach, describe, expect, it } from "vitest";
import {
getTokens,
Expand Down Expand Up @@ -53,9 +54,11 @@ function setup(sourceText: string) {
resetFileContext();
resetSourceAndAst();

// Set file path
const path = "dummy.js";
setupFileContext(path);
// Set file path and CWD
const filename = "dummy.js";
const cwd = import.meta.dirname;
const path = pathJoin(cwd, filename);
setupFileContext(path, cwd);

// Parse source text into buffer
parseRaw(path, sourceText);
Expand Down
Loading