]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/linter/config-comment-parser.js
b88c5e6c850929873e64b3b64a87c480647ca501
[pve-eslint.git] / eslint / lib / linter / config-comment-parser.js
1 /**
2 * @fileoverview Config Comment Parser
3 * @author Nicholas C. Zakas
4 */
5
6 /* eslint class-methods-use-this: off -- Methods desired on instance */
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Requirements
11 //------------------------------------------------------------------------------
12
13 const levn = require("levn"),
14 {
15 Legacy: {
16 ConfigOps
17 }
18 } = require("@eslint/eslintrc/universal");
19
20 const debug = require("debug")("eslint:config-comment-parser");
21
22 //------------------------------------------------------------------------------
23 // Public Interface
24 //------------------------------------------------------------------------------
25
26 /**
27 * Object to parse ESLint configuration comments inside JavaScript files.
28 * @name ConfigCommentParser
29 */
30 module.exports = class ConfigCommentParser {
31
32 /**
33 * Parses a list of "name:string_value" or/and "name" options divided by comma or
34 * whitespace. Used for "global" and "exported" comments.
35 * @param {string} string The string to parse.
36 * @param {Comment} comment The comment node which has the string.
37 * @returns {Object} Result map object of names and string values, or null values if no value was provided
38 */
39 parseStringConfig(string, comment) {
40 debug("Parsing String config");
41
42 const items = {};
43
44 // Collapse whitespace around `:` and `,` to make parsing easier
45 const trimmedString = string.replace(/\s*([:,])\s*/gu, "$1");
46
47 trimmedString.split(/\s|,+/u).forEach(name => {
48 if (!name) {
49 return;
50 }
51
52 // value defaults to null (if not provided), e.g: "foo" => ["foo", null]
53 const [key, value = null] = name.split(":");
54
55 items[key] = { value, comment };
56 });
57 return items;
58 }
59
60 /**
61 * Parses a JSON-like config.
62 * @param {string} string The string to parse.
63 * @param {Object} location Start line and column of comments for potential error message.
64 * @returns {({success: true, config: Object}|{success: false, error: Problem})} Result map object
65 */
66 parseJsonConfig(string, location) {
67 debug("Parsing JSON config");
68
69 let items = {};
70
71 // Parses a JSON-like comment by the same way as parsing CLI option.
72 try {
73 items = levn.parse("Object", string) || {};
74
75 // Some tests say that it should ignore invalid comments such as `/*eslint no-alert:abc*/`.
76 // Also, commaless notations have invalid severity:
77 // "no-alert: 2 no-console: 2" --> {"no-alert": "2 no-console: 2"}
78 // Should ignore that case as well.
79 if (ConfigOps.isEverySeverityValid(items)) {
80 return {
81 success: true,
82 config: items
83 };
84 }
85 } catch {
86
87 debug("Levn parsing failed; falling back to manual parsing.");
88
89 // ignore to parse the string by a fallback.
90 }
91
92 /*
93 * Optionator cannot parse commaless notations.
94 * But we are supporting that. So this is a fallback for that.
95 */
96 items = {};
97 const normalizedString = string.replace(/([-a-zA-Z0-9/]+):/gu, "\"$1\":").replace(/(\]|[0-9])\s+(?=")/u, "$1,");
98
99 try {
100 items = JSON.parse(`{${normalizedString}}`);
101 } catch (ex) {
102 debug("Manual parsing failed.");
103
104 return {
105 success: false,
106 error: {
107 ruleId: null,
108 fatal: true,
109 severity: 2,
110 message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`,
111 line: location.start.line,
112 column: location.start.column + 1
113 }
114 };
115
116 }
117
118 return {
119 success: true,
120 config: items
121 };
122 }
123
124 /**
125 * Parses a config of values separated by comma.
126 * @param {string} string The string to parse.
127 * @returns {Object} Result map of values and true values
128 */
129 parseListConfig(string) {
130 debug("Parsing list config");
131
132 const items = {};
133
134 // Collapse whitespace around commas
135 string.replace(/\s*,\s*/gu, ",").split(/,+/u).forEach(name => {
136 const trimmedName = name.trim();
137
138 if (trimmedName) {
139 items[trimmedName] = true;
140 }
141 });
142 return items;
143 }
144
145 };