-
-
Notifications
You must be signed in to change notification settings - Fork 454
feat: added new rule "attr-space-between" #1459
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
Open
actengage
wants to merge
1
commit into
htmlhint:main
Choose a base branch
from
ActiveEngagement:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,23 @@ | ||
| import { Rule } from '../types' | ||
|
|
||
| export default { | ||
| id: 'attr-space-between', | ||
| description: 'Attributes must be separated by a space.', | ||
| init(parser, reporter) { | ||
| parser.addListener('tagstart', (event) => { | ||
| const col = event.col + event.tagName.length + 1 | ||
|
|
||
| for (const { index, name, raw } of event.attrs) { | ||
| if (!raw.match(/^\s/)) { | ||
| reporter.error( | ||
| `Attribute [ ${name} ] must be separated with a space.`, | ||
| event.line, | ||
| col + index, | ||
| this, | ||
| event.raw | ||
| ) | ||
| } | ||
| } | ||
| }) | ||
| }, | ||
| } as Rule |
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,43 @@ | ||
| const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
|
|
||
| const ruleId = 'attr-space-between' | ||
| const ruleOptions = {} | ||
|
|
||
| ruleOptions[ruleId] = true | ||
|
|
||
| describe(`Rules: ${ruleId}`, () => { | ||
| it('Attributes without a space between them should result in an error', () => { | ||
| const code = '<div id="foo"class="bar"></div>' | ||
| const messages = HTMLHint.verify(code, ruleOptions) | ||
| expect(messages.length).toBe(1) | ||
| expect(messages[0].rule.id).toBe(ruleId) | ||
| expect(messages[0].message).toBe( | ||
| 'Attribute [ class ] must be separated with a space.' | ||
| ) | ||
| }) | ||
|
|
||
| it('Multiple attributes without spaces should each result in an error', () => { | ||
| const code = '<div id="foo"class="bar"title="baz"></div>' | ||
| const messages = HTMLHint.verify(code, ruleOptions) | ||
| expect(messages.length).toBe(2) | ||
| messages.forEach((msg) => expect(msg.rule.id).toBe(ruleId)) | ||
| }) | ||
|
|
||
| it('Attributes separated by spaces should not result in an error', () => { | ||
| const code = '<div id="foo" class="bar"></div>' | ||
| const messages = HTMLHint.verify(code, ruleOptions) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('Unquoted and valueless attributes should not result in an error', () => { | ||
| const code = '<input type=checkbox checked>' | ||
| const messages = HTMLHint.verify(code, ruleOptions) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
|
|
||
| it('Attributes separated by newlines should not result in an error', () => { | ||
| const code = '<div id="foo"\n class="bar"></div>' | ||
| const messages = HTMLHint.verify(code, ruleOptions) | ||
| expect(messages.length).toBe(0) | ||
| }) | ||
| }) |
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,39 @@ | ||
| --- | ||
| id: attr-space-between | ||
| title: attr-space-between | ||
| description: Attributes must be separated by a space. | ||
| sidebar: | ||
| badge: New | ||
| --- | ||
|
|
||
| import { Badge } from '@astrojs/starlight/components'; | ||
|
|
||
| Attributes must be separated by a space. | ||
|
|
||
| Level: <Badge text="Error" variant="danger" /> | ||
|
|
||
| ## Config value | ||
|
|
||
| - `true`: enable rule | ||
| - `false`: disable rule | ||
|
|
||
| ## Description | ||
|
|
||
| Without a space between attributes, browsers recover from the markup, but the | ||
| HTMLHint parser interprets the tag as text — which produces misleading errors | ||
| from other rules (such as `spec-char-escape` and `tag-pair`) instead of | ||
| pointing at the real problem. This rule reports the missing space directly. | ||
|
|
||
| ### The following patterns are **not** considered rule violations | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```html | ||
| <div class="foo" id="bar"></div> | ||
| ``` | ||
|
|
||
| ### The following patterns are considered rule violations: | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```html | ||
| <div class="foo"id="bar"></div> | ||
| ``` |
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
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.
Check failure
Code scanning / CodeQL
Bad HTML filtering regexp