diff --git a/cli/js/40_lint.js b/cli/js/40_lint.js index 0eb2f4a1e0..af4ba6ba1e 100644 --- a/cli/js/40_lint.js +++ b/cli/js/40_lint.js @@ -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; } diff --git a/tests/specs/lint/lint_plugin_runtime_api/__test__.jsonc b/tests/specs/lint/lint_plugin_runtime_api/__test__.jsonc index ef871830cd..4e9dcc65cd 100644 --- a/tests/specs/lint/lint_plugin_runtime_api/__test__.jsonc +++ b/tests/specs/lint/lint_plugin_runtime_api/__test__.jsonc @@ -8,6 +8,10 @@ { "args": "test main.ts", "output": "test.out" + }, + { + "args": "test main_test.ts", + "output": "main_test.out" } ] } diff --git a/tests/specs/lint/lint_plugin_runtime_api/main_test.out b/tests/specs/lint/lint_plugin_runtime_api/main_test.out new file mode 100644 index 0000000000..81c9f7efea --- /dev/null +++ b/tests/specs/lint/lint_plugin_runtime_api/main_test.out @@ -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]) + diff --git a/tests/specs/lint/lint_plugin_runtime_api/main_test.ts b/tests/specs/lint/lint_plugin_runtime_api/main_test.ts new file mode 100644 index 0000000000..5a5410d473 --- /dev/null +++ b/tests/specs/lint/lint_plugin_runtime_api/main_test.ts @@ -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);`, + ); +});