]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/linter/config-comment-parser.js
07bbead281cbf6cabb1712bd3f541967a2752fa2
2 * @fileoverview Config Comment Parser
3 * @author Nicholas C. Zakas
6 /* eslint-disable class-methods-use-this*/
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const levn
= require("levn"),
14 ConfigOps
= require("@eslint/eslintrc/lib/shared/config-ops");
16 const debug
= require("debug")("eslint:config-comment-parser");
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
23 * Object to parse ESLint configuration comments inside JavaScript files.
24 * @name ConfigCommentParser
26 module
.exports
= class ConfigCommentParser
{
29 * Parses a list of "name:string_value" or/and "name" options divided by comma or
30 * whitespace. Used for "global" and "exported" comments.
31 * @param {string} string The string to parse.
32 * @param {Comment} comment The comment node which has the string.
33 * @returns {Object} Result map object of names and string values, or null values if no value was provided
35 parseStringConfig(string
, comment
) {
36 debug("Parsing String config");
40 // Collapse whitespace around `:` and `,` to make parsing easier
41 const trimmedString
= string
.replace(/\s*([:,])\s*/gu, "$1");
43 trimmedString
.split(/\s|,+/u).forEach(name
=> {
48 // value defaults to null (if not provided), e.g: "foo" => ["foo", null]
49 const [key
, value
= null] = name
.split(":");
51 items
[key
] = { value
, comment
};
57 * Parses a JSON-like config.
58 * @param {string} string The string to parse.
59 * @param {Object} location Start line and column of comments for potential error message.
60 * @returns {({success: true, config: Object}|{success: false, error: Problem})} Result map object
62 parseJsonConfig(string
, location
) {
63 debug("Parsing JSON config");
67 // Parses a JSON-like comment by the same way as parsing CLI option.
69 items
= levn
.parse("Object", string
) || {};
71 // Some tests say that it should ignore invalid comments such as `/*eslint no-alert:abc*/`.
72 // Also, commaless notations have invalid severity:
73 // "no-alert: 2 no-console: 2" --> {"no-alert": "2 no-console: 2"}
74 // Should ignore that case as well.
75 if (ConfigOps
.isEverySeverityValid(items
)) {
83 debug("Levn parsing failed; falling back to manual parsing.");
85 // ignore to parse the string by a fallback.
89 * Optionator cannot parse commaless notations.
90 * But we are supporting that. So this is a fallback for that.
93 const normalizedString
= string
.replace(/([-a-zA-Z0-9/]+):/gu, "\"$1\":").replace(/(\]|[0-9])\s
+(?=")/u, "$1,");
96 items = JSON.parse(`{${normalizedString}}`);
98 debug("Manual parsing failed
.");
106 message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`,
107 line: location.start.line,
108 column: location.start.column + 1
121 * Parses a config of values separated by comma.
122 * @param {string} string The string to parse.
123 * @returns {Object} Result map of values and true values
125 parseListConfig(string) {
126 debug("Parsing list config
");
130 // Collapse whitespace around commas
131 string.replace(/\s*,\s*/gu, ",").split(/,+/u).forEach(name => {
132 const trimmedName = name.trim();
135 items[trimmedName] = true;