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
10 changes: 8 additions & 2 deletions apps/oxlint/src-js/package/rule_tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ interface EcmaFeatures {
*/
type Language = "js" | "jsx" | "ts" | "tsx" | "dts";

// Empty language options
const EMPTY_LANGUAGE_OPTIONS: LanguageOptionsInternal = {};

// `RuleTester` uses this config as its default. Can be overwritten via `RuleTester.setDefaultConfig()`.
let sharedConfig: Config = {};

Expand Down Expand Up @@ -1027,8 +1030,8 @@ function lint(test: TestCase, plugin: Plugin): Diagnostic[] {
function getParseOptions(test: TestCase): ParseOptions {
const parseOptions: ParseOptions = {};

const languageOptions = test.languageOptions as LanguageOptionsInternal | undefined;
if (languageOptions == null) return parseOptions;
let languageOptions = test.languageOptions as LanguageOptionsInternal | undefined;
if (languageOptions == null) languageOptions = EMPTY_LANGUAGE_OPTIONS;

// Throw error if custom parser is provided
if (languageOptions.parser != null) throw new Error("Custom parsers are not supported");
Expand Down Expand Up @@ -1059,6 +1062,9 @@ function getParseOptions(test: TestCase): ParseOptions {
}

parseOptions.sourceType = sourceType;
} else if (test.eslintCompat === true) {
// ESLint defaults to `module` if no source type is specified
parseOptions.sourceType = "module";
}

// Handle `languageOptions.parserOptions`
Expand Down
66 changes: 53 additions & 13 deletions apps/oxlint/test/rule_tester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -941,21 +941,61 @@ describe("RuleTester", () => {

describe("parsing options", () => {
describe("sourceType", () => {
it("default (unambiguous)", () => {
const tester = new RuleTester();
tester.run("no-foo", simpleRule, {
// with (obj) {} - no ESM syntax, parsed as script, `with` is allowed
// import x from 'foo'; - has ESM syntax, parsed as module
valid: ["with (obj) {}", "import x from 'foo';"],
invalid: [],
describe("default", () => {
const reportSourceTypeRule: Rule = {
create(context) {
return {
Program(node) {
context.report({
message: `sourceType: ${context.languageOptions.sourceType}`,
node,
});
},
};
},
};

it("unambiguous without ESLint compatibility mode", () => {
const tester = new RuleTester();
tester.run("source-type", reportSourceTypeRule, {
valid: [],
invalid: [
// No ESM syntax, parsed as script, so `with` is allowed
{
code: "with (obj) {}",
errors: ["sourceType: script"],
},
// Has ESM syntax, parsed as module
{
code: "import x from 'foo';",
errors: ["sourceType: module"],
},
],
});
expect(runCases()).toEqual([null, null]);
});

expect(runCases()).toMatchInlineSnapshot(`
[
null,
null,
]
`);
it("module with ESLint compatibility mode", () => {
const tester = new RuleTester({ eslintCompat: true });
tester.run("source-type", reportSourceTypeRule, {
// Parsed as module, `with` is not allowed, so parse error
valid: ["with (obj) {}"],
// Has ESM syntax, successfully parsed as module
invalid: [
{
code: "import x from 'foo';",
errors: ["sourceType: module"],
},
],
});

expect(runCases()).toMatchInlineSnapshot(`
[
[Error: Parsing failed],
null,
]
`);
});
});

describe("module", () => {
Expand Down
Loading