-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathno-spaces-around-equal-signs-in-attribute.ts
74 lines (67 loc) · 1.94 KB
/
no-spaces-around-equal-signs-in-attribute.ts
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
import { createRule } from "../utils"
import type { AST } from "svelte-eslint-parser"
export default createRule("no-spaces-around-equal-signs-in-attribute", {
meta: {
docs: {
description: "disallow spaces around equal signs in attribute",
category: "Stylistic Issues",
recommended: false,
conflictWithPrettier: true,
},
schema: {},
fixable: "whitespace",
messages: {
noSpaces: "Unexpected spaces found around equal signs.",
},
type: "layout",
},
create(ctx) {
const source = ctx.getSourceCode()
/**
* Returns source text between attribute key and value, and range of that source
*/
function getAttrEq(
node:
| AST.SvelteAttribute
| AST.SvelteDirective
| AST.SvelteStyleDirective
| AST.SvelteSpecialDirective,
): [string, AST.Range] {
const keyRange = node.key.range
const eqSource = /^[\s=]*/u.exec(
source.text.slice(keyRange[1], node.range[1]),
)![0]
const valueStart = keyRange[1] + eqSource.length
return [eqSource, [keyRange[1], valueStart]]
}
/**
* Returns true if string contains whitespace characters
*/
function containsWhitespace(string: string): boolean {
return /\s/u.test(string)
}
return {
"SvelteAttribute, SvelteDirective, SvelteStyleDirective, SvelteSpecialDirective"(
node:
| AST.SvelteAttribute
| AST.SvelteDirective
| AST.SvelteStyleDirective
| AST.SvelteSpecialDirective,
) {
const [eqSource, range] = getAttrEq(node)
if (!containsWhitespace(eqSource)) return
const loc = {
start: source.getLocFromIndex(range[0]),
end: source.getLocFromIndex(range[1]),
}
ctx.report({
loc,
messageId: "noSpaces",
*fix(fixer) {
yield fixer.replaceTextRange(range, "=")
},
})
},
}
},
})