Skip to content

Commit 610fa13

Browse files
committed
refactor(linter/plugins): make Context properties getters
1 parent 3ec550b commit 610fa13

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

apps/oxlint/src-js/plugins/context.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export const diagnostics: DiagnosticReport[] = [];
2121
/**
2222
* Update a `Context` with file-specific data.
2323
*
24-
* We have to define this function within class body, as it's not possible to set private property
25-
* `#ruleIndex` from outside the class.
24+
* We have to define this function within class body, as it's not possible to access private property
25+
* `#internal` from outside the class.
2626
* We don't use a normal class method, because we don't want to expose this to user.
2727
*
2828
* @param context - `Context` object
@@ -35,27 +35,53 @@ export let setupContextForFile: (
3535
filePath: string,
3636
) => void;
3737

38+
// Internal data within `Context` that don't want to expose to plugins.
39+
// Stored as `#internal` property of `Context`.
40+
interface InternalContext {
41+
// Full rule name, including plugin name e.g. `my-plugin/my-rule`.
42+
id: string;
43+
// Index into `ruleIds` sent from Rust
44+
ruleIndex: number;
45+
// Absolute path of file being linted
46+
filePath: string;
47+
}
48+
3849
/**
3950
* Context class.
4051
*
4152
* Each rule has its own `Context` object. It is passed to that rule's `create` function.
4253
*/
4354
export class Context {
44-
// Full rule name, including plugin name e.g. `my-plugin/my-rule`.
45-
id: string;
46-
// Index into `ruleIds` sent from Rust. Set before calling `rule`'s `create` method.
47-
#ruleIndex: number;
48-
// Absolute path of file being linted. Set before calling `rule`'s `create` method.
49-
filename: string;
50-
// Absolute path of file being linted. Set before calling `rule`'s `create` method.
51-
physicalFilename: string;
55+
// Internal data.
56+
// Initialized in constructor, updated by `setupContextForFile` before running visitor on file.
57+
#internal: InternalContext;
5258

5359
/**
5460
* @class
5561
* @param fullRuleName - Rule name, in form `<plugin>/<rule>`
5662
*/
5763
constructor(fullRuleName: string) {
58-
this.id = fullRuleName;
64+
this.#internal = {
65+
id: fullRuleName,
66+
filePath: '',
67+
ruleIndex: 0,
68+
};
69+
}
70+
71+
// Getter for full rule name, in form `<plugin>/<rule>`
72+
get id() {
73+
return this.#internal.id;
74+
}
75+
76+
// Getter for absolute path of file being linted.
77+
get filename() {
78+
return this.#internal.filePath;
79+
}
80+
81+
// Getter for absolute path of file being linted.
82+
// TODO: Unclear how this differs from `filename`.
83+
get physicalFilename() {
84+
return this.#internal.filePath;
5985
}
6086

6187
/**
@@ -66,15 +92,15 @@ export class Context {
6692
diagnostics.push({
6793
message: diagnostic.message,
6894
loc: { start: diagnostic.node.start, end: diagnostic.node.end },
69-
ruleIndex: this.#ruleIndex,
95+
ruleIndex: this.#internal.ruleIndex,
7096
});
7197
}
7298

7399
static {
74100
setupContextForFile = (context, ruleIndex, filePath) => {
75-
context.#ruleIndex = ruleIndex;
76-
context.filename = filePath;
77-
context.physicalFilename = filePath;
101+
const internal = context.#internal;
102+
internal.ruleIndex = ruleIndex;
103+
internal.filePath = filePath;
78104
};
79105
}
80106
}

0 commit comments

Comments
 (0)