Skip to content
Merged
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
19 changes: 14 additions & 5 deletions apps/oxlint/src-js/package/rule_tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

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

// Determine filename.
// Determine path.
// If not provided, use default filename based on `parseOptions.lang`.
let { filename } = test;
let path: string;

const { filename } = test;
if (filename == null) {
let ext: string | undefined = parseOptions.lang;
if (ext == null) {
ext = "js";
} else if (ext === "dts") {
ext = "d.ts";
}
filename = `${DEFAULT_FILENAME_BASE}.${ext}`;
const cwd = dirname(import.meta.dirname); // Root of `oxlint` package once bundled into `dist`
path = pathJoin(cwd, `${DEFAULT_FILENAME_BASE}.${ext}`);
} else if (isAbsolutePath(filename)) {
path = filename;
} else {
const cwd = dirname(import.meta.dirname); // Root of `oxlint` package once bundled into `dist`
path = pathJoin(cwd, filename);
}

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

// Parse file into buffer
parse(filename, test.code, parseOptions);
parse(path, test.code, parseOptions);

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

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

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