fix(lint): give access to SourceCode in 'deno test' (#28278)

Closes https://github.com/denoland/deno/issues/28273
This commit is contained in:
Bartek Iwańczuk 2025-02-24 17:21:16 +01:00 committed by GitHub
parent 0fbab02d0f
commit 93a1bb738a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 1 deletions

View file

@ -17,6 +17,7 @@ const {
} = core.ops;
let doReport = op_lint_report;
let doGetSource = op_lint_get_source;
// Keep these in sync with Rust
const AST_IDX_INVALID = 0;
@ -264,7 +265,7 @@ export class SourceCode {
*/
#getSource() {
if (this.#source === null) {
this.#source = op_lint_get_source();
this.#source = doGetSource();
}
return /** @type {string} */ (this.#source);
}
@ -1365,6 +1366,9 @@ function runLintPlugin(plugin, fileName, sourceText) {
fix,
});
};
doGetSource = () => {
return sourceText;
};
try {
const serializedAst = op_lint_create_serialized_ast(fileName, sourceText);
@ -1373,6 +1377,7 @@ function runLintPlugin(plugin, fileName, sourceText) {
resetState();
}
doReport = op_lint_report;
doGetSource = op_lint_get_source;
return diagnostics;
}

View file

@ -8,6 +8,10 @@
{
"args": "test main.ts",
"output": "test.out"
},
{
"args": "test main_test.ts",
"output": "main_test.out"
}
]
}

View file

@ -0,0 +1,6 @@
Check [WILDCARD]main_test.ts
running 1 test from ./main_test.ts
SourceCode.text ... ok ([WILDCARD])
ok | 1 passed | 0 failed ([WILDCARD])

View file

@ -0,0 +1,22 @@
Deno.test("SourceCode.text", () => {
const plugin: Deno.lint.Plugin = {
name: "sample",
rules: {
"test": {
create: (ctx) => {
ctx.sourceCode.text;
return {};
},
},
},
};
Deno.lint.runPlugin(
plugin,
"./test.js",
`function add(a, b) {
return a + b;
}
add(1, 2);`,
);
});