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.

object-property-newline.js 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @fileoverview Rule to enforce placing object properties on separate lines.
  3. * @author Vitor Balocco
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "enforce placing object properties on separate lines",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/object-property-newline"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. allowAllPropertiesOnSameLine: {
  23. type: "boolean"
  24. },
  25. allowMultiplePropertiesPerLine: { // Deprecated
  26. type: "boolean"
  27. }
  28. },
  29. additionalProperties: false
  30. }
  31. ],
  32. fixable: "whitespace"
  33. },
  34. create(context) {
  35. const allowSameLine = context.options[0] && (
  36. (Boolean(context.options[0].allowAllPropertiesOnSameLine) || Boolean(context.options[0].allowMultiplePropertiesPerLine)) // Deprecated
  37. );
  38. const errorMessage = allowSameLine
  39. ? "Object properties must go on a new line if they aren't all on the same line."
  40. : "Object properties must go on a new line.";
  41. const sourceCode = context.getSourceCode();
  42. return {
  43. ObjectExpression(node) {
  44. if (allowSameLine) {
  45. if (node.properties.length > 1) {
  46. const firstTokenOfFirstProperty = sourceCode.getFirstToken(node.properties[0]);
  47. const lastTokenOfLastProperty = sourceCode.getLastToken(node.properties[node.properties.length - 1]);
  48. if (firstTokenOfFirstProperty.loc.end.line === lastTokenOfLastProperty.loc.start.line) {
  49. // All keys and values are on the same line
  50. return;
  51. }
  52. }
  53. }
  54. for (let i = 1; i < node.properties.length; i++) {
  55. const lastTokenOfPreviousProperty = sourceCode.getLastToken(node.properties[i - 1]);
  56. const firstTokenOfCurrentProperty = sourceCode.getFirstToken(node.properties[i]);
  57. if (lastTokenOfPreviousProperty.loc.end.line === firstTokenOfCurrentProperty.loc.start.line) {
  58. context.report({
  59. node,
  60. loc: firstTokenOfCurrentProperty.loc.start,
  61. message: errorMessage,
  62. fix(fixer) {
  63. const comma = sourceCode.getTokenBefore(firstTokenOfCurrentProperty);
  64. const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]];
  65. // Don't perform a fix if there are any comments between the comma and the next property.
  66. if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
  67. return null;
  68. }
  69. return fixer.replaceTextRange(rangeAfterComma, "\n");
  70. }
  71. });
  72. }
  73. }
  74. }
  75. };
  76. }
  77. };