Skip to content

Commit b34a155

Browse files
committed
fix(linter/plugins)!: RuleTester set context.filename to absolute path (#18702)
`RuleTester` accepts `filename` as a property of test cases. Previously it passed `filename` directly through to the linter, but that wasn't right - the linter expects it to be passed an absolute path. Instead, convert `filename` to an absolute path, if it isn't one already. Not using `process.cwd()` as the base of the path, as `RuleTester` is a testing utility, and the results should be deterministic, unaffected by external factors like CWD. Use the root of `oxlint` package as the base instead (a bit arbitrary, but what would be better?). This is preamble to #17809. This PR is some of the changes from that one split off, so the diff on #17809 is focused just on that one change.
1 parent 3818486 commit b34a155

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

apps/oxlint/src-js/package/rule_tester.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import { default as assert, AssertionError } from "node:assert";
11+
import { join as pathJoin, isAbsolute as isAbsolutePath, dirname } from "node:path";
1112
import util from "node:util";
1213
import stableJsonStringify from "json-stable-stringify-without-jsonify";
1314
import { ecmaFeaturesOverride, setEcmaVersion, ECMA_VERSION } from "../plugins/context.ts";
@@ -974,17 +975,25 @@ function lint(test: TestCase, plugin: Plugin): Diagnostic[] {
974975
// Get parse options
975976
const parseOptions = getParseOptions(test);
976977

977-
// Determine filename.
978+
// Determine path.
978979
// If not provided, use default filename based on `parseOptions.lang`.
979-
let { filename } = test;
980+
let path: string;
981+
982+
const { filename } = test;
980983
if (filename == null) {
981984
let ext: string | undefined = parseOptions.lang;
982985
if (ext == null) {
983986
ext = "js";
984987
} else if (ext === "dts") {
985988
ext = "d.ts";
986989
}
987-
filename = `${DEFAULT_FILENAME_BASE}.${ext}`;
990+
const cwd = dirname(import.meta.dirname); // Root of `oxlint` package once bundled into `dist`
991+
path = pathJoin(cwd, `${DEFAULT_FILENAME_BASE}.${ext}`);
992+
} else if (isAbsolutePath(filename)) {
993+
path = filename;
994+
} else {
995+
const cwd = dirname(import.meta.dirname); // Root of `oxlint` package once bundled into `dist`
996+
path = pathJoin(cwd, filename);
988997
}
989998

990999
try {
@@ -995,7 +1004,7 @@ function lint(test: TestCase, plugin: Plugin): Diagnostic[] {
9951004
const optionsId = setupOptions(test);
9961005

9971006
// Parse file into buffer
998-
parse(filename, test.code, parseOptions);
1007+
parse(path, test.code, parseOptions);
9991008

10001009
// In conformance tests, set `context.languageOptions.ecmaVersion`.
10011010
// This is not supported outside of conformance tests.
@@ -1007,7 +1016,7 @@ function lint(test: TestCase, plugin: Plugin): Diagnostic[] {
10071016

10081017
// Lint file.
10091018
// Buffer is stored already, at index 0. No need to pass it.
1010-
lintFileImpl(filename, 0, null, [0], [optionsId], settingsJSON, globalsJSON);
1019+
lintFileImpl(path, 0, null, [0], [optionsId], settingsJSON, globalsJSON);
10111020

10121021
// Return diagnostics
10131022
const ruleId = `${plugin.meta!.name!}/${Object.keys(plugin.rules)[0]}`;

0 commit comments

Comments
 (0)