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
89 changes: 64 additions & 25 deletions apps/oxlint/test/lsp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ import { codeFrameColumns } from "@babel/code-frame";

const CLI_PATH = join(import.meta.dirname, "..", "..", "dist", "cli.js");

const PULL_DIAGNOSTICS_CAPABILITY = {
textDocument: {
diagnostic: {},
},
workspace: {
diagnostics: {
refreshSupport: true,
},
},
};

export function createLspConnection() {
const proc = spawn(process.execPath, [CLI_PATH, "--lsp"], {
env: {
Expand Down Expand Up @@ -110,32 +121,64 @@ export async function lintFixture(
languageId: string,
initializationOptions?: OxlintLSPConfig,
): Promise<string> {
const filePath = join(fixturesDir, fixturePath);
const dirPath = dirname(filePath);
const fileUri = pathToFileURL(filePath).href;
const content = await fs.readFile(filePath, "utf-8");
return lintMultiWorkspaceFixture(
fixturesDir,
[{ path: fixturePath, languageId }],
initializationOptions ? [initializationOptions] : undefined,
);
}

export async function lintMultiWorkspaceFixture(
fixturesDir: string,
fixturePaths: {
path: string;
languageId: string;
}[],
initializationOptions?: OxlintLSPConfig[],
): Promise<string> {
const workspaceUris = fixturePaths.map(
({ path }) => pathToFileURL(dirname(join(fixturesDir, path))).href,
);
await using client = createLspConnection();

await client.initialize(
[{ uri: pathToFileURL(dirPath).href, name: "test" }],
{
textDocument: {
diagnostic: {},
},
workspace: {
diagnostics: {
refreshSupport: true,
},
},
},
[
{
workspaceUri: pathToFileURL(dirPath).href,
options: initializationOptions,
},
],
workspaceUris.map((uri, index) => ({ uri, name: `workspace-${index}` })),
PULL_DIAGNOSTICS_CAPABILITY,
workspaceUris.map((workspaceUri, index) => ({
workspaceUri,
options: initializationOptions?.[index] ?? null,
})),
);

const snapshots = [];
for (const fixturePath of fixturePaths) {
snapshots.push(
// oxlint-disable-next-line no-await-in-loop -- for snapshot consistency
await getDiagnosticSnapshot(
fixturePath.path,
join(fixturesDir, fixturePath.path),
fixturePath.languageId,
client,
),
);
}

return snapshots.join("\n\n");
}

// ---

type OxlintLSPConfig = {};

async function getDiagnosticSnapshot(
fixturePath: string,
filePath: string,
languageId: string,
client: ReturnType<typeof createLspConnection>,
): Promise<string> {
const fileUri = pathToFileURL(filePath).href;
const content = await fs.readFile(filePath, "utf-8");

await client.didOpen(fileUri, languageId, content);

const diagnostics = await client.diagnostic(fileUri);
Expand All @@ -149,10 +192,6 @@ ${applyDiagnostics(content, diagnostics).join("\n--------------------")}
`.trim();
}

// ---

type OxlintLSPConfig = {};

function getSeverityLabel(severity: number | undefined): string {
if (!severity) return "Unknown";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`LSP multi workspace linting > basic linting > should handle { path: 'config-default-both/workspace1/test.ts', languageId: 'typescript' } 1`] = `
"--- FILE -----------
config-default-both/workspace1/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Warning: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------

--- FILE -----------
config-default-both/workspace2/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Warning: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------"
`;

exports[`LSP multi workspace linting > basic linting > should handle { path: 'default-both/workspace1/test.ts', languageId: 'typescript' } 1`] = `
"--- FILE -----------
default-both/workspace1/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Warning: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------

--- FILE -----------
default-both/workspace2/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Warning: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------"
`;

exports[`LSP multi workspace linting > basic linting > should handle { path: 'different-js-plugin/workspace1/test.js', languageId: 'javascript' } 1`] = `
"--- FILE -----------
different-js-plugin/workspace1/test.js
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Error: Workspace 1 Plugin
2 |
--------------------

--- FILE -----------
different-js-plugin/workspace2/test.js
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Error: Workspace 2 Plugin
2 |
--------------------"
`;

exports[`LSP multi workspace linting > basic linting > should handle { path: 'different-severity/workspace1/test.ts', languageId: 'typescript' } 1`] = `
"--- FILE -----------
different-severity/workspace1/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Error: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------

--- FILE -----------
different-severity/workspace2/test.ts
--- Diagnostics ---------
> 1 | debugger;
| ^^^^^^^^^ Warning: \`debugger\` statement is not allowed
help: Remove the debugger statement
--------------------"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
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,2 @@
{
}
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 @@
debugger;
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,7 @@
{
"jsPlugins": ["./plugin.js"],
"rules": {
"js-plugin/test-rule": "error",
"no-debugger": "off",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const plugin = {
meta: {
name: 'js-plugin',
},
rules: {
'test-rule': {
create(context) {
return {
DebuggerStatement(debuggerStatement) {
context.report({
message: 'Workspace 1 Plugin',
node: debuggerStatement,
});
},
};
},
},
},
};


export default plugin;
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,7 @@
{
"jsPlugins": ["./plugin.js"],
"rules": {
"js-plugin/test-rule": "error",
"no-debugger": "off",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const plugin = {
meta: {
name: 'js-plugin',
},
rules: {
'test-rule': {
create(context) {
return {
DebuggerStatement(debuggerStatement) {
context.report({
message: 'Workspace 2 Plugin',
node: debuggerStatement,
});
},
};
},
},
},
};


export default plugin;
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,5 @@
{
"rules": {
"no-debugger": "error"
}
}
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,5 @@
{
"rules": {
"no-debugger": "warn"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
30 changes: 30 additions & 0 deletions apps/oxlint/test/lsp/workspaces/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { lintMultiWorkspaceFixture } from "../utils";

const FIXTURES_DIR = join(import.meta.dirname, "fixtures");

describe("LSP multi workspace linting", () => {
describe("basic linting", () => {
it.each([
[
{ path: "default-both/workspace1/test.ts", languageId: "typescript" },
{ path: "default-both/workspace2/test.ts", languageId: "typescript" },
],
[
{ path: "config-default-both/workspace1/test.ts", languageId: "typescript" },
{ path: "config-default-both/workspace2/test.ts", languageId: "typescript" },
],
[
{ path: "different-severity/workspace1/test.ts", languageId: "typescript" },
{ path: "different-severity/workspace2/test.ts", languageId: "typescript" },
],
[
{ path: "different-js-plugin/workspace1/test.js", languageId: "javascript" },
{ path: "different-js-plugin/workspace2/test.js", languageId: "javascript" },
],
])("should handle %s", async (...paths) => {
expect(await lintMultiWorkspaceFixture(FIXTURES_DIR, paths)).toMatchSnapshot();
});
});
});
Loading