Ohm-Management - Projektarbeit B-ME
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.

quote-props.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @fileoverview Rule to flag non-quoted property names in object literals.
  3. * @author Mathias Bynens <http://mathiasbynens.be/>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const espree = require("espree"),
  10. keywords = require("../util/keywords");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "require quotes around object literal property names",
  19. category: "Stylistic Issues",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/quote-props"
  22. },
  23. schema: {
  24. anyOf: [
  25. {
  26. type: "array",
  27. items: [
  28. {
  29. enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
  30. }
  31. ],
  32. minItems: 0,
  33. maxItems: 1
  34. },
  35. {
  36. type: "array",
  37. items: [
  38. {
  39. enum: ["always", "as-needed", "consistent", "consistent-as-needed"]
  40. },
  41. {
  42. type: "object",
  43. properties: {
  44. keywords: {
  45. type: "boolean"
  46. },
  47. unnecessary: {
  48. type: "boolean"
  49. },
  50. numbers: {
  51. type: "boolean"
  52. }
  53. },
  54. additionalProperties: false
  55. }
  56. ],
  57. minItems: 0,
  58. maxItems: 2
  59. }
  60. ]
  61. },
  62. fixable: "code"
  63. },
  64. create(context) {
  65. const MODE = context.options[0],
  66. KEYWORDS = context.options[1] && context.options[1].keywords,
  67. CHECK_UNNECESSARY = !context.options[1] || context.options[1].unnecessary !== false,
  68. NUMBERS = context.options[1] && context.options[1].numbers,
  69. MESSAGE_UNNECESSARY = "Unnecessarily quoted property '{{property}}' found.",
  70. MESSAGE_UNQUOTED = "Unquoted property '{{property}}' found.",
  71. MESSAGE_NUMERIC = "Unquoted number literal '{{property}}' used as key.",
  72. MESSAGE_RESERVED = "Unquoted reserved word '{{property}}' used as key.",
  73. sourceCode = context.getSourceCode();
  74. /**
  75. * Checks whether a certain string constitutes an ES3 token
  76. * @param {string} tokenStr - The string to be checked.
  77. * @returns {boolean} `true` if it is an ES3 token.
  78. */
  79. function isKeyword(tokenStr) {
  80. return keywords.indexOf(tokenStr) >= 0;
  81. }
  82. /**
  83. * Checks if an espree-tokenized key has redundant quotes (i.e. whether quotes are unnecessary)
  84. * @param {string} rawKey The raw key value from the source
  85. * @param {espreeTokens} tokens The espree-tokenized node key
  86. * @param {boolean} [skipNumberLiterals=false] Indicates whether number literals should be checked
  87. * @returns {boolean} Whether or not a key has redundant quotes.
  88. * @private
  89. */
  90. function areQuotesRedundant(rawKey, tokens, skipNumberLiterals) {
  91. return tokens.length === 1 && tokens[0].start === 0 && tokens[0].end === rawKey.length &&
  92. (["Identifier", "Keyword", "Null", "Boolean"].indexOf(tokens[0].type) >= 0 ||
  93. (tokens[0].type === "Numeric" && !skipNumberLiterals && String(+tokens[0].value) === tokens[0].value));
  94. }
  95. /**
  96. * Returns a string representation of a property node with quotes removed
  97. * @param {ASTNode} key Key AST Node, which may or may not be quoted
  98. * @returns {string} A replacement string for this property
  99. */
  100. function getUnquotedKey(key) {
  101. return key.type === "Identifier" ? key.name : key.value;
  102. }
  103. /**
  104. * Returns a string representation of a property node with quotes added
  105. * @param {ASTNode} key Key AST Node, which may or may not be quoted
  106. * @returns {string} A replacement string for this property
  107. */
  108. function getQuotedKey(key) {
  109. if (key.type === "Literal" && typeof key.value === "string") {
  110. // If the key is already a string literal, don't replace the quotes with double quotes.
  111. return sourceCode.getText(key);
  112. }
  113. // Otherwise, the key is either an identifier or a number literal.
  114. return `"${key.type === "Identifier" ? key.name : key.value}"`;
  115. }
  116. /**
  117. * Ensures that a property's key is quoted only when necessary
  118. * @param {ASTNode} node Property AST node
  119. * @returns {void}
  120. */
  121. function checkUnnecessaryQuotes(node) {
  122. const key = node.key;
  123. if (node.method || node.computed || node.shorthand) {
  124. return;
  125. }
  126. if (key.type === "Literal" && typeof key.value === "string") {
  127. let tokens;
  128. try {
  129. tokens = espree.tokenize(key.value);
  130. } catch (e) {
  131. return;
  132. }
  133. if (tokens.length !== 1) {
  134. return;
  135. }
  136. const isKeywordToken = isKeyword(tokens[0].value);
  137. if (isKeywordToken && KEYWORDS) {
  138. return;
  139. }
  140. if (CHECK_UNNECESSARY && areQuotesRedundant(key.value, tokens, NUMBERS)) {
  141. context.report({
  142. node,
  143. message: MESSAGE_UNNECESSARY,
  144. data: { property: key.value },
  145. fix: fixer => fixer.replaceText(key, getUnquotedKey(key))
  146. });
  147. }
  148. } else if (KEYWORDS && key.type === "Identifier" && isKeyword(key.name)) {
  149. context.report({
  150. node,
  151. message: MESSAGE_RESERVED,
  152. data: { property: key.name },
  153. fix: fixer => fixer.replaceText(key, getQuotedKey(key))
  154. });
  155. } else if (NUMBERS && key.type === "Literal" && typeof key.value === "number") {
  156. context.report({
  157. node,
  158. message: MESSAGE_NUMERIC,
  159. data: { property: key.value },
  160. fix: fixer => fixer.replaceText(key, getQuotedKey(key))
  161. });
  162. }
  163. }
  164. /**
  165. * Ensures that a property's key is quoted
  166. * @param {ASTNode} node Property AST node
  167. * @returns {void}
  168. */
  169. function checkOmittedQuotes(node) {
  170. const key = node.key;
  171. if (!node.method && !node.computed && !node.shorthand && !(key.type === "Literal" && typeof key.value === "string")) {
  172. context.report({
  173. node,
  174. message: MESSAGE_UNQUOTED,
  175. data: { property: key.name || key.value },
  176. fix: fixer => fixer.replaceText(key, getQuotedKey(key))
  177. });
  178. }
  179. }
  180. /**
  181. * Ensures that an object's keys are consistently quoted, optionally checks for redundancy of quotes
  182. * @param {ASTNode} node Property AST node
  183. * @param {boolean} checkQuotesRedundancy Whether to check quotes' redundancy
  184. * @returns {void}
  185. */
  186. function checkConsistency(node, checkQuotesRedundancy) {
  187. const quotedProps = [],
  188. unquotedProps = [];
  189. let keywordKeyName = null,
  190. necessaryQuotes = false;
  191. node.properties.forEach(property => {
  192. const key = property.key;
  193. if (!key || property.method || property.computed || property.shorthand) {
  194. return;
  195. }
  196. if (key.type === "Literal" && typeof key.value === "string") {
  197. quotedProps.push(property);
  198. if (checkQuotesRedundancy) {
  199. let tokens;
  200. try {
  201. tokens = espree.tokenize(key.value);
  202. } catch (e) {
  203. necessaryQuotes = true;
  204. return;
  205. }
  206. necessaryQuotes = necessaryQuotes || !areQuotesRedundant(key.value, tokens) || KEYWORDS && isKeyword(tokens[0].value);
  207. }
  208. } else if (KEYWORDS && checkQuotesRedundancy && key.type === "Identifier" && isKeyword(key.name)) {
  209. unquotedProps.push(property);
  210. necessaryQuotes = true;
  211. keywordKeyName = key.name;
  212. } else {
  213. unquotedProps.push(property);
  214. }
  215. });
  216. if (checkQuotesRedundancy && quotedProps.length && !necessaryQuotes) {
  217. quotedProps.forEach(property => {
  218. context.report({
  219. node: property,
  220. message: "Properties shouldn't be quoted as all quotes are redundant.",
  221. fix: fixer => fixer.replaceText(property.key, getUnquotedKey(property.key))
  222. });
  223. });
  224. } else if (unquotedProps.length && keywordKeyName) {
  225. unquotedProps.forEach(property => {
  226. context.report({
  227. node: property,
  228. message: "Properties should be quoted as '{{property}}' is a reserved word.",
  229. data: { property: keywordKeyName },
  230. fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key))
  231. });
  232. });
  233. } else if (quotedProps.length && unquotedProps.length) {
  234. unquotedProps.forEach(property => {
  235. context.report({
  236. node: property,
  237. message: "Inconsistently quoted property '{{key}}' found.",
  238. data: { key: property.key.name || property.key.value },
  239. fix: fixer => fixer.replaceText(property.key, getQuotedKey(property.key))
  240. });
  241. });
  242. }
  243. }
  244. return {
  245. Property(node) {
  246. if (MODE === "always" || !MODE) {
  247. checkOmittedQuotes(node);
  248. }
  249. if (MODE === "as-needed") {
  250. checkUnnecessaryQuotes(node);
  251. }
  252. },
  253. ObjectExpression(node) {
  254. if (MODE === "consistent") {
  255. checkConsistency(node, false);
  256. }
  257. if (MODE === "consistent-as-needed") {
  258. checkConsistency(node, true);
  259. }
  260. }
  261. };
  262. }
  263. };