Skip to content

Commit fdb6c84

Browse files
authored
build: Implement eslint rules to move style prop in the bottom of the class (#615)
* setup * format styles
1 parent bb839af commit fdb6c84

File tree

87 files changed

+4157
-4056
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+4157
-4056
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
ignorePatterns: ['vite.*.js', 'packages/**/*.js', 'src/**/*'],
33
root: true,
4-
plugins: ['html', 'import'],
4+
plugins: ['html', 'import', 'eslint-plugin-local-rules'],
55
overrides: [
66
{
77
files: ['*.ts', '*.tsx'],
@@ -25,6 +25,8 @@ module.exports = {
2525
'@typescript-eslint/ban-types': 'off', //TODO: Remove (maybe)
2626
'lit/no-useless-template-literals': 'error',
2727
'lit/prefer-nothing': 'error',
28+
'local-rules/uui-class-prefix': 'warn',
29+
'local-rules/prefer-static-styles-last': 'warn',
2830
},
2931
parserOptions: {
3032
project: './tsconfig.json',

eslint-local-rules.cjs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// eslint-disable-next-line no-undef
4+
module.exports = {
5+
/** @type {import('eslint').Rule.RuleModule} */
6+
'uui-class-prefix': {
7+
meta: {
8+
type: 'problem',
9+
docs: {
10+
description:
11+
'Ensure that all class declarations are prefixed with "UUI"',
12+
category: 'Best Practices',
13+
recommended: true,
14+
},
15+
schema: [],
16+
},
17+
create: function (context) {
18+
function checkClassName(node) {
19+
if (node.id && node.id.name && !node.id.name.startsWith('UUI')) {
20+
context.report({
21+
node: node.id,
22+
message: 'Class declaration should be prefixed with "UUI"',
23+
});
24+
}
25+
}
26+
27+
return {
28+
ClassDeclaration: checkClassName,
29+
};
30+
},
31+
},
32+
33+
/** @type {import('eslint').Rule.RuleModule}*/
34+
'prefer-static-styles-last': {
35+
meta: {
36+
type: 'suggestion',
37+
docs: {
38+
description:
39+
'Enforce the "styles" property with the static modifier to be the last property of a class that ends with "Element".',
40+
category: 'Best Practices',
41+
recommended: true,
42+
},
43+
fixable: 'code',
44+
schema: [],
45+
},
46+
create: function (context) {
47+
return {
48+
ClassDeclaration(node) {
49+
const className = node.id.name;
50+
if (className.endsWith('Element')) {
51+
const staticStylesProperty = node.body.body.find(bodyNode => {
52+
return (
53+
bodyNode.type === 'PropertyDefinition' &&
54+
bodyNode.key.name === 'styles' &&
55+
bodyNode.static
56+
);
57+
});
58+
if (staticStylesProperty) {
59+
const lastProperty = node.body.body[node.body.body.length - 1];
60+
if (lastProperty.key.name !== staticStylesProperty.key.name) {
61+
context.report({
62+
node: staticStylesProperty,
63+
message:
64+
'The "styles" property should be the last property of a class declaration.',
65+
data: {
66+
className: className,
67+
},
68+
fix: function (fixer) {
69+
const sourceCode = context.getSourceCode();
70+
const staticStylesPropertyText =
71+
sourceCode.getText(staticStylesProperty);
72+
return [
73+
fixer.replaceTextRange(staticStylesProperty.range, ''),
74+
fixer.insertTextAfterRange(
75+
lastProperty.range,
76+
'\n \n ' + staticStylesPropertyText
77+
),
78+
];
79+
},
80+
});
81+
}
82+
}
83+
}
84+
},
85+
};
86+
},
87+
},
88+
};

package-lock.json

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"eslint-plugin-import": "2.27.5",
9393
"eslint-plugin-lit": "1.8.3",
9494
"eslint-plugin-lit-a11y": "4.1.0",
95+
"eslint-plugin-local-rules": "^2.0.0",
9596
"eslint-plugin-storybook": "0.6.14",
9697
"eslint-plugin-wc": "1.5.0",
9798
"github-markdown-css": "5.2.0",

packages/uui-avatar-group/lib/uui-avatar-group.element.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@ import { property, queryAssignedElements, state } from 'lit/decorators.js';
1010
*/
1111
@defineElement('uui-avatar-group')
1212
export class UUIAvatarGroupElement extends LitElement {
13-
static styles = [
14-
css`
15-
:host {
16-
display: inline-flex;
17-
align-items: center;
18-
padding-left: 3px;
19-
padding-right: 3px;
20-
}
21-
22-
::slotted(uui-avatar) {
23-
margin-left: -0.2em;
24-
margin-right: -0.2em;
25-
border: 0.1em solid var(--uui-avatar-border-color);
26-
}
27-
28-
#overflow-indication {
29-
margin-left: 6px;
30-
}
31-
`,
32-
];
33-
3413
@queryAssignedElements({
3514
selector: 'uui-avatar, [uui-avatar]',
3615
flatten: true,
@@ -88,6 +67,27 @@ export class UUIAvatarGroupElement extends LitElement {
8867
: ''}
8968
`;
9069
}
70+
71+
static styles = [
72+
css`
73+
:host {
74+
display: inline-flex;
75+
align-items: center;
76+
padding-left: 3px;
77+
padding-right: 3px;
78+
}
79+
80+
::slotted(uui-avatar) {
81+
margin-left: -0.2em;
82+
margin-right: -0.2em;
83+
border: 0.1em solid var(--uui-avatar-border-color);
84+
}
85+
86+
#overflow-indication {
87+
margin-left: 6px;
88+
}
89+
`,
90+
];
9191
}
9292

9393
declare global {

packages/uui-avatar/lib/uui-avatar.element.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,6 @@ import { property, state } from 'lit/decorators.js';
99
*/
1010
@defineElement('uui-avatar')
1111
export class UUIAvatarElement extends LitElement {
12-
static styles = [
13-
css`
14-
:host {
15-
display: inline-flex;
16-
align-items: center;
17-
justify-content: center;
18-
flex-shrink: 0;
19-
position: relative;
20-
overflow: hidden;
21-
border-radius: 50%;
22-
font-weight: 700;
23-
-webkit-font-smoothing: subpixel-antialiased;
24-
width: calc(2em + 4px);
25-
height: calc(2em + 4px);
26-
user-select: none;
27-
/* box-sizing: border-box; */
28-
aspect-ratio: 1;
29-
background-color: var(--uui-palette-spanish-pink);
30-
color: var(--uui-palette-space-cadet);
31-
}
32-
33-
:host([overflow]) {
34-
overflow: unset;
35-
}
36-
37-
img {
38-
object-fit: cover;
39-
height: 100%;
40-
width: 100%;
41-
overflow: hidden;
42-
border-radius: 50%;
43-
}
44-
`,
45-
];
46-
4712
/**
4813
* Set to true to prevent content from getting hidden if going outside the parent. Useful in combination with something like a Badge.
4914
* @type {boolean}
@@ -136,6 +101,41 @@ export class UUIAvatarElement extends LitElement {
136101
<slot></slot>
137102
`;
138103
}
104+
105+
static styles = [
106+
css`
107+
:host {
108+
display: inline-flex;
109+
align-items: center;
110+
justify-content: center;
111+
flex-shrink: 0;
112+
position: relative;
113+
overflow: hidden;
114+
border-radius: 50%;
115+
font-weight: 700;
116+
-webkit-font-smoothing: subpixel-antialiased;
117+
width: calc(2em + 4px);
118+
height: calc(2em + 4px);
119+
user-select: none;
120+
/* box-sizing: border-box; */
121+
aspect-ratio: 1;
122+
background-color: var(--uui-palette-spanish-pink);
123+
color: var(--uui-palette-space-cadet);
124+
}
125+
126+
:host([overflow]) {
127+
overflow: unset;
128+
}
129+
130+
img {
131+
object-fit: cover;
132+
height: 100%;
133+
width: 100%;
134+
overflow: hidden;
135+
border-radius: 50%;
136+
}
137+
`,
138+
];
139139
}
140140

141141
declare global {

packages/uui-badge/lib/uui-badge.element.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ import type {
1414

1515
@defineElement('uui-badge')
1616
export class UUIBadgeElement extends LitElement {
17+
/**
18+
* Changes the look of the button to one of the predefined, symbolic looks. For example - set this to positive if you want nice, green "confirm" button.
19+
* @type {"default" | "positive" | "warning" | "danger"}
20+
* @attr
21+
* @default "default"
22+
*/
23+
@property({ reflect: true })
24+
color: InterfaceColor = 'default';
25+
26+
/**
27+
* Changes the look of the button to one of the predefined, symbolic looks. For example - set this to positive if you want nice, green "confirm" button.
28+
* @type {"default" | "primary" | "secondary" | "outline" | "placeholder"}
29+
* @attr
30+
* @default "default"
31+
*/
32+
@property({ reflect: true })
33+
look: InterfaceLook = 'primary';
34+
35+
/**
36+
* Bring attention to this badge by applying a bounce animation.
37+
* @type Boolean
38+
* @attr
39+
* @default false
40+
*/
41+
@property({ type: Boolean, reflect: true })
42+
attention = false;
43+
44+
render() {
45+
return html` <slot></slot> `;
46+
}
47+
1748
static styles = [
1849
css`
1950
:host {
@@ -127,37 +158,6 @@ export class UUIBadgeElement extends LitElement {
127158
}
128159
`,
129160
];
130-
131-
/**
132-
* Changes the look of the button to one of the predefined, symbolic looks. For example - set this to positive if you want nice, green "confirm" button.
133-
* @type {"default" | "positive" | "warning" | "danger"}
134-
* @attr
135-
* @default "default"
136-
*/
137-
@property({ reflect: true })
138-
color: InterfaceColor = 'default';
139-
140-
/**
141-
* Changes the look of the button to one of the predefined, symbolic looks. For example - set this to positive if you want nice, green "confirm" button.
142-
* @type {"default" | "primary" | "secondary" | "outline" | "placeholder"}
143-
* @attr
144-
* @default "default"
145-
*/
146-
@property({ reflect: true })
147-
look: InterfaceLook = 'primary';
148-
149-
/**
150-
* Bring attention to this badge by applying a bounce animation.
151-
* @type Boolean
152-
* @attr
153-
* @default false
154-
*/
155-
@property({ type: Boolean, reflect: true })
156-
attention = false;
157-
158-
render() {
159-
return html` <slot></slot> `;
160-
}
161161
}
162162

163163
declare global {

0 commit comments

Comments
 (0)