123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- "use strict";
- const levn = require("levn"),
- {
- Legacy: {
- ConfigOps
- }
- } = require("@eslint/eslintrc/universal");
- const debug = require("debug")("eslint:config-comment-parser");
- module.exports = class ConfigCommentParser {
-
- parseStringConfig(string, comment) {
- debug("Parsing String config");
- const items = {};
-
- const trimmedString = string.replace(/\s*([:,])\s*/gu, "$1");
- trimmedString.split(/\s|,+/u).forEach(name => {
- if (!name) {
- return;
- }
-
- const [key, value = null] = name.split(":");
- items[key] = { value, comment };
- });
- return items;
- }
-
- parseJsonConfig(string, location) {
- debug("Parsing JSON config");
- let items = {};
-
- try {
- items = levn.parse("Object", string) || {};
-
-
-
-
- if (ConfigOps.isEverySeverityValid(items)) {
- return {
- success: true,
- config: items
- };
- }
- } catch {
- debug("Levn parsing failed; falling back to manual parsing.");
-
- }
-
- items = {};
- const normalizedString = string.replace(/([-a-zA-Z0-9/]+):/gu, "\"$1\":").replace(/(\]|[0-9])\s+(?=")/u, "$1,");
- try {
- items = JSON.parse(`{${normalizedString}}`);
- } catch (ex) {
- debug("Manual parsing failed.");
- return {
- success: false,
- error: {
- ruleId: null,
- fatal: true,
- severity: 2,
- message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`,
- line: location.start.line,
- column: location.start.column + 1
- }
- };
- }
- return {
- success: true,
- config: items
- };
- }
-
- parseListConfig(string) {
- debug("Parsing list config");
- const items = {};
- string.split(",").forEach(name => {
- const trimmedName = name.trim();
- if (trimmedName) {
- items[trimmedName] = true;
- }
- });
- return items;
- }
- };
|