Skip to content

Commit 54ec8e3

Browse files
magic-akaricamc314
andauthored
feat(linter): add cwd property to JS plugin Context (#14814)
- Closes: #14777 --------- Co-authored-by: Cameron Clark <cameron.clark@hey.com>
1 parent 983c31c commit 54ec8e3

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ export interface InternalContext {
8989
messages: Record<string, string> | null;
9090
}
9191

92+
// Cached current working directory.
93+
let cachedCwd: string | null = null;
94+
9295
/**
9396
* Context class.
9497
*
@@ -132,6 +135,12 @@ export class Context {
132135
return getInternal(this, 'access `context.physicalFilename`').filePath;
133136
}
134137

138+
// Getter for current working directory.
139+
get cwd() {
140+
getInternal(this, 'access `context.cwd`');
141+
return cachedCwd ??= process.cwd();
142+
}
143+
135144
// Getter for options for file being linted.
136145
get options() {
137146
return getInternal(this, 'access `context.options`').options;

apps/oxlint/test/fixtures/context_properties/plugin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ const rule: Rule = {
3535
node: SPAN,
3636
});
3737

38+
if (context.cwd.length === 0) {
39+
context.report({ message: 'cwd.length === 0', node: SPAN });
40+
}
41+
42+
if (context.cwd !== process.cwd()) {
43+
context.report({ message: 'cwd !== process.cwd()', node: SPAN });
44+
}
45+
3846
if (this !== rule) context.report({ message: 'this !== rule', node: SPAN });
3947

4048
return {

0 commit comments

Comments
 (0)