no-title-property-in-meta.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. /**
  3. * @fileoverview No title property in meta
  4. * @author Yann Braga
  5. */
  6. const utils_1 = require("../utils");
  7. const constants_1 = require("../utils/constants");
  8. const create_storybook_rule_1 = require("../utils/create-storybook-rule");
  9. const ast_1 = require("../utils/ast");
  10. module.exports = (0, create_storybook_rule_1.createStorybookRule)({
  11. name: 'no-title-property-in-meta',
  12. defaultOptions: [],
  13. meta: {
  14. type: 'problem',
  15. fixable: 'code',
  16. hasSuggestions: true,
  17. docs: {
  18. description: 'Do not define a title in meta',
  19. categories: [constants_1.CategoryId.CSF_STRICT],
  20. recommended: 'error',
  21. },
  22. messages: {
  23. removeTitleInMeta: 'Remove title property from meta',
  24. noTitleInMeta: `CSF3 does not need a title in meta`,
  25. },
  26. schema: [],
  27. },
  28. create: function (context) {
  29. return {
  30. ExportDefaultDeclaration: function (node) {
  31. const meta = (0, utils_1.getMetaObjectExpression)(node, context);
  32. if (!meta) {
  33. return null;
  34. }
  35. const titleNode = meta.properties.find((prop) => { var _a; return !(0, ast_1.isSpreadElement)(prop) && 'name' in prop.key && ((_a = prop.key) === null || _a === void 0 ? void 0 : _a.name) === 'title'; });
  36. if (titleNode) {
  37. context.report({
  38. node: titleNode,
  39. messageId: 'noTitleInMeta',
  40. suggest: [
  41. {
  42. messageId: 'removeTitleInMeta',
  43. fix(fixer) {
  44. const fullText = context.getSourceCode().text;
  45. const propertyTextWithExtraCharacter = fullText.slice(titleNode.range[0], titleNode.range[1] + 1);
  46. const hasComma = propertyTextWithExtraCharacter.slice(-1) === ',';
  47. const propertyRange = [
  48. titleNode.range[0],
  49. hasComma ? titleNode.range[1] + 1 : titleNode.range[1],
  50. ];
  51. return fixer.removeRange(propertyRange);
  52. },
  53. },
  54. ],
  55. });
  56. }
  57. },
  58. };
  59. },
  60. });