@swiftwc/eslint-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc.yml +4 -0
- package/README.md +3 -0
- package/emberjs/en.js +20 -0
- package/html/en.js +20 -0
- package/package.json +32 -0
- package/src/index.js +145 -0
package/.prettierrc.yml
ADDED
package/README.md
ADDED
package/emberjs/en.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RULES, swiftwc, validate } from '../src/index.js'
|
|
2
|
+
|
|
3
|
+
swiftwc.rules['allowed-tags'].meta.docs.description = 'Restrict allowed HTML tags'
|
|
4
|
+
|
|
5
|
+
swiftwc.rules['allowed-tags'].meta.messages.disallowedTag = 'Tag <{{tag}}> is only allowed inside any of: {{allowed}}'
|
|
6
|
+
|
|
7
|
+
swiftwc.rules['allowed-tags'].create = (context) => {
|
|
8
|
+
return {
|
|
9
|
+
ElementNode(node) {
|
|
10
|
+
const tag = node.tag
|
|
11
|
+
|
|
12
|
+
const allowedParents = RULES[tag]
|
|
13
|
+
if (!allowedParents) return // 👈 ignore unknown tags completely
|
|
14
|
+
|
|
15
|
+
validate(tag, (item) => item.tag, allowedParents, context, node)
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default swiftwc
|
package/html/en.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RULES, swiftwc, validate } from '../src/index.js'
|
|
2
|
+
|
|
3
|
+
swiftwc.rules['allowed-tags'].meta.docs.description = 'Restrict allowed HTML tags'
|
|
4
|
+
|
|
5
|
+
swiftwc.rules['allowed-tags'].meta.messages.disallowedTag = 'Tag <{{tag}}> is only allowed inside any of: {{allowed}}'
|
|
6
|
+
|
|
7
|
+
swiftwc.rules['allowed-tags'].create = (context) => {
|
|
8
|
+
return {
|
|
9
|
+
Tag(node) {
|
|
10
|
+
const tag = node.name
|
|
11
|
+
|
|
12
|
+
const allowedParents = RULES[tag]
|
|
13
|
+
if (!allowedParents) return // 👈 ignore unknown tags completely
|
|
14
|
+
|
|
15
|
+
validate(tag, (item) => item.parent, allowedParents, context, node)
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default swiftwc
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@swiftwc/eslint-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Rules enforcing best practices while using SwiftWC.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"eslint",
|
|
7
|
+
"eslintplugin",
|
|
8
|
+
"eslint-plugin"
|
|
9
|
+
],
|
|
10
|
+
"license": "SEE LICENSE IN ../../LICENSE.md",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "The SwiftWC/UI Authors",
|
|
13
|
+
"url": "https://github.com/swiftwc/ui/graphs/contributors"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/swiftwc/ui.git",
|
|
18
|
+
"directory": "packages/eslint-plugin"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/swiftwc/ui/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@swiftwc/ui": "^1.0.0",
|
|
26
|
+
"eslint": ">=10.0.0"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
"./html/en": "./html/en.js",
|
|
30
|
+
"./emberjs/en": "./emberjs/en.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { readFileSync } from 'fs'
|
|
2
|
+
|
|
3
|
+
export const RULES = {
|
|
4
|
+
'v-keyboard': ['body'],
|
|
5
|
+
|
|
6
|
+
'scroll-view': ['body-view', 'dialog', 'navigation-stack', 'navigation-split-view', 'detail-placeholder'],
|
|
7
|
+
|
|
8
|
+
dialog: ['tab-view', 'template'],
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function validate(tag, getParentTag, allowedParents, context, node) {
|
|
12
|
+
// let parent = getParentTag(node)
|
|
13
|
+
// let isValid = false
|
|
14
|
+
|
|
15
|
+
// while (parent) {
|
|
16
|
+
// if (parent.type === 'Tag' && allowedParents.includes(parent.name)) {
|
|
17
|
+
// isValid = true
|
|
18
|
+
// break
|
|
19
|
+
// }
|
|
20
|
+
// parent = getParentTag(parent)
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
const parentTag = getParentTag(node)
|
|
24
|
+
|
|
25
|
+
const isValid = allowedParents.includes(parentTag.name)
|
|
26
|
+
|
|
27
|
+
if (isValid) return
|
|
28
|
+
|
|
29
|
+
context.report({
|
|
30
|
+
node,
|
|
31
|
+
messageId: 'disallowedTag',
|
|
32
|
+
data: {
|
|
33
|
+
tag,
|
|
34
|
+
allowed: allowedParents.join(', '),
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const { name, version } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
40
|
+
|
|
41
|
+
export const swiftwc = {
|
|
42
|
+
meta: {
|
|
43
|
+
name,
|
|
44
|
+
version,
|
|
45
|
+
namespace: 'swiftwc',
|
|
46
|
+
},
|
|
47
|
+
configs: {},
|
|
48
|
+
rules: {
|
|
49
|
+
'allowed-tags': {
|
|
50
|
+
meta: {
|
|
51
|
+
type: 'problem',
|
|
52
|
+
docs: {
|
|
53
|
+
description: 'Restrict allowed HTML tags',
|
|
54
|
+
url: `https://github.com/swiftwc/ui`,
|
|
55
|
+
},
|
|
56
|
+
languages: ['html/html'],
|
|
57
|
+
schema: [],
|
|
58
|
+
// schema: [
|
|
59
|
+
// {
|
|
60
|
+
// type: 'object',
|
|
61
|
+
// properties: {
|
|
62
|
+
// allowed: {
|
|
63
|
+
// type: 'array',
|
|
64
|
+
// items: { type: 'string' },
|
|
65
|
+
// },
|
|
66
|
+
// },
|
|
67
|
+
// additionalProperties: false,
|
|
68
|
+
// },
|
|
69
|
+
// ],
|
|
70
|
+
messages: {
|
|
71
|
+
disallowedTag: 'Tag <{{tag}}> is only allowed inside any of: {{allowed}}',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
// create(context) {
|
|
76
|
+
// const options = context.options?.[0] || {},
|
|
77
|
+
// allowed = new Set(options.allowed || [...DEFAULT_ALLOWED])
|
|
78
|
+
|
|
79
|
+
// return {
|
|
80
|
+
// Tag(node) {
|
|
81
|
+
// const tag = node.name
|
|
82
|
+
// if (!allowed.has(tag)) {
|
|
83
|
+
// context.report({
|
|
84
|
+
// node,
|
|
85
|
+
// messageId: 'disallowedTag',
|
|
86
|
+
// data: {
|
|
87
|
+
// tag,
|
|
88
|
+
// allowed: [...allowed].join(', '),
|
|
89
|
+
// },
|
|
90
|
+
// })
|
|
91
|
+
// }
|
|
92
|
+
// },
|
|
93
|
+
// }
|
|
94
|
+
// },
|
|
95
|
+
|
|
96
|
+
create(context) {
|
|
97
|
+
return {
|
|
98
|
+
Tag(node) {
|
|
99
|
+
const tag = node.name
|
|
100
|
+
|
|
101
|
+
const allowedParents = RULES[tag]
|
|
102
|
+
if (!allowedParents) return // 👈 ignore unknown tags completely
|
|
103
|
+
|
|
104
|
+
validate(tag, (item) => item.parent, allowedParents, context, node)
|
|
105
|
+
|
|
106
|
+
// let parent = node.parent
|
|
107
|
+
// let valid = false
|
|
108
|
+
|
|
109
|
+
// while (parent) {
|
|
110
|
+
// if (parent.type === 'Tag' && allowedParents.includes(parent.name)) {
|
|
111
|
+
// valid = true
|
|
112
|
+
// break
|
|
113
|
+
// }
|
|
114
|
+
// parent = parent.parent
|
|
115
|
+
// }
|
|
116
|
+
|
|
117
|
+
// if (!valid) {
|
|
118
|
+
// context.report({
|
|
119
|
+
// node,
|
|
120
|
+
// messageId: 'disallowedTag',
|
|
121
|
+
// data: {
|
|
122
|
+
// tag,
|
|
123
|
+
// allowed: allowedParents.join(', '),
|
|
124
|
+
// },
|
|
125
|
+
// })
|
|
126
|
+
// }
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Object.assign(swiftwc.configs, {
|
|
135
|
+
recommended: [
|
|
136
|
+
{
|
|
137
|
+
plugins: {
|
|
138
|
+
swiftwc,
|
|
139
|
+
},
|
|
140
|
+
rules: {
|
|
141
|
+
'swiftwc/allowed-tags': 'error', //['error', { allowed: ['div', 'span', 'my-button', 'my-card'] }],
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
})
|