Skip to content

Commit 931d650

Browse files
nzakasmdjermanovic
andauthored
refactor: Use @eslint/plugin-kit (#18822)
* refactor: Extract ConfigCommentParser * refactor: Use @eslint/plugin-kit * Remove levn * Slice off environment-specific portion of JSON error message * Update lib/languages/js/source-code/source-code.js Co-authored-by: Milos Djermanovic <[email protected]> * Update lib/linter/linter.js Co-authored-by: Milos Djermanovic <[email protected]> * Update lib/languages/js/source-code/source-code.js Co-authored-by: Milos Djermanovic <[email protected]> * Apply feedback * Remove unused variable --------- Co-authored-by: Milos Djermanovic <[email protected]>
1 parent ed5cf0c commit 931d650

9 files changed

Lines changed: 67 additions & 570 deletions

File tree

lib/languages/js/source-code/source-code.js

Lines changed: 29 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const
2020

2121
CodePathAnalyzer = require("../../../linter/code-path-analysis/code-path-analyzer"),
2222
createEmitter = require("../../../linter/safe-emitter"),
23-
ConfigCommentParser = require("../../../linter/config-comment-parser"),
23+
{ ConfigCommentParser, VisitNodeStep, CallMethodStep } = require("@eslint/plugin-kit"),
2424

2525
eslintScope = require("eslint-scope");
2626

@@ -316,65 +316,6 @@ function markExportedVariables(globalScope, variables) {
316316

317317
}
318318

319-
const STEP_KIND = {
320-
visit: 1,
321-
call: 2
322-
};
323-
324-
/**
325-
* A class to represent a step in the traversal process.
326-
*/
327-
class TraversalStep {
328-
329-
/**
330-
* The type of the step.
331-
* @type {string}
332-
*/
333-
type;
334-
335-
/**
336-
* The kind of the step. Represents the same data as the `type` property
337-
* but it's a number for performance.
338-
* @type {number}
339-
*/
340-
kind;
341-
342-
/**
343-
* The target of the step.
344-
* @type {ASTNode|string}
345-
*/
346-
target;
347-
348-
/**
349-
* The phase of the step.
350-
* @type {number|undefined}
351-
*/
352-
phase;
353-
354-
/**
355-
* The arguments of the step.
356-
* @type {Array<any>}
357-
*/
358-
args;
359-
360-
/**
361-
* Creates a new instance.
362-
* @param {Object} options The options for the step.
363-
* @param {string} options.type The type of the step.
364-
* @param {ASTNode|string} options.target The target of the step.
365-
* @param {number|undefined} [options.phase] The phase of the step.
366-
* @param {Array<any>} options.args The arguments of the step.
367-
* @returns {void}
368-
*/
369-
constructor({ type, target, phase, args }) {
370-
this.type = type;
371-
this.kind = STEP_KIND[type];
372-
this.target = target;
373-
this.phase = phase;
374-
this.args = args;
375-
}
376-
}
377-
378319
/**
379320
* A class to represent a directive comment.
380321
* @implements {IDirective}
@@ -1002,16 +943,18 @@ class SourceCode extends TokenStore {
1002943
return false;
1003944
}
1004945

1005-
const { directivePart } = commentParser.extractDirectiveComment(comment.value);
946+
const directive = commentParser.parseDirective(comment.value);
1006947

1007-
const directiveMatch = directivesPattern.exec(directivePart);
948+
if (!directive) {
949+
return false;
950+
}
1008951

1009-
if (!directiveMatch) {
952+
if (!directivesPattern.test(directive.label)) {
1010953
return false;
1011954
}
1012955

1013956
// only certain comment types are supported as line comments
1014-
return comment.type !== "Line" || !!/^eslint-disable-(next-)?line$/u.test(directiveMatch[1]);
957+
return comment.type !== "Line" || !!/^eslint-disable-(next-)?line$/u.test(directive.label);
1015958
});
1016959

1017960
this[caches].set("configNodes", configNodes);
@@ -1038,27 +981,24 @@ class SourceCode extends TokenStore {
1038981
const directives = [];
1039982

1040983
this.getInlineConfigNodes().forEach(comment => {
1041-
const { directivePart, justificationPart } = commentParser.extractDirectiveComment(comment.value);
1042984

1043-
// Step 1: Extract the directive text
1044-
const match = directivesPattern.exec(directivePart);
1045-
1046-
if (!match) {
1047-
return;
1048-
}
1049-
1050-
const directiveText = match[1];
985+
// Step 1: Parse the directive
986+
const {
987+
label,
988+
value,
989+
justification: justificationPart
990+
} = commentParser.parseDirective(comment.value);
1051991

1052992
// Step 2: Extract the directive value
1053-
const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(directiveText);
993+
const lineCommentSupported = /^eslint-disable-(next-)?line$/u.test(label);
1054994

1055995
if (comment.type === "Line" && !lineCommentSupported) {
1056996
return;
1057997
}
1058998

1059999
// Step 3: Validate the directive does not span multiple lines
1060-
if (directiveText === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) {
1061-
const message = `${directiveText} comment should not span multiple lines.`;
1000+
if (label === "eslint-disable-line" && comment.loc.start.line !== comment.loc.end.line) {
1001+
const message = `${label} comment should not span multiple lines.`;
10621002

10631003
problems.push({
10641004
ruleId: null,
@@ -1069,19 +1009,17 @@ class SourceCode extends TokenStore {
10691009
}
10701010

10711011
// Step 4: Extract the directive value and create the Directive object
1072-
const directiveValue = directivePart.slice(match.index + directiveText.length);
1073-
1074-
switch (directiveText) {
1012+
switch (label) {
10751013
case "eslint-disable":
10761014
case "eslint-enable":
10771015
case "eslint-disable-next-line":
10781016
case "eslint-disable-line": {
1079-
const directiveType = directiveText.slice("eslint-".length);
1017+
const directiveType = label.slice("eslint-".length);
10801018

10811019
directives.push(new Directive({
10821020
type: directiveType,
10831021
node: comment,
1084-
value: directiveValue,
1022+
value,
10851023
justification: justificationPart
10861024
}));
10871025
}
@@ -1136,20 +1074,20 @@ class SourceCode extends TokenStore {
11361074

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

1139-
const { directiveText, directiveValue } = commentParser.parseDirective(comment);
1077+
const { label, value } = commentParser.parseDirective(comment.value);
11401078

1141-
switch (directiveText) {
1079+
switch (label) {
11421080
case "exported":
1143-
Object.assign(exportedVariables, commentParser.parseListConfig(directiveValue, comment));
1081+
Object.assign(exportedVariables, commentParser.parseListConfig(value));
11441082
break;
11451083

11461084
case "globals":
11471085
case "global":
1148-
for (const [id, { value }] of Object.entries(commentParser.parseStringConfig(directiveValue, comment))) {
1086+
for (const [id, idSetting] of Object.entries(commentParser.parseStringConfig(value))) {
11491087
let normalizedValue;
11501088

11511089
try {
1152-
normalizedValue = normalizeConfigGlobal(value);
1090+
normalizedValue = normalizeConfigGlobal(idSetting);
11531091
} catch (err) {
11541092
problems.push({
11551093
ruleId: null,
@@ -1172,9 +1110,9 @@ class SourceCode extends TokenStore {
11721110
break;
11731111

11741112
case "eslint": {
1175-
const parseResult = commentParser.parseJsonConfig(directiveValue);
1113+
const parseResult = commentParser.parseJSONLikeConfig(value);
11761114

1177-
if (parseResult.success) {
1115+
if (parseResult.ok) {
11781116
configs.push({
11791117
config: {
11801118
rules: parseResult.config
@@ -1251,16 +1189,14 @@ class SourceCode extends TokenStore {
12511189
const emitter = createEmitter();
12521190
let analyzer = {
12531191
enterNode(node) {
1254-
steps.push(new TraversalStep({
1255-
type: "visit",
1192+
steps.push(new VisitNodeStep({
12561193
target: node,
12571194
phase: 1,
12581195
args: [node, node.parent]
12591196
}));
12601197
},
12611198
leaveNode(node) {
1262-
steps.push(new TraversalStep({
1263-
type: "visit",
1199+
steps.push(new VisitNodeStep({
12641200
target: node,
12651201
phase: 2,
12661202
args: [node, node.parent]
@@ -1283,8 +1219,7 @@ class SourceCode extends TokenStore {
12831219

12841220
CODE_PATH_EVENTS.forEach(eventName => {
12851221
emitter.on(eventName, (...args) => {
1286-
steps.push(new TraversalStep({
1287-
type: "call",
1222+
steps.push(new CallMethodStep({
12881223
target: eventName,
12891224
args
12901225
}));

lib/linter/config-comment-parser.js

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)