Skip to content

Commit 0df4d4f

Browse files
snitin315fasttime
andauthored
feat: add cwd to rule context (#17106)
* feat: add `cwd` to rule context * docs: add more context.cwd references * tests: add asertions for context.getCwd() * docs: apply suggestions from code review Co-authored-by: Francesco Trotta <github@fasttime.org> * chore: update test assestion for context.getCwd() --------- Co-authored-by: Francesco Trotta <github@fasttime.org>
1 parent 52018f2 commit 0df4d4f

4 files changed

Lines changed: 96 additions & 6 deletions

File tree

docs/src/extend/custom-rules.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ The `context` object has the following properties:
125125

126126
* `id`: (`string`) The rule ID.
127127
* `filename`: (`string`) The filename associated with the source.
128+
* `cwd`: (`string`) The `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
128129
* `options`: (`array`) An array of the [configured options](../use/configure/rules) for this rule. This array does not include the rule severity (see the [dedicated section](#accessing-options-passed-to-a-rule)).
129130
* `settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
130131
* `parserPath`: (`string`) The name of the `parser` from the configuration.
@@ -134,7 +135,7 @@ The `context` object has the following properties:
134135
Additionally, the `context` object has the following methods:
135136

136137
* `getAncestors()`: (**Deprecated:** Use `SourceCode#getAncestors(node)` instead.) Returns an array of the ancestors of the currently-traversed node, starting at the root of the AST and continuing through the direct parent of the current node. This array does not include the currently-traversed node itself.
137-
* `getCwd()`: Returns the `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
138+
* `getCwd()`: (**Deprecated:** Use `context.cwd` instead.) Returns the `cwd` option passed to the [Linter](../integrate/nodejs-api#linter). It is a path to a directory that should be considered the current working directory.
138139
* `getDeclaredVariables(node)`: (**Deprecated:** Use `SourceCode#getDeclaredVariables(node)` instead.) Returns a list of [variables](./scope-manager-interface#variable-interface) declared by the given node. This information can be used to track references to variables.
139140
* If the node is a `VariableDeclaration`, all variables declared in the declaration are returned.
140141
* If the node is a `VariableDeclarator`, all variables declared in the declarator are returned.

docs/src/integrate/nodejs-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ The `Linter` object does the actual evaluation of the JavaScript code. It doesn'
510510

511511
The `Linter` is a constructor, and you can create a new instance by passing in the options you want to use. The available options are:
512512

513-
* `cwd` - Path to a directory that should be considered as the current working directory. It is accessible to rules by calling `context.getCwd()` (see [The Context Object](../extend/custom-rules#the-context-object)). If `cwd` is `undefined`, it will be normalized to `process.cwd()` if the global `process` object is defined (for example, in the Node.js runtime) , or `undefined` otherwise.
513+
* `cwd` - Path to a directory that should be considered as the current working directory. It is accessible to rules from `context.cwd` or by calling `context.getCwd()` (see [The Context Object](../extend/custom-rules#the-context-object)). If `cwd` is `undefined`, it will be normalized to `process.cwd()` if the global `process` object is defined (for example, in the Node.js runtime) , or `undefined` otherwise.
514514

515515
For example:
516516

@@ -520,7 +520,7 @@ const linter1 = new Linter({ cwd: 'path/to/project' });
520520
const linter2 = new Linter();
521521
```
522522

523-
In this example, rules run on `linter1` will get `path/to/project` when calling `context.getCwd()`.
523+
In this example, rules run on `linter1` will get `path/to/project` from `context.cwd` or when calling `context.getCwd()`.
524524
Those run on `linter2` will get `process.cwd()` if the global `process` object is defined or `undefined` otherwise (e.g. on the browser <https://eslint.org/demo>).
525525

526526
### Linter#verify

lib/linter/linter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserName, languageO
951951
getAncestors: () => sourceCode.getAncestors(currentNode),
952952
getDeclaredVariables: node => sourceCode.getDeclaredVariables(node),
953953
getCwd: () => cwd,
954+
cwd,
954955
getFilename: () => filename,
955956
filename,
956957
getPhysicalFilename: () => physicalFilename || filename,

tests/lib/linter/linter.js

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,8 @@ var a = "test2";
39473947
linterWithOption.defineRule("checker", {
39483948
create(context) {
39493949
spy = sinon.spy(() => {
3950-
assert.strictEqual(context.getCwd(), cwd);
3950+
assert.strictEqual(context.getCwd(), context.cwd);
3951+
assert.strictEqual(context.cwd, cwd);
39513952
});
39523953
return { Program: spy };
39533954
}
@@ -3965,7 +3966,8 @@ var a = "test2";
39653966
create(context) {
39663967

39673968
spy = sinon.spy(() => {
3968-
assert.strictEqual(context.getCwd(), process.cwd());
3969+
assert.strictEqual(context.getCwd(), context.cwd);
3970+
assert.strictEqual(context.cwd, process.cwd());
39693971
});
39703972
return { Program: spy };
39713973
}
@@ -3982,7 +3984,8 @@ var a = "test2";
39823984
create(context) {
39833985

39843986
spy = sinon.spy(() => {
3985-
assert.strictEqual(context.getCwd(), process.cwd());
3987+
assert.strictEqual(context.getCwd(), context.cwd);
3988+
assert.strictEqual(context.cwd, process.cwd());
39863989
});
39873990
return { Program: spy };
39883991
}
@@ -11020,6 +11023,91 @@ describe("Linter with FlatConfigArray", () => {
1102011023
});
1102111024
});
1102211025

11026+
describe("context.cwd", () => {
11027+
const code = "a;\nb;";
11028+
const baseConfig = { rules: { "test/checker": "error" } };
11029+
11030+
it("should get cwd correctly in the context", () => {
11031+
const cwd = "cwd";
11032+
const linterWithOption = new Linter({ cwd, configType: "flat" });
11033+
let spy;
11034+
const config = {
11035+
plugins: {
11036+
test: {
11037+
rules: {
11038+
checker: {
11039+
create(context) {
11040+
spy = sinon.spy(() => {
11041+
assert.strictEqual(context.cwd, cwd);
11042+
});
11043+
return { Program: spy };
11044+
}
11045+
}
11046+
}
11047+
}
11048+
},
11049+
...baseConfig
11050+
};
11051+
11052+
linterWithOption.verify(code, config);
11053+
assert(spy && spy.calledOnce);
11054+
});
11055+
11056+
it("should assign process.cwd() to it if cwd is undefined", () => {
11057+
11058+
const linterWithOption = new Linter({ configType: "flat" });
11059+
let spy;
11060+
const config = {
11061+
plugins: {
11062+
test: {
11063+
rules: {
11064+
checker: {
11065+
create(context) {
11066+
11067+
spy = sinon.spy(() => {
11068+
assert.strictEqual(context.getCwd(), context.cwd);
11069+
assert.strictEqual(context.cwd, process.cwd());
11070+
});
11071+
return { Program: spy };
11072+
}
11073+
}
11074+
}
11075+
}
11076+
},
11077+
...baseConfig
11078+
};
11079+
11080+
linterWithOption.verify(code, config);
11081+
assert(spy && spy.calledOnce);
11082+
});
11083+
11084+
it("should assign process.cwd() to it if the option is undefined", () => {
11085+
let spy;
11086+
const config = {
11087+
plugins: {
11088+
test: {
11089+
rules: {
11090+
checker: {
11091+
create(context) {
11092+
11093+
spy = sinon.spy(() => {
11094+
assert.strictEqual(context.getCwd(), context.cwd);
11095+
assert.strictEqual(context.cwd, process.cwd());
11096+
});
11097+
return { Program: spy };
11098+
}
11099+
}
11100+
}
11101+
}
11102+
},
11103+
...baseConfig
11104+
};
11105+
11106+
linter.verify(code, config);
11107+
assert(spy && spy.calledOnce);
11108+
});
11109+
});
11110+
1102311111
});
1102411112

1102511113
describe("Rule Severity", () => {

0 commit comments

Comments
 (0)