enforceLineBreak.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var schema = [];
  7. var breakLineMessage = function breakLineMessage(direction) {
  8. return "New line required ".concat(direction, " type declaration");
  9. };
  10. var create = function create(context) {
  11. return {
  12. TypeAlias: function TypeAlias(node) {
  13. var sourceCode = context.getSourceCode();
  14. if (sourceCode.lines.length === 1) {
  15. return;
  16. }
  17. var exportedType = node.parent.type === 'ExportNamedDeclaration';
  18. var leadingComments = sourceCode.getCommentsBefore(exportedType ? node.parent : node);
  19. var hasLeadingComments = leadingComments.length > 0;
  20. if (node.loc.start.line !== 1) {
  21. if (hasLeadingComments && leadingComments[0].loc.start.line !== 1) {
  22. var lineAboveComment = sourceCode.lines[leadingComments[0].loc.start.line - 2];
  23. if (lineAboveComment !== '') {
  24. context.report({
  25. fix: function fix(fixer) {
  26. return fixer.insertTextBeforeRange(leadingComments[0].range, '\n');
  27. },
  28. message: breakLineMessage('above'),
  29. node: node
  30. });
  31. }
  32. } else if (!hasLeadingComments) {
  33. var isLineAbove = sourceCode.lines[node.loc.start.line - 2];
  34. if (isLineAbove !== '') {
  35. context.report({
  36. fix: function fix(fixer) {
  37. return fixer.insertTextBefore(exportedType ? node.parent : node, '\n');
  38. },
  39. message: breakLineMessage('above'),
  40. node: node
  41. });
  42. }
  43. }
  44. }
  45. if (sourceCode.lines.length !== node.loc.end.line) {
  46. var isLineBelow = sourceCode.lines[node.loc.end.line];
  47. if (isLineBelow !== '') {
  48. context.report({
  49. fix: function fix(fixer) {
  50. return fixer.insertTextAfter(node, '\n');
  51. },
  52. message: breakLineMessage('below'),
  53. node: node
  54. });
  55. }
  56. }
  57. }
  58. };
  59. };
  60. var _default = {
  61. create: create,
  62. meta: {
  63. fixable: 'code'
  64. },
  65. schema: schema
  66. };
  67. exports["default"] = _default;
  68. module.exports = exports.default;