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 27, 2023
1 parent 1aaece9 commit bf183f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/src/extend/custom-rules.md
Expand Up @@ -124,13 +124,14 @@ As the name implies, the `context` object contains information that is relevant
The `context` object has the following properties:

* `id`: (`string`) The rule ID.
* `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.
* `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()`: 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 All @@ -145,8 +146,7 @@ Additionally, the `context` object has the following methods & properties:
* If the node is an `ImportSpecifier`, `ImportDefaultSpecifier`, or `ImportNamespaceSpecifier`, the declared variable is returned.
* Otherwise, if the node does not declare any variables, an empty array is returned.
* `getFilename()`: Returns the filename associated with the source.
* `getPhysicalFilename()`: (**Deprecated:** Use `context#physicalFilename` instead.) 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.
* `physicalFilename`: 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.
* `getPhysicalFilename()`: (**Deprecated:** Use `context.physicalFilename` instead.) 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.
* `getScope()`: (**Deprecated:** Use `SourceCode#getScope(node)` instead.) Returns the [scope](./scope-manager-interface#scope-interface) of the currently-traversed node. This information can be used to track references to variables.
* `getSourceCode()`: Returns a `SourceCode` object that you can use to work with the source that was passed to ESLint (see [Accessing the Source Code](#accessing-the-source-code)).
* `markVariableAsUsed(name)`: (**Deprecated:** Use `SourceCode#markVariableAsUsed(name, node)` instead.) Marks a variable with the given name in the current scope as used. This affects the [no-unused-vars](../rules/no-unused-vars) rule. Returns `true` if a variable with the given name was found and marked as used, otherwise `false`.
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -1846,6 +1846,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 @@ -4863,6 +4864,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 @@ -4874,6 +4876,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 @@ -4885,6 +4888,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 @@ -9156,6 +9160,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 @@ -11191,6 +11196,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 @@ -11214,6 +11220,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 @@ -11237,6 +11244,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 bf183f9

Please sign in to comment.