hierarchy-separator.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. /**
  3. * @fileoverview Deprecated hierarchy separator
  4. * @author Yann Braga
  5. */
  6. const utils_1 = require("../utils");
  7. const ast_1 = require("../utils/ast");
  8. const constants_1 = require("../utils/constants");
  9. const create_storybook_rule_1 = require("../utils/create-storybook-rule");
  10. module.exports = (0, create_storybook_rule_1.createStorybookRule)({
  11. name: 'hierarchy-separator',
  12. defaultOptions: [],
  13. meta: {
  14. type: 'problem',
  15. fixable: 'code',
  16. hasSuggestions: true,
  17. docs: {
  18. description: 'Deprecated hierarchy separator in title property',
  19. categories: [constants_1.CategoryId.CSF, constants_1.CategoryId.RECOMMENDED],
  20. recommended: 'warn',
  21. },
  22. messages: {
  23. useCorrectSeparators: 'Use correct separators',
  24. deprecatedHierarchySeparator: 'Deprecated hierarchy separator in title property: {{metaTitle}}.',
  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 || !(0, ast_1.isLiteral)(titleNode.value)) {
  37. return;
  38. }
  39. const metaTitle = titleNode.value.raw || '';
  40. if (metaTitle.includes('|')) {
  41. context.report({
  42. node: titleNode,
  43. messageId: 'deprecatedHierarchySeparator',
  44. data: { metaTitle },
  45. // In case we want this to be auto fixed by --fix
  46. fix: function (fixer) {
  47. return fixer.replaceTextRange(titleNode.value.range, metaTitle.replace(/\|/g, '/'));
  48. },
  49. suggest: [
  50. {
  51. messageId: 'useCorrectSeparators',
  52. fix: function (fixer) {
  53. return fixer.replaceTextRange(titleNode.value.range, metaTitle.replace(/\|/g, '/'));
  54. },
  55. },
  56. ],
  57. });
  58. }
  59. },
  60. };
  61. },
  62. });