From 5a3b6990f79aebd6107f9d74491e1a6b79d8a2a1 Mon Sep 17 00:00:00 2001 From: ChihebSMIDA Date: Wed, 29 May 2024 16:24:50 +0200 Subject: [PATCH 1/4] Add rule "Avoid autoplay" Co-authored-by: Diane Cordier --- eslint-plugin/README.md | 25 +++--- eslint-plugin/docs/rules/avoid-autoplay.md | 46 ++++++++++ eslint-plugin/lib/rules/avoid-autoplay.js | 78 +++++++++++++++++ .../tests/lib/rules/avoid-autoplay.js | 84 +++++++++++++++++++ .../java/io/ecocode/javascript/CheckList.java | 3 +- .../javascript/checks/AvoidAutoPlay.java | 34 ++++++++ .../profiles/ecocode_javascript_profile.json | 3 +- .../profiles/ecocode_typescript_profile.json | 3 +- .../io/ecocode/rules/javascript/EC36.html | 10 +++ .../io/ecocode/rules/javascript/EC36.json | 22 +++++ 10 files changed, 293 insertions(+), 15 deletions(-) create mode 100644 eslint-plugin/docs/rules/avoid-autoplay.md create mode 100644 eslint-plugin/lib/rules/avoid-autoplay.js create mode 100644 eslint-plugin/tests/lib/rules/avoid-autoplay.js create mode 100644 sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidAutoPlay.java create mode 100644 sonar-plugin/src/main/resources/io/ecocode/rules/javascript/EC36.html create mode 100644 sonar-plugin/src/main/resources/io/ecocode/rules/javascript/EC36.json diff --git a/eslint-plugin/README.md b/eslint-plugin/README.md index a5e19f7..376065d 100644 --- a/eslint-plugin/README.md +++ b/eslint-plugin/README.md @@ -67,18 +67,19 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c ⚠️ Configurations set to warn in.\ ✅ Set in the `recommended` configuration. -| Name | Description | ⚠️ | -| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :- | -| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations | ✅ | -| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. | ✅ | -| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query | ✅ | -| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute | ✅ | -| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library | ✅ | -| [no-multiple-access-dom-element](docs/rules/no-multiple-access-dom-element.md) | Disallow multiple access of same DOM element. | ✅ | -| [no-multiple-style-changes](docs/rules/no-multiple-style-changes.md) | Disallow multiple style changes at once. | ✅ | -| [prefer-collections-with-pagination](docs/rules/prefer-collections-with-pagination.md) | Prefer API collections with pagination. | ✅ | -| [prefer-shorthand-css-notations](docs/rules/prefer-shorthand-css-notations.md) | Encourage usage of shorthand CSS notations | ✅ | -| [provide-print-css](docs/rules/provide-print-css.md) | Enforce providing a print stylesheet | ✅ | +| Name                               | Description | ⚠️ | +| :------------------------------------------------------------------------------------- | :------------------------------------------------------------------------ | :- | +| [avoid-autoplay](docs/rules/avoid-autoplay.md) | Disallow autoplay and enforce preload='none' for video and audio elements | ✅ | +| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations | ✅ | +| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. | ✅ | +| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query | ✅ | +| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute | ✅ | +| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library | ✅ | +| [no-multiple-access-dom-element](docs/rules/no-multiple-access-dom-element.md) | Disallow multiple access of same DOM element. | ✅ | +| [no-multiple-style-changes](docs/rules/no-multiple-style-changes.md) | Disallow multiple style changes at once. | ✅ | +| [prefer-collections-with-pagination](docs/rules/prefer-collections-with-pagination.md) | Prefer API collections with pagination. | ✅ | +| [prefer-shorthand-css-notations](docs/rules/prefer-shorthand-css-notations.md) | Encourage usage of shorthand CSS notations | ✅ | +| [provide-print-css](docs/rules/provide-print-css.md) | Enforce providing a print stylesheet | ✅ | diff --git a/eslint-plugin/docs/rules/avoid-autoplay.md b/eslint-plugin/docs/rules/avoid-autoplay.md new file mode 100644 index 0000000..03cb6ab --- /dev/null +++ b/eslint-plugin/docs/rules/avoid-autoplay.md @@ -0,0 +1,46 @@ +# Disallow autoplay and enforce preload='none' for video and audio elements (`@ecocode/avoid-autoplay`) + +⚠️ This rule _warns_ in the ✅ `recommended` config. + + + +⚠️ This rule _warns_ in the ✅ `recommended` config. + + + +## Why is this an issue? + +Automatic videos and audio files activation (autoplay) during web pages loading uses resources on each tier (device, +network, data center). In many cases, automatic playback is not necessary. Moreover, it can draw users' attention and +distract them from the initially requested service. Therefore, whenever possible, these playbacks should be initiated by +the users and by not using the autoplay attributes in the `