newlineAfterFlowAnnotation.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  8. var looksLikeFlowFileAnnotation = function looksLikeFlowFileAnnotation(comment) {
  9. return /@(?:no)?flo/i.test(comment);
  10. };
  11. var schema = [{
  12. "enum": ['always', 'always-windows', 'never'],
  13. type: 'string'
  14. }];
  15. var create = function create(context) {
  16. var mode = context.options[0];
  17. var never = mode === 'never';
  18. var newline = mode === 'always-windows' ? '\r\n' : '\n';
  19. return {
  20. Program: function Program(node) {
  21. var sourceCode = context.getSourceCode();
  22. var potentialFlowFileAnnotation = _lodash["default"].find(context.getSourceCode().getAllComments(), function (comment) {
  23. return looksLikeFlowFileAnnotation(comment.value);
  24. });
  25. if (potentialFlowFileAnnotation) {
  26. var line = potentialFlowFileAnnotation.loc.end.line;
  27. var nextLineIsEmpty = sourceCode.lines[line] === '';
  28. if (!never && !nextLineIsEmpty) {
  29. context.report({
  30. fix: function fix(fixer) {
  31. return fixer.insertTextAfter(potentialFlowFileAnnotation, newline);
  32. },
  33. message: 'Expected newline after flow annotation',
  34. node: node
  35. });
  36. }
  37. if (never && nextLineIsEmpty) {
  38. context.report({
  39. fix: function fix(fixer) {
  40. var lineBreak = sourceCode.text[potentialFlowFileAnnotation.range[1]];
  41. return fixer.replaceTextRange([potentialFlowFileAnnotation.range[1], potentialFlowFileAnnotation.range[1] + (lineBreak === '\r' ? 2 : 1)], '');
  42. },
  43. message: 'Expected no newline after flow annotation',
  44. node: node
  45. });
  46. }
  47. }
  48. }
  49. };
  50. };
  51. var _default = {
  52. create: create,
  53. meta: {
  54. fixable: 'code'
  55. },
  56. schema: schema
  57. };
  58. exports["default"] = _default;
  59. module.exports = exports.default;