-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathavoid-autoplay.js
More file actions
109 lines (98 loc) · 3.58 KB
/
Copy pathavoid-autoplay.js
File metadata and controls
109 lines (98 loc) · 3.58 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Avoid autoplay for videos and audio content",
category: "eco-design",
recommended: "warn",
},
messages: {
NoAutoplay: "Avoid autoplay for video and audio elements.",
EnforcePreloadNone: "Set preload='none' for video and audio elements.",
NoAutoplayAndEnforcePreloadNone:
"Avoid autoplay and set preload='none' for video and audio elements.",
},
schema: [],
},
create(context) {
const reportAutoplay = (autoplayAttr, preloadAttr, preloadValue, fallback) => {
if (autoplayAttr && preloadValue !== "none") {
context.report({
node: autoplayAttr || preloadAttr,
messageId: "NoAutoplayAndEnforcePreloadNone",
});
return;
}
if (autoplayAttr) {
context.report({
node: autoplayAttr,
messageId: "NoAutoplay",
});
}
if (!preloadAttr || preloadValue !== "none") {
context.report({
node: preloadAttr || fallback,
messageId: "EnforcePreloadNone",
});
}
};
const parserServices =
context.parserServices || context.sourceCode?.parserServices;
const vueTemplateVisitor = parserServices?.defineTemplateBodyVisitor
? parserServices.defineTemplateBodyVisitor({
VElement(node) {
const rawName =
typeof node.name === "string"
? node.name
: node.name?.name || node.rawName;
const name = rawName?.toLowerCase();
if (name !== "video" && name !== "audio") return;
const getAttr = (attrName) =>
node.startTag.attributes.find(
(attr) =>
attr.type === "VAttribute" &&
attr.key?.name?.toLowerCase() === attrName,
);
const autoplayAttr = getAttr("autoplay");
const preloadAttr = getAttr("preload");
const preloadValue = preloadAttr?.value?.value;
reportAutoplay(autoplayAttr, preloadAttr, preloadValue, node);
},
})
: {};
return {
JSXOpeningElement(node) {
if (node.name.name === "video" || node.name.name === "audio") {
const autoplayAttr = node.attributes.find(
(attr) => attr.name?.name.toLowerCase() === "autoplay",
);
const preloadAttr = node.attributes.find(
(attr) => attr.name?.name.toLowerCase() === "preload",
);
const preloadValue = preloadAttr?.value?.value;
reportAutoplay(autoplayAttr, preloadAttr, preloadValue, node);
}
},
...vueTemplateVisitor,
};
},
};