diff --git a/docs/src/extend/custom-rules.md b/docs/src/extend/custom-rules.md index 84fbe20f4c9..c5c05bc4fbb 100644 --- a/docs/src/extend/custom-rules.md +++ b/docs/src/extend/custom-rules.md @@ -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 `` 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. @@ -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 `` 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 `` 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 `` 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`. diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index ba29ea3d643..aa1d2c91731 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -1846,6 +1846,7 @@ describe("Linter", () => { linter.defineRule(code, { create: context => ({ Literal(node) { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); context.report(node, context.physicalFilename); } }) @@ -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 {}; }); @@ -4874,6 +4876,7 @@ var a = "test2"; it("should default physicalFilename to when options object doesn't have filename", () => { const physicalFilenameChecker = sinon.spy(context => { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); assert.strictEqual(context.physicalFilename, ""); return {}; }); @@ -4885,6 +4888,7 @@ var a = "test2"; it("should default physicalFilename to when only two arguments are passed", () => { const physicalFilenameChecker = sinon.spy(context => { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); assert.strictEqual(context.physicalFilename, ""); return {}; }); @@ -9156,6 +9160,7 @@ describe("Linter with FlatConfigArray", () => { [ruleId]: { create: context => ({ Literal(node) { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); context.report(node, context.physicalFilename); } }) @@ -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 {}; }); @@ -11214,6 +11220,7 @@ describe("Linter with FlatConfigArray", () => { it("should default physicalFilename to when options object doesn't have filename", () => { const physicalFilenameChecker = sinon.spy(context => { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); assert.strictEqual(context.physicalFilename, ""); return {}; }); @@ -11237,6 +11244,7 @@ describe("Linter with FlatConfigArray", () => { it("should default physicalFilename to when only two arguments are passed", () => { const physicalFilenameChecker = sinon.spy(context => { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); assert.strictEqual(context.physicalFilename, ""); return {}; });