]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/linter/config-comment-parser.js
b88c5e6c850929873e64b3b64a87c480647ca501
2 * @fileoverview Config Comment Parser
3 * @author Nicholas C. Zakas
6 /* eslint class-methods-use-this: off -- Methods desired on instance */
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const levn
= require("levn"),
18 } = require("@eslint/eslintrc/universal");
20 const debug
= require("debug")("eslint:config-comment-parser");
22 //------------------------------------------------------------------------------
24 //------------------------------------------------------------------------------
27 * Object to parse ESLint configuration comments inside JavaScript files.
28 * @name ConfigCommentParser
30 module
.exports
= class ConfigCommentParser
{
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
39 parseStringConfig(string
, comment
) {
40 debug("Parsing String config");
44 // Collapse whitespace around `:` and `,` to make parsing easier
45 const trimmedString
= string
.replace(/\s*([:,])\s*/gu, "$1");
47 trimmedString
.split(/\s|,+/u).forEach(name
=> {
52 // value defaults to null (if not provided), e.g: "foo" => ["foo", null]
53 const [key
, value
= null] = name
.split(":");
55 items
[key
] = { value
, comment
};
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
66 parseJsonConfig(string
, location
) {
67 debug("Parsing JSON config");
71 // Parses a JSON-like comment by the same way as parsing CLI option.
73 items
= levn
.parse("Object", string
) || {};
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
)) {
87 debug("Levn parsing failed; falling back to manual parsing.");
89 // ignore to parse the string by a fallback.
93 * Optionator cannot parse commaless notations.
94 * But we are supporting that. So this is a fallback for that.
97 const normalizedString
= string
.replace(/([-a-zA-Z0-9/]+):/gu, "\"$1\":").replace(/(\]|[0-9])\s
+(?=")/u, "$1,");
100 items = JSON.parse(`{${normalizedString}}`);
102 debug("Manual parsing failed
.");
110 message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`,
111 line: location.start.line,
112 column: location.start.column + 1
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
129 parseListConfig(string) {
130 debug("Parsing list config
");
134 // Collapse whitespace around commas
135 string.replace(/\s*,\s*/gu, ",").split(/,+/u).forEach(name => {
136 const trimmedName = name.trim();
139 items[trimmedName] = true;