-
-
Notifications
You must be signed in to change notification settings - Fork 929
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
Add declaration-block-no-redundant-longhand-properties
autofix
#6580
Merged
mattxwang
merged 17 commits into
main
from
autofix-declaration-block-no-redundant-longhand-properties
Feb 10, 2023
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ea1f473
MVP for failing tests: add autofix
mattxwang d572fa0
add updated version of test approach on padding
mattxwang 1f85c7d
Merge branch 'main' into autofix-declaration-block-no-redundant-longh…
mattxwang 2317322
first pass for a complete-ish solution
mattxwang 51d102d
fix incorrect ordering
mattxwang dafa55f
add changeset
mattxwang b637c53
fix text-decoration longhand ordering
mattxwang 1263e9e
Uses insertion-order for `Set` instead of defining `orderedLonghandSu…
mattxwang 59345ae
Merge branch 'main' into autofix-declaration-block-no-redundant-longh…
jeddy3 2930872
Merge branch 'main' into autofix-declaration-block-no-redundant-longh…
mattxwang 8721e4b
add typedef to reduce type imports
mattxwang 806c2a6
Merge branch 'main' into autofix-declaration-block-no-redundant-longh…
mattxwang 52181fa
Apply suggestions from code review
mattxwang e46717b
Use `firstDeclNode` everywhere else
mattxwang 359e036
Applies @ybiquitous' (wonderful) suggestion
mattxwang 61a9d61
add TODO comment for text-decoration-thickness
mattxwang 3836097
Merge branch 'main' into autofix-declaration-block-no-redundant-longh…
mattxwang File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"stylelint": minor | ||
--- | ||
|
||
Added: `declaration-block-no-redundant-longhand-properties` autofix |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -18,12 +18,13 @@ const messages = ruleMessages(ruleName, { | |
|
||
const meta = { | ||
url: 'https://stylelint.io/user-guide/rules/declaration-block-no-redundant-longhand-properties', | ||
fixable: true, | ||
}; | ||
|
||
const IGNORED_VALUES = new Set(['inherit']); | ||
|
||
/** @type {import('stylelint').Rule} */ | ||
const rule = (primary, secondaryOptions) => { | ||
const rule = (primary, secondaryOptions, context) => { | ||
return (root, result) => { | ||
const validOptions = validateOptions( | ||
result, | ||
|
@@ -42,7 +43,7 @@ const rule = (primary, secondaryOptions) => { | |
return; | ||
} | ||
|
||
/** @type {Map<string, string[]>} */ | ||
/** @type {Map<string, import('stylelint').ShorthandProperties[]>} */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure the previous type was slightly incorrect, but could be wrong! |
||
const longhandToShorthands = new Map(); | ||
|
||
for (const [shorthand, longhandProps] of longhandSubPropertiesOfShorthandProperties.entries()) { | ||
|
@@ -61,6 +62,8 @@ const rule = (primary, secondaryOptions) => { | |
eachDeclarationBlock(root, (eachDecl) => { | ||
/** @type {Map<string, string[]>} */ | ||
const longhandDeclarations = new Map(); | ||
/** @type {Map<string, import('postcss').Declaration[]>} */ | ||
mattxwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const longhandDeclarationNodes = new Map(); | ||
|
||
eachDecl((decl) => { | ||
if (IGNORED_VALUES.has(decl.value)) { | ||
|
@@ -80,21 +83,69 @@ const rule = (primary, secondaryOptions) => { | |
for (const shorthandProperty of shorthandProperties) { | ||
const prefixedShorthandProperty = prefix + shorthandProperty; | ||
const longhandDeclaration = longhandDeclarations.get(prefixedShorthandProperty) || []; | ||
const longhandDeclarationNode = | ||
longhandDeclarationNodes.get(prefixedShorthandProperty) || []; | ||
|
||
longhandDeclaration.push(prop); | ||
longhandDeclarations.set(prefixedShorthandProperty, longhandDeclaration); | ||
|
||
const shorthandProps = /** @type {Map<string, Set<string>>} */ ( | ||
longhandSubPropertiesOfShorthandProperties | ||
).get(shorthandProperty); | ||
longhandDeclarationNode.push(decl); | ||
longhandDeclarationNodes.set(prefixedShorthandProperty, longhandDeclarationNode); | ||
|
||
const shorthandProps = | ||
/** @type {import('stylelint').LonghandSubPropertiesOfShorthandProperties} */ ( | ||
longhandSubPropertiesOfShorthandProperties | ||
).get(shorthandProperty); | ||
mattxwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const prefixedShorthandData = Array.from(shorthandProps || []).map( | ||
(item) => prefix + item, | ||
); | ||
|
||
if (!arrayEqual(prefixedShorthandData.sort(), longhandDeclaration.sort())) { | ||
const copiedPrefixedShorthandData = [...prefixedShorthandData]; | ||
|
||
if (!arrayEqual(copiedPrefixedShorthandData.sort(), longhandDeclaration.sort())) { | ||
continue; | ||
} | ||
|
||
if (context.fix) { | ||
const declNodes = longhandDeclarationNodes.get(prefixedShorthandProperty) || []; | ||
|
||
if (declNodes !== undefined && declNodes[0] !== undefined) { | ||
mattxwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const resolveShorthandValues = () => { | ||
mattxwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const values = prefixedShorthandData; | ||
|
||
/** @type {Map<string, import('postcss').Declaration>} */ | ||
const transformedDeclarationNodes = new Map(); | ||
|
||
declNodes.forEach((declarationNode) => { | ||
transformedDeclarationNodes.set( | ||
declarationNode.prop.toLowerCase(), | ||
declarationNode, | ||
); | ||
}); | ||
|
||
return values | ||
.map((shorthandProp) => { | ||
const val = transformedDeclarationNodes.get(shorthandProp); | ||
|
||
return val !== undefined ? val.value : ''; | ||
}) | ||
.join(' ') | ||
.replace(/\s{2,}/g, ' '); | ||
}; | ||
mattxwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const newShorthandDeclarationNode = declNodes[0].clone({ | ||
prop: prefixedShorthandProperty, | ||
value: resolveShorthandValues(), | ||
}); | ||
|
||
declNodes[0].replaceWith(newShorthandDeclarationNode); | ||
|
||
declNodes.forEach((node) => node.remove()); | ||
|
||
return; | ||
} | ||
} | ||
|
||
report({ | ||
ruleName, | ||
result, | ||
|
This file contains 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.
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.
minor
You could add an
unfixable: true,
test to illustrate that.