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
43 changes: 43 additions & 0 deletions apps/oxlint/src-js/js_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,47 @@ type LoadJsConfigsResult =
| { Failures: { path: string; error: string }[] }
| { Error: string };

function validateConfigExtends(root: object): void {
const visited = new Set<object>();
const stack = new Set<object>();

const visit = (config: object): void => {
if (visited.has(config)) return;
visited.add(config);

if (stack.has(config)) {
// Defensive: this should never happen because we check before recursing.
throw new Error("`extends` contains a circular reference.");
}
stack.add(config);

const maybeExtends = (config as Record<string, unknown>).extends;
if (maybeExtends !== undefined) {
if (!Array.isArray(maybeExtends)) {
throw new Error(
"`extends` must be an array of config objects (strings/paths are not supported).",
);
}
for (let i = 0; i < maybeExtends.length; i++) {
const item = maybeExtends[i];
if (typeof item !== "object" || item === null || Array.isArray(item)) {
throw new Error(
`\`extends[${i}]\` must be a config object (strings/paths are not supported).`,
);
}
if (stack.has(item)) {
throw new Error("`extends` contains a circular reference.");
}
visit(item);
}
}

stack.delete(config);
};

visit(root);
}

/**
* Load JavaScript config files in parallel.
*
Expand Down Expand Up @@ -43,6 +84,8 @@ export async function loadJsConfigs(paths: string[]): Promise<string> {
);
}

validateConfigExtends(config as object);

return { path, config };
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Exit code
1

# stdout
```
Failed to parse oxlint configuration file.

x Failed to load config: <fixture>/oxlint.config.ts
|
| Error: `extends` contains a circular reference.
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "#oxlint";

const a = defineConfig({}) as any;
const b = defineConfig({ extends: [a] }) as any;
a.extends = [b];

export default defineConfig({
extends: [a],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Exit code
1

# stdout
```
Failed to parse oxlint configuration file.

x Failed to load config: <fixture>/oxlint.config.ts
|
| Error: `extends` must be an array of config objects (strings/paths are not supported).
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "#oxlint";

export default defineConfig({
// @ts-expect-error - we are testing invalid config
extends: {},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Exit code
1

# stdout
```
Failed to parse oxlint configuration file.

x Failed to load config: <fixture>/oxlint.config.ts
|
| Error: `extends[0]` must be a config object (strings/paths are not supported).
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "#oxlint";

export default defineConfig({
// @ts-expect-error - we are testing invalid config
extends: ["./base.ts"],
});
Loading