Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config-comment-parser.js 4.4KB

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