mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
fix(lint): give access to SourceCode in 'deno test' (#28278)
Closes https://github.com/denoland/deno/issues/28273
This commit is contained in:
parent
0fbab02d0f
commit
93a1bb738a
4 changed files with 38 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,10 @@
|
|||
{
|
||||
"args": "test main.ts",
|
||||
"output": "test.out"
|
||||
},
|
||||
{
|
||||
"args": "test main_test.ts",
|
||||
"output": "main_test.out"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
6
tests/specs/lint/lint_plugin_runtime_api/main_test.out
Normal file
6
tests/specs/lint/lint_plugin_runtime_api/main_test.out
Normal 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])
|
||||
|
22
tests/specs/lint/lint_plugin_runtime_api/main_test.ts
Normal file
22
tests/specs/lint/lint_plugin_runtime_api/main_test.ts
Normal 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);`,
|
||||
);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue