Skip to content

Commit a698dcb

Browse files
authored
Refactor (#133)
1 parent edb6c24 commit a698dcb

File tree

5 files changed

+124
-71
lines changed

5 files changed

+124
-71
lines changed

src/rules/no-dupe-style-properties.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { AST } from "svelte-eslint-parser"
22
import { createRule } from "../utils"
3-
import type { SvelteStyleInlineRoot, SvelteStyleRoot } from "../utils/css-utils"
3+
import type {
4+
SvelteStyleInterpolation,
5+
SvelteStyleRoot,
6+
} from "../utils/css-utils"
47
import { parseStyleAttributeValue } from "../utils/css-utils"
58

69
export default createRule("no-dupe-style-properties", {
@@ -76,9 +79,9 @@ export default createRule("no-dupe-style-properties", {
7679
}
7780

7881
/** Iterate the style decl set from style root */
79-
function* iterateStyleDeclSetFromStyleRoot(
80-
root: SvelteStyleRoot | SvelteStyleInlineRoot,
81-
): Iterable<StyleDeclSet> {
82+
function* iterateStyleDeclSetFromStyleRoot<
83+
E extends SvelteStyleInterpolation,
84+
>(root: SvelteStyleRoot<E>): Iterable<StyleDeclSet> {
8285
for (const child of root.nodes) {
8386
if (child.type === "decl") {
8487
yield {

src/rules/no-shorthand-style-property-overrides.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { AST } from "svelte-eslint-parser"
22
import { createRule } from "../utils"
3-
import type { SvelteStyleInlineRoot, SvelteStyleRoot } from "../utils/css-utils"
3+
import type {
4+
SvelteStyleInterpolation,
5+
SvelteStyleRoot,
6+
} from "../utils/css-utils"
47
import {
58
getVendorPrefix,
69
stripVendorPrefix,
@@ -91,9 +94,9 @@ export default createRule("no-shorthand-style-property-overrides", {
9194
}
9295

9396
/** Iterate the style decl set from style root */
94-
function* iterateStyleDeclSetFromStyleRoot(
95-
root: SvelteStyleRoot | SvelteStyleInlineRoot,
96-
): Iterable<StyleDeclSet> {
97+
function* iterateStyleDeclSetFromStyleRoot<
98+
E extends SvelteStyleInterpolation,
99+
>(root: SvelteStyleRoot<E>): Iterable<StyleDeclSet> {
97100
for (const child of root.nodes) {
98101
if (child.type === "decl") {
99102
yield {

src/rules/prefer-style-directive.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default createRule("prefer-style-directive", {
3838
*/
3939
function processStyleValue(
4040
node: AST.SvelteAttribute,
41-
root: SvelteStyleRoot,
41+
root: SvelteStyleRoot<AST.SvelteMustacheTagText>,
4242
) {
4343
for (const child of root.nodes) {
4444
if (child.type === "decl") {
@@ -54,10 +54,15 @@ export default createRule("prefer-style-directive", {
5454
*/
5555
function processDeclaration(
5656
attrNode: AST.SvelteAttribute,
57-
root: SvelteStyleRoot,
58-
decl: SvelteStyleDeclaration,
57+
root: SvelteStyleRoot<AST.SvelteMustacheTagText>,
58+
decl: SvelteStyleDeclaration<AST.SvelteMustacheTagText>,
5959
) {
60-
if (decl.important || decl.unsafe) return
60+
if (
61+
decl.important ||
62+
decl.unknownInterpolations.length ||
63+
decl.prop.interpolations.length
64+
)
65+
return
6166
if (
6267
attrNode.parent.attributes.some(
6368
(attr) =>
@@ -101,8 +106,8 @@ export default createRule("prefer-style-directive", {
101106
*/
102107
function processInline(
103108
attrNode: AST.SvelteAttribute,
104-
root: SvelteStyleRoot,
105-
inline: SvelteStyleInline,
109+
root: SvelteStyleRoot<AST.SvelteMustacheTagText>,
110+
inline: SvelteStyleInline<AST.SvelteMustacheTagText>,
106111
) {
107112
const node = inline.node.expression
108113

@@ -194,8 +199,10 @@ export default createRule("prefer-style-directive", {
194199
/** Remove style */
195200
function removeStyle(
196201
fixer: RuleFixer,
197-
root: SvelteStyleRoot,
198-
node: SvelteStyleDeclaration | SvelteStyleInline,
202+
root: SvelteStyleRoot<AST.SvelteMustacheTagText>,
203+
node:
204+
| SvelteStyleDeclaration<AST.SvelteMustacheTagText>
205+
| SvelteStyleInline<AST.SvelteMustacheTagText>,
199206
) {
200207
const index = root.nodes.indexOf(node)
201208
const after = root.nodes[index + 1]

src/rules/require-optimized-style-attribute.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,22 @@ export default createRule("require-optimized-style-attribute", {
3737
}
3838
const root = parseStyleAttributeValue(node, context)
3939
if (!root) {
40+
context.report({
41+
node,
42+
messageId: "complex",
43+
})
4044
return
4145
}
4246

4347
for (const child of root.nodes) {
4448
if (child.type === "decl") {
45-
if (child.unsafe) {
49+
if (child.unknownInterpolations.length) {
4650
context.report({
4751
node,
4852
loc: child.loc,
4953
messageId: "complex",
5054
})
51-
} else if (child.prop.name.includes("{")) {
55+
} else if (child.prop.interpolations.length) {
5256
context.report({
5357
node,
5458
loc: child.prop.loc,

0 commit comments

Comments
 (0)