项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

92 lines
3.5 KiB

  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. docs: {
  12. description: "enforce placing object properties on separate lines",
  13. category: "Stylistic Issues",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/object-property-newline"
  16. },
  17. schema: [
  18. {
  19. type: "object",
  20. properties: {
  21. allowAllPropertiesOnSameLine: {
  22. type: "boolean"
  23. },
  24. allowMultiplePropertiesPerLine: { // Deprecated
  25. type: "boolean"
  26. }
  27. },
  28. additionalProperties: false
  29. }
  30. ],
  31. fixable: "whitespace"
  32. },
  33. create(context) {
  34. const allowSameLine = context.options[0] && (
  35. Boolean(context.options[0].allowAllPropertiesOnSameLine) ||
  36. 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. };