Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: add getPhysicalFilename() method to rule context (fixes #11989) #14616

Merged
merged 7 commits into from Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/developer-guide/working-with-rules.md
Expand Up @@ -139,6 +139,7 @@ Additionally, the `context` object has the following methods:
* 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()` - returns the full path of the file on disk without any code block information.
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
* `getScope()` - returns the [scope](./scope-manager-interface.md#scope-interface) of the currently-traversed node. This information can be used to track references to variables.
* `getSourceCode()` - returns a [`SourceCode`](#contextgetsourcecode) object that you can use to work with the source that was passed to ESLint.
* `markVariableAsUsed(name)` - marks a variable with the given name in the current scope as used. This affects the [no-unused-vars](../rules/no-unused-vars.md) rule. Returns `true` if a variable with the given name was found and marked as used, otherwise `false`.
Expand Down
10 changes: 7 additions & 3 deletions lib/linter/linter.js
Expand Up @@ -828,9 +828,10 @@ const BASE_TRAVERSAL_CONTEXT = Object.freeze(
* @param {string} filename The reported filename of the code
* @param {boolean} disableFixes If true, it doesn't make `fix` properties.
* @param {string | undefined} cwd cwd of the cli
* @param {string} physicalFilename The full path of the file on disk without any code block information
* @returns {Problem[]} An array of reported problems
*/
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd) {
function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parserName, settings, filename, disableFixes, cwd, physicalFilename) {
const emitter = createEmitter();
const nodeQueue = [];
let currentNode = sourceCode.ast;
Expand Down Expand Up @@ -859,6 +860,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
getDeclaredVariables: sourceCode.scopeManager.getDeclaredVariables.bind(sourceCode.scopeManager),
getCwd: () => cwd,
getFilename: () => filename,
getPhysicalFilename: () => physicalFilename || filename,
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
getScope: () => getScope(sourceCode.scopeManager, currentNode),
getSourceCode: () => sourceCode,
markVariableAsUsed: name => markVariableAsUsed(sourceCode.scopeManager, currentNode, parserOptions, name),
Expand Down Expand Up @@ -1181,7 +1183,8 @@ class Linter {
settings,
options.filename,
options.disableFixes,
slots.cwd
slots.cwd,
options.physicalFilename
);
} catch (err) {
err.message += `\nOccurred while linting ${options.filename}`;
Expand Down Expand Up @@ -1283,6 +1286,7 @@ class Linter {
*/
_verifyWithProcessor(textOrSourceCode, config, options, configForRecursive) {
const filename = options.filename || "<input>";
const physicalFilename = filename;
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
const filenameToExpose = normalizeFilename(filename);
const text = ensureText(textOrSourceCode);
const preprocess = options.preprocess || (rawText => [rawText]);
Expand Down Expand Up @@ -1324,7 +1328,7 @@ class Linter {
return this._verifyWithoutProcessors(
blockText,
config,
{ ...options, filename: blockName }
{ ...options, filename: blockName, physicalFilename }
);
});

Expand Down
63 changes: 63 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -52,6 +52,7 @@ const ESLINT_ENV = "eslint-env";

describe("Linter", () => {
const filename = "filename.js";
const physicalFilename = "physical-filename.js";

/** @type {InstanceType<import("../../../lib/linter/linter.js")["Linter"]>} */
let linter;
Expand Down Expand Up @@ -1559,6 +1560,22 @@ describe("Linter", () => {
assert.strictEqual(messages[0].message, filename);
});

it("has access to the physicalFilename", () => {
linter.defineRule(code, context => ({
Literal(node) {
context.report(node, context.getPhysicalFilename());
}
}));

const config = { rules: {} };

config.rules[code] = 1;

const messages = linter.verify("0", config, physicalFilename);

assert.strictEqual(messages[0].message, physicalFilename);
});

it("defaults filename to '<input>'", () => {
linter.defineRule(code, context => ({
Literal(node) {
Expand Down Expand Up @@ -3408,6 +3425,52 @@ var a = "test2";
});
});

describe("physicalFilenames", () => {
it("should allow physicalFilename to be passed on options object", () => {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), "foo.js");
return {};
});

linter.defineRule("checker", physicalFilenameChecker);
linter.verify("foo;", { rules: { checker: "error" } }, { filename: "foo.js" });
assert(physicalFilenameChecker.calledOnce);
});

it("should allow filename to be passed as third argument", () => {
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), "foo.js");
return {};
});

linter.defineRule("checker", physicalFilenameChecker);
linter.verify("foo;", { rules: { checker: "error" } }, { filename: "foo.js" });
assert(physicalFilenameChecker.calledOnce);
});

it("should default physicalFilename to <input> when options object doesn't have filename", () => {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
const physicalFilenameChecker = sinon.spy(context => {
assert.strictEqual(context.getPhysicalFilename(), "<input>");
return {};
});

linter.defineRule("checker", physicalFilenameChecker);
linter.verify("foo;", { rules: { checker: "error" } }, {});
assert(physicalFilenameChecker.calledOnce);
});

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

linter.defineRule("checker", physicalFilenameChecker);
linter.verify("foo;", { rules: { checker: "error" } });
assert(physicalFilenameChecker.calledOnce);
});
});

snitin315 marked this conversation as resolved.
Show resolved Hide resolved
it("should report warnings in order by line and column when called", () => {

const code = "foo()\n alert('test')";
Expand Down