This repository was archived by the owner on Nov 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathno-title-property-in-meta.ts
More file actions
70 lines (65 loc) · 2.12 KB
/
no-title-property-in-meta.ts
File metadata and controls
70 lines (65 loc) · 2.12 KB
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
/**
* @fileoverview No title property in meta
* @author Yann Braga
*/
import { TSESTree } from '@typescript-eslint/utils'
import { getMetaObjectExpression, getObjectBareProperty } from '../utils'
import { CategoryId } from '../utils/constants'
import { createStorybookRule } from '../utils/create-storybook-rule'
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
export = createStorybookRule({
name: 'no-title-property-in-meta',
defaultOptions: [],
meta: {
type: 'problem',
fixable: 'code',
hasSuggestions: true,
docs: {
description: 'Do not define a title in meta',
categories: [CategoryId.CSF_STRICT],
recommended: 'error',
},
messages: {
removeTitleInMeta: 'Remove title property from meta',
noTitleInMeta: `CSF3 does not need a title in meta`,
},
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) {
context.report({
node: titleNode,
messageId: 'noTitleInMeta',
suggest: [
{
messageId: 'removeTitleInMeta',
fix(fixer) {
const fullText = context.getSourceCode().text
const propertyTextWithExtraCharacter = fullText.slice(
titleNode.range[0],
titleNode.range[1] + 1
)
const hasComma = propertyTextWithExtraCharacter.slice(-1) === ','
const propertyRange: TSESTree.Range = [
titleNode.range[0],
hasComma ? titleNode.range[1] + 1 : titleNode.range[1],
]
return fixer.removeRange(propertyRange)
},
},
],
})
}
},
}
},
})