Skip to content

Commit

Permalink
tests: add asertions for context.getPhysicalFilename()
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 29, 2023
1 parent 2008de9 commit e566cc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/src/extend/custom-rules.md
Expand Up @@ -125,14 +125,15 @@ The `context` object has the following properties:

* `id`: (`string`) The rule ID.
* `filename`: (`string`) The filename associated with the source.
* `physicalFilename`: (`string`) When linting a file, it returns the full path of the file on disk without any code block information. When linting text, it returns the value passed to `—stdin-filename` or `<text>` if not specified.
* `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.
* `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)).
* `settings`: (`object`) The [shared settings](../use/configure/configuration-files#adding-shared-settings) from the configuration.
* `parserPath`: (`string`) The name of the `parser` from the configuration.
* `parserServices`: (`object`) Contains parser-provided services for rules. The default parser does not provide any services. However, if a rule is intended to be used with a custom parser, it could use `parserServices` to access anything provided by that parser. (For example, a TypeScript parser could provide the ability to get the computed type of a given node.)
* `parserOptions`: The parser options configured for this run (more details [here](../use/configure/language-options#specifying-parser-options)).

Additionally, the `context` object has the following methods & properties:
Additionally, the `context` object has the following methods:

* `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.
* `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.
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -1847,6 +1847,7 @@ describe("Linter", () => {
linter.defineRule(code, {
create: context => ({
Literal(node) {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
context.report(node, context.physicalFilename);
}
})
Expand Down Expand Up @@ -4868,6 +4869,7 @@ var a = "test2";
describe("physicalFilenames", () => {
it("should be same as `filename` passed on options object, if no processors are used", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "foo.js");
return {};
});
Expand All @@ -4879,6 +4881,7 @@ var a = "test2";

it("should default physicalFilename to <input> when options object doesn't have filename", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "<input>");
return {};
});
Expand All @@ -4890,6 +4893,7 @@ var a = "test2";

it("should default physicalFilename to <input> when only two arguments are passed", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "<input>");
return {};
});
Expand Down Expand Up @@ -9224,6 +9228,7 @@ describe("Linter with FlatConfigArray", () => {
[ruleId]: {
create: context => ({
Literal(node) {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
context.report(node, context.physicalFilename);
}
})
Expand Down Expand Up @@ -11344,6 +11349,7 @@ describe("Linter with FlatConfigArray", () => {
describe("physicalFilename", () => {
it("should be same as `filename` passed on options object, if no processors are used", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "foo.js");
return {};
});
Expand All @@ -11367,6 +11373,7 @@ describe("Linter with FlatConfigArray", () => {

it("should default physicalFilename to <input> when options object doesn't have filename", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "<input>");
return {};
});
Expand All @@ -11390,6 +11397,7 @@ describe("Linter with FlatConfigArray", () => {

it("should default physicalFilename to <input> when only two arguments are passed", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename);
assert.strictEqual(context.physicalFilename, "<input>");
return {};
});
Expand Down

0 comments on commit e566cc5

Please sign in to comment.