From 4e9a30a0c166d2b7954edb1bb17a9050292fc0fe Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 27 Apr 2023 19:13:18 +0530 Subject: [PATCH] tests: add asertions for context.getPhysicalFilename() --- docs/src/extend/custom-rules.md | 6 +-- tests/lib/linter/linter.js | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/docs/src/extend/custom-rules.md b/docs/src/extend/custom-rules.md index 84fbe20f4c9e..c5c05bc4fbb3 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 ba29ea3d643c..82685a784768 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -133,6 +133,75 @@ describe("Linter", () => { }); }); + describe("context.getPhysicalFilename()", () => { + + const ruleId = "filename-rule"; + + it("has access to the physicalFilename", () => { + + const config = { + plugins: { + test: { + rules: { + [ruleId]: { + create: context => ({ + Literal(node) { + context.report(node, context.getPhysicalFilename()); + } + }) + } + } + } + }, + rules: { + [`test/${ruleId}`]: 1 + } + }; + + const messages = linter.verify("0", config, filename); + const suppressedMessages = linter.getSuppressedMessages(); + + assert.strictEqual(messages[0].message, filename); + assert.strictEqual(suppressedMessages.length, 0); + }); + + }); + + describe("context.physicalFilename", () => { + + const ruleId = "filename-rule"; + + it("has access to the physicalFilename", () => { + + const config = { + plugins: { + test: { + rules: { + [ruleId]: { + create: context => ({ + Literal(node) { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); + context.report(node, context.physicalFilename); + } + }) + } + } + } + }, + rules: { + [`test/${ruleId}`]: 1 + } + }; + + const messages = linter.verify("0", config, filename); + const suppressedMessages = linter.getSuppressedMessages(); + + assert.strictEqual(messages[0].message, filename); + assert.strictEqual(suppressedMessages.length, 0); + }); + + }); + describe("context.getSourceLines()", () => { it("should get proper lines when using \\n as a line break", () => { @@ -1846,6 +1915,7 @@ describe("Linter", () => { linter.defineRule(code, { create: context => ({ Literal(node) { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); context.report(node, context.physicalFilename); } }) @@ -4863,6 +4933,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 +4945,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 +4957,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 +9229,7 @@ describe("Linter with FlatConfigArray", () => { [ruleId]: { create: context => ({ Literal(node) { + assert.strictEqual(context.getPhysicalFilename(), context.physicalFilename); context.report(node, context.physicalFilename); } }) @@ -11191,6 +11265,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 +11289,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 +11313,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 {}; });