-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathhierarchy-separator.ts
75 lines (68 loc) · 2.19 KB
/
hierarchy-separator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @fileoverview Deprecated hierarchy separator
* @author Yann Braga
*/
import { getMetaObjectExpression, getObjectBareProperty } from '../utils'
import { isLiteral } from '../utils/ast'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
export = createStorybookRule({
name: 'hierarchy-separator',
defaultOptions: [],
meta: {
type: 'problem',
fixable: 'code',
hasSuggestions: true,
docs: {
description: 'Deprecated hierarchy separator in title property',
categories: [CategoryId.CSF, CategoryId.RECOMMENDED],
recommended: 'warn',
},
messages: {
useCorrectSeparators: 'Use correct separators',
deprecatedHierarchySeparator:
'Deprecated hierarchy separator in title property: {{metaTitle}}.',
},
schema: [],
},
create: function (context) {
return {
ExportDefaultDeclaration: function (node) {
const meta = getMetaObjectExpression(node, context)
if (!meta) {
return null
}
const titleNode = getObjectBareProperty(meta.properties, 'title')
if (!titleNode || !isLiteral(titleNode.value)) {
return
}
const metaTitle = titleNode.value.raw || ''
if (metaTitle.includes('|')) {
context.report({
node: titleNode,
messageId: 'deprecatedHierarchySeparator',
data: { metaTitle },
// In case we want this to be auto fixed by --fix
fix: function (fixer) {
return fixer.replaceTextRange(titleNode.value.range, metaTitle.replace(/\|/g, '/'))
},
suggest: [
{
messageId: 'useCorrectSeparators',
fix: function (fixer) {
return fixer.replaceTextRange(
titleNode.value.range,
metaTitle.replace(/\|/g, '/')
)
},
},
],
})
}
},
}
},
})