File size: 3,541 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const css_utils_1 = require("../utils/css-utils");
exports.default = (0, utils_1.createRule)('no-dupe-style-properties', {
    meta: {
        docs: {
            description: 'disallow duplicate style properties',
            category: 'Possible Errors',
            recommended: true
        },
        schema: [],
        messages: {
            unexpected: "Duplicate property '{{name}}'."
        },
        type: 'problem'
    },
    create(context) {
        return {
            SvelteStartTag(node) {
                const reported = new Set();
                const beforeDeclarations = new Map();
                for (const { decls } of iterateStyleDeclSetFromAttrs(node.attributes)) {
                    for (const decl of decls) {
                        const already = beforeDeclarations.get(decl.prop);
                        if (already) {
                            for (const report of [already, decl].filter((n) => !reported.has(n))) {
                                context.report({
                                    node,
                                    loc: report.loc,
                                    messageId: 'unexpected',
                                    data: { name: report.prop }
                                });
                                reported.add(report);
                            }
                        }
                    }
                    for (const decl of decls) {
                        beforeDeclarations.set(decl.prop, decl);
                    }
                }
            }
        };
        /** Iterate the style decl set from attrs */
        function* iterateStyleDeclSetFromAttrs(attrs) {
            for (const attr of attrs) {
                if (attr.type === 'SvelteStyleDirective') {
                    yield {
                        decls: [{ prop: attr.key.name.name, loc: attr.key.name.loc }]
                    };
                }
                else if (attr.type === 'SvelteAttribute') {
                    if (attr.key.name !== 'style') {
                        continue;
                    }
                    const root = (0, css_utils_1.parseStyleAttributeValue)(attr, context);
                    if (!root) {
                        continue;
                    }
                    yield* iterateStyleDeclSetFromStyleRoot(root);
                }
            }
        }
        /** Iterate the style decl set from style root */
        function* iterateStyleDeclSetFromStyleRoot(root) {
            for (const child of root.nodes) {
                if (child.type === 'decl') {
                    yield {
                        decls: [
                            {
                                prop: child.prop.name,
                                get loc() {
                                    return child.prop.loc;
                                }
                            }
                        ]
                    };
                }
                else if (child.type === 'inline') {
                    const decls = [];
                    for (const root of child.getAllInlineStyles().values()) {
                        for (const set of iterateStyleDeclSetFromStyleRoot(root)) {
                            decls.push(...set.decls);
                        }
                    }
                    yield { decls };
                }
            }
        }
    }
});