Skip to content

Commit

Permalink
[ec36-avoid-autoplay] fix : eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
chihebtalan committed May 30, 2024
1 parent f067a0f commit b95b6e9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 87 deletions.
103 changes: 54 additions & 49 deletions eslint-plugin/lib/rules/avoid-autoplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,61 @@

"use strict";

/** @type {import('eslint').Rule.RuleModule} */
/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Disallow autoplay and enforce preload='none' for video and audio elements",
category: "eco-design",
recommended: "warn",
},
messages: {
NoAutoplay: "Avoid using autoplay attribute for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html",
EnforcePreloadNone: "Set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html",
NoAutoplay_EnforcePreloadNone: "Avoid using autoplay attribute and set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html ",
},
schema: [],
meta: {
type: "suggestion",
docs: {
description:
"Disallow autoplay and enforce preload='none' for video and audio elements",
category: "eco-design",
recommended: "warn",
},
create(context) {
return {
JSXOpeningElement(node) {
if (node.name.name === "video" || node.name.name === "audio") {
const autoplayAttr = node.attributes.find(
(attr) => attr.name.name === "autoplay",
);
const preloadAttr = node.attributes.find(
(attr) => attr.name.name === "preload",
);
if(autoplayAttr && (!preloadAttr || preloadAttr.value.value !== "none")) {
context.report({
node: autoplayAttr||preloadAttr,
messageId: "NoAutoplay_EnforcePreloadNone",
});
}
else {
if (autoplayAttr) {
context.report({
node: autoplayAttr,
messageId: "NoAutoplay",

});
}

if (!preloadAttr || preloadAttr.value.value !== "none") {
context.report({
node: preloadAttr || node,
messageId: "EnforcePreloadNone",
});
}
}
}
},
};
messages: {
NoAutoplay:
"Avoid using autoplay attribute for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html",
EnforcePreloadNone:
"Set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html",
NoAutoplay_EnforcePreloadNone:
"Avoid using autoplay attribute and set preload='none' for <video> and <audio> elements. Reference to Rule RGESN 4.1 : https://www.arcep.fr/mes-demarches-et-services/entreprises/fiches-pratiques/referentiel-general-ecoconception-services-numeriques.html ",
},
schema: [],
},
create(context) {
return {
JSXOpeningElement(node) {
if (node.name.name === "video" || node.name.name === "audio") {
const autoplayAttr = node.attributes.find(
(attr) => attr.name.name === "autoplay",
);
const preloadAttr = node.attributes.find(
(attr) => attr.name.name === "preload",
);
if (
autoplayAttr &&
(!preloadAttr || preloadAttr.value.value !== "none")
) {
context.report({
node: autoplayAttr || preloadAttr,
messageId: "NoAutoplay_EnforcePreloadNone",
});
} else {
if (autoplayAttr) {
context.report({
node: autoplayAttr,
messageId: "NoAutoplay",
});
}

if (!preloadAttr || preloadAttr.value.value !== "none") {
context.report({
node: preloadAttr || node,
messageId: "EnforcePreloadNone",
});
}
}
}
},
};
},
};
72 changes: 34 additions & 38 deletions eslint-plugin/tests/lib/rules/avoid-autoplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,55 @@ const RuleTester = require("eslint").RuleTester;
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
});
const expectedError1 = {
messageId: "NoAutoplay",
type: "JSXAttribute",
messageId: "NoAutoplay",
type: "JSXAttribute",
};
const expectedError2 = {
messageId: "EnforcePreloadNone",
type: "JSXAttribute",
messageId: "EnforcePreloadNone",
type: "JSXAttribute",
};
const expectedError3 = {
messageId: "NoAutoplay_EnforcePreloadNone",
type: "JSXAttribute",
messageId: "NoAutoplay_EnforcePreloadNone",
type: "JSXAttribute",
};


ruleTester.run("autoplay-audio-video-attribute-not-present", rule, {
valid: [
`
valid: [
`
<audio preload="none"></audio>
`,
'<video preload="none"></video>',
],
'<video preload="none"></video>',
],

invalid: [
{
code: `
invalid: [
{
code: `
<audio autoplay></audio>
`,
errors: [expectedError3],
},
{
code: `
errors: [expectedError3],
},
{
code: `
<video autoplay preload="auto"></video>
`,
errors: [expectedError3],
}
,
{
code:'<video autoplay preload="none"></video>',
errors: [expectedError1],
}
,
{
code:'<audio preload="auto"></audio>',
errors: [expectedError2],
}

],
errors: [expectedError3],
},
{
code: '<video autoplay preload="none"></video>',
errors: [expectedError1],
},
{
code: '<audio preload="auto"></audio>',
errors: [expectedError2],
},
],
});

0 comments on commit b95b6e9

Please sign in to comment.