-
Notifications
You must be signed in to change notification settings - Fork 166
Add and enforce image size attributes for rendered images #7248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| module RuboCop | ||
| module Cop | ||
| module IdentityIdp | ||
| # This lint ensures that images rendered with Rails tag helpers include a size attribute | ||
| # (`width` and `height`, or `size`), which is a best practice to avoid layout shifts. | ||
| # | ||
| # @see https://web.dev/optimize-cls/#images-without-dimensions | ||
| # | ||
| # @example | ||
| # # bad | ||
| # image_tag 'example.svg' | ||
| # | ||
| # # good | ||
| # image_tag 'example.svg', width: 10, height: 20 | ||
| # | ||
| class ImageSizeLinter < RuboCop::Cop::Cop | ||
| MSG = 'Assign width and height to images'.freeze | ||
|
|
||
| RESTRICT_ON_SEND = [:image_tag] | ||
|
|
||
| def on_send(node) | ||
| add_offense(node, location: :expression) if !valid?(node) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def valid?(node) | ||
| options = node.arguments.last | ||
| return false if options.type != :hash | ||
| return true if options.child_nodes.any? { |child_node| child_node.type == :kwsplat } | ||
| key_names = options.keys.map { |key| key.value } | ||
| key_names.include?(:size) || (key_names.include?(:width) && key_names.include?(:height)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| require 'rubocop' | ||
| require 'rubocop/rspec/support' | ||
| require_relative '../../../lib/linters/image_size_linter' | ||
|
|
||
| describe RuboCop::Cop::IdentityIdp::ImageSizeLinter do | ||
| include CopHelper | ||
| include RuboCop::RSpec::ExpectOffense | ||
|
|
||
| let(:config) { RuboCop::Config.new } | ||
| let(:cop) { RuboCop::Cop::IdentityIdp::ImageSizeLinter.new(config) } | ||
|
|
||
| it 'registers offense when calling image_tag without any size attributes' do | ||
| expect_offense(<<~RUBY) | ||
| image_tag 'example.svg' | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ Assign width and height to images | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers offense when calling image_tag with only one of width or height' do | ||
| expect_offense(<<~RUBY) | ||
| image_tag 'example.svg', width: 10 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Assign width and height to images | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers no offense if there is ambiguous hash splatting' do | ||
| expect_no_offenses(<<~RUBY) | ||
| image_tag 'example.svg', **size_attributes | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers no offense when calling image_tag with size' do | ||
| expect_no_offenses(<<~RUBY) | ||
| image_tag 'example.svg', size: 10 | ||
| RUBY | ||
| end | ||
|
|
||
| it 'registers no offense when calling image_tag with width and height' do | ||
| expect_no_offenses(<<~RUBY) | ||
| image_tag 'example.svg', width: 10, height: 20 | ||
| RUBY | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting that I'd hoped to have disabled these rules inline at the specific point in the file where the
image_tagis called, but it doesn't currently appear to be possible to do with ERBLint.Upstream issue: Shopify/erb_lint#243