Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unstable): align js lint context API with eslint #28066

Merged
merged 5 commits into from
Feb 13, 2025
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
13 changes: 11 additions & 2 deletions cli/js/40_lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ export class SourceCode {
*/
export class Context {
id;
fileName;
// ESLint uses lowercase
filename;
sourceCode;

/**
Expand All @@ -287,10 +288,18 @@ export class Context {
*/
constructor(ctx, id, fileName) {
this.id = id;
this.fileName = fileName;
this.filename = fileName;
this.sourceCode = new SourceCode(ctx);
}

getFilename() {
return this.filename;
}

getSourceCode() {
return this.sourceCode;
}

/**
* @param {Deno.lint.ReportData} data
*/
Expand Down
10 changes: 9 additions & 1 deletion cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ declare namespace Deno {
/**
* Name of the file that's currently being linted.
*/
fileName: string;
filename: string;
/**
* Helper methods for working with the raw source code.
*/
Expand All @@ -1437,6 +1437,14 @@ declare namespace Deno {
* Report a lint error.
*/
report(data: ReportData): void;
/**
* @deprecated Use `ctx.filename` instead.
*/
getFilename(): string;
/**
* @deprecated Use `ctx.sourceCode` instead.
*/
getSourceCode(): SourceCode;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/specs/lint/lint_plugin_rule_context/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tempDir": true,
"steps": [
{
"args": "lint main.ts",
"output": "log.out"
}
]
}
5 changes: 5 additions & 0 deletions tests/specs/lint/lint_plugin_rule_context/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lint": {
"plugins": ["./plugin.ts"]
}
}
13 changes: 13 additions & 0 deletions tests/specs/lint/lint_plugin_rule_context/log.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ctx.id:
test-plugin/my-rule

ctx.filename:
main.ts

ctx.getFilename():
main.ts

ctx.getSourceCode():
true

Checked 1 file
2 changes: 2 additions & 0 deletions tests/specs/lint/lint_plugin_rule_context/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const value = "unfixed";
console.log(value);
30 changes: 30 additions & 0 deletions tests/specs/lint/lint_plugin_rule_context/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from "node:path";

export default {
name: "test-plugin",
rules: {
"my-rule": {
create(context) {
return {
VariableDeclarator(node) {
console.log(`ctx.id:`);
console.log(context.id);
console.log();

console.log(`ctx.filename:`);
console.log(path.relative(Deno.cwd(), context.filename));
console.log();

console.log(`ctx.getFilename():`);
console.log(path.relative(Deno.cwd(), context.getFilename()));
console.log();

console.log(`ctx.getSourceCode():`);
console.log(context.getSourceCode() === context.sourceCode);
console.log();
},
};
},
},
},
} satisfies Deno.lint.Plugin;
Loading