-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: add eslint rule for custom element class name
- Loading branch information
1 parent
87d0e68
commit 0ee530c
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { ESLintUtils, TSESLint, TSESTree, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
const createRule = ESLintUtils.RuleCreator( | ||
(name) => | ||
`https://github.com/lyne-design-system/lyne-components/blob/main/tools/eslint/${name}.ts`, | ||
); | ||
|
||
type MessageIds = 'customElementClassName'; | ||
|
||
export const name = 'custom-element-class-name-rule'; | ||
export const rule: TSESLint.RuleModule<'customElementClassName', never[]> = createRule< | ||
never[], | ||
MessageIds | ||
>({ | ||
create(context) { | ||
return { | ||
['ClassDeclaration > Decorator[expression.callee.name="customElement"]']( | ||
node: TSESTree.Decorator, | ||
) { | ||
const isClassDeclaration = (node: TSESTree.Node): node is TSESTree.ClassDeclaration => { | ||
return node.type === AST_NODE_TYPES.ClassDeclaration; | ||
}; | ||
|
||
const getClassName = (node: TSESTree.Node): string | undefined => { | ||
if (isClassDeclaration(node)) { | ||
return node.id?.name; | ||
} | ||
if (node.parent) { | ||
return getClassName(node.parent); | ||
} | ||
return undefined; | ||
}; | ||
|
||
const classParent = node.parent as TSESTree.ClassDeclaration; | ||
const className = getClassName(classParent); | ||
|
||
if (!className || !className.startsWith('Sbb') || !className.endsWith('Element')) { | ||
context.report({ | ||
node: classParent.id ? classParent.id : classParent, | ||
messageId: 'customElementClassName', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
name, | ||
meta: { | ||
docs: { | ||
description: 'Components class name should start with `Sbb` and end with `Element`', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
customElementClassName: | ||
'Components class name should start with `Sbb` and end with `Element`', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters