Skip to content

Commit

Permalink
move parseDirective out of SourceCode
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Dec 6, 2023
1 parent 9c5e280 commit b14bf59
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 53 deletions.
36 changes: 35 additions & 1 deletion lib/linter/config-comment-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const levn = require("levn"),
Legacy: {
ConfigOps
}
} = require("@eslint/eslintrc/universal");
} = require("@eslint/eslintrc/universal"),
{
directivesPattern
} = require("../shared/directives");

const debug = require("debug")("eslint:config-comment-parser");

Expand Down Expand Up @@ -148,4 +151,35 @@ module.exports = class ConfigCommentParser {
return items;
}

/**
* Extract the directive and the justification from a given directive comment and trim them.
* @param {string} value The comment text to extract.
* @returns {{directivePart: string, justificationPart: string}} The extracted directive and justification.
*/
extractDirectiveComment(value) {
const match = /\s-{2,}\s/u.exec(value);

if (!match) {
return { directivePart: value.trim(), justificationPart: "" };
}

const directive = value.slice(0, match.index).trim();
const justification = value.slice(match.index + match[0].length).trim();

return { directivePart: directive, justificationPart: justification };
}

/**
* Parses a directive comment into directive text and value.
* @param {Comment} comment The comment node with the directive to be parsed.
* @returns {{directiveText: string, directiveValue: string}} The directive text and value.
*/
parseDirective(comment) {
const { directivePart } = this.extractDirectiveComment(comment.value);
const match = directivesPattern.exec(directivePart);
const directiveText = match[1];
const directiveValue = directivePart.slice(match.index + directiveText.length);

return { directiveText, directiveValue };
}
};
24 changes: 3 additions & 21 deletions lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,24 +316,6 @@ function createDisableDirectives(options) {
return result;
}

/**
* Extract the directive and the justification from a given directive comment and trim them.
* @param {string} value The comment text to extract.
* @returns {{directivePart: string, justificationPart: string}} The extracted directive and justification.
*/
function extractDirectiveComment(value) {
const match = /\s-{2,}\s/u.exec(value);

if (!match) {
return { directivePart: value.trim(), justificationPart: "" };
}

const directive = value.slice(0, match.index).trim();
const justification = value.slice(match.index + match[0].length).trim();

return { directivePart: directive, justificationPart: justification };
}

/**
* Parses comments in file to extract file-specific config of rules, globals
* and environments and merges them with global config; also code blocks
Expand All @@ -355,7 +337,7 @@ function getDirectiveComments(sourceCode, ruleMapper, warnInlineConfig) {
});

sourceCode.getInlineConfigNodes().filter(token => token.type !== "Shebang").forEach(comment => {
const { directivePart, justificationPart } = extractDirectiveComment(comment.value);
const { directivePart, justificationPart } = commentParser.extractDirectiveComment(comment.value);

const match = directivesPattern.exec(directivePart);

Expand Down Expand Up @@ -500,7 +482,7 @@ function getDirectiveCommentsForFlatConfig(sourceCode, ruleMapper) {
const disableDirectives = [];

sourceCode.getInlineConfigNodes().filter(token => token.type !== "Shebang").forEach(comment => {
const { directivePart, justificationPart } = extractDirectiveComment(comment.value);
const { directivePart, justificationPart } = commentParser.extractDirectiveComment(comment.value);

const match = directivesPattern.exec(directivePart);

Expand Down Expand Up @@ -620,7 +602,7 @@ function findEslintEnv(text) {
if (match[0].endsWith("*/")) {
retv = Object.assign(
retv || {},
commentParser.parseListConfig(extractDirectiveComment(match[1]).directivePart)
commentParser.parseListConfig(commentParser.extractDirectiveComment(match[1]).directivePart)
);
}
}
Expand Down
31 changes: 2 additions & 29 deletions lib/source-code/source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,6 @@ function isSpaceBetween(sourceCode, first, second, checkInsideOfJSXText) {
// Directive Comments
//-----------------------------------------------------------------------------

/**
* Extract the directive and the justification from a given directive comment and trim them.
* @param {string} value The comment text to extract.
* @returns {{directivePart: string, justificationPart: string}} The extracted directive and justification.
*/
function extractDirectiveComment(value) {
const match = /\s-{2,}\s/u.exec(value);

if (!match) {
return { directivePart: value.trim(), justificationPart: "" };
}

const directive = value.slice(0, match.index).trim();
const justification = value.slice(match.index + match[0].length).trim();

return { directivePart: directive, justificationPart: justification };
}

/**
* Ensures that variables representing built-in properties of the Global Object,
* and any globals declared by special block comments, are present in the global
Expand Down Expand Up @@ -921,7 +903,7 @@ class SourceCode extends TokenStore {
return false;
}

const { directivePart } = extractDirectiveComment(comment.value);
const { directivePart } = commentParser.extractDirectiveComment(comment.value);

const directiveMatch = directivesPattern.exec(directivePart);

Expand Down Expand Up @@ -962,15 +944,6 @@ class SourceCode extends TokenStore {
varsCache.set("configGlobals", configGlobals);
}

static parseDirective(commentValue) {
const { directivePart } = extractDirectiveComment(commentValue);
const match = directivesPattern.exec(directivePart);
const directiveText = match[1];
const directiveValue = directivePart.slice(match.index + directiveText.length);

return { directiveText, directiveValue };
}

/**
* Applies configuration found inside of the source code. This method is only
* called when ESLint is running with inline configuration allowed.
Expand All @@ -986,7 +959,7 @@ class SourceCode extends TokenStore {

this.getInlineConfigNodes().forEach(comment => {

const { directiveText, directiveValue } = SourceCode.parseDirective(comment.value);
const { directiveText, directiveValue } = commentParser.parseDirective(comment);

switch (directiveText) {
case "exported":
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/linter/config-comment-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,28 @@ describe("ConfigCommentParser", () => {
});
});

describe("parseDirective", () => {

it("should parse a directive comment with a justification", () => {
const comment = { value: " eslint no-unused-vars: error -- test " };
const result = commentParser.parseDirective(comment);

assert.deepStrictEqual(result, {
directiveText: "eslint",
directiveValue: " no-unused-vars: error"
});
});

it("should parse a directive comment without a justification", () => {
const comment = { value: "global foo" };
const result = commentParser.parseDirective(comment);

assert.deepStrictEqual(result, {
directiveText: "global",
directiveValue: " foo"
});
});

});

});
3 changes: 1 addition & 2 deletions tools/check-rule-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const { promisify } = require("util");
const meta = require("../docs/src/_data/rules_meta.json");
const markdownItRuleExample = require("../docs/tools/markdown-it-rule-example");
const ConfigCommentParser = require("../lib/linter/config-comment-parser");
const { SourceCode } = require("../lib/source-code");

//------------------------------------------------------------------------------
// Typedefs
Expand Down Expand Up @@ -89,7 +88,7 @@ async function findProblems(filename) {
if (comment.type !== "Block" || !/^\s*eslint(?!\S)/u.test(comment.value)) {
return false;
}
const { directiveValue } = SourceCode.parseDirective(comment.value);
const { directiveValue } = commentParser.parseDirective(comment);
const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc);

return parseResult.success && Object.prototype.hasOwnProperty.call(parseResult.config, title);
Expand Down

0 comments on commit b14bf59

Please sign in to comment.