This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Codify the Suggestion-on-partial-fix pattern #7543
Merged
Merged
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
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,5 @@ | ||
--- | ||
'@shopify/polaris-migrator': minor | ||
--- | ||
|
||
Expose utilities for SASS Migrations to leverage the Suggestion-on-partial-fix pattern |
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
import type {FileInfo, API, Options} from 'jscodeshift'; | ||
import postcss, {Root, Result, Plugin, Node as PostCSSNode} from 'postcss'; | ||
import postcss, { | ||
Root, | ||
Result, | ||
Plugin, | ||
Container, | ||
Declaration, | ||
Node as PostCSSNode, | ||
Rule as PostCSSRule, | ||
Comment as PostCSSComment, | ||
AtRule, | ||
} from 'postcss'; | ||
import valueParser, { | ||
Node, | ||
ParsedValue, | ||
|
@@ -259,7 +269,7 @@ interface PluginOptions extends Options, NamespaceOptions {} | |
|
||
interface Report { | ||
node: PostCSSNode; | ||
severity: 'warning' | 'error' | 'suggestion'; | ||
severity: 'warning' | 'error'; | ||
message: string; | ||
} | ||
|
||
|
@@ -286,14 +296,32 @@ type StylelintRule<P = any, S = any> = StylelintRuleBase<P, S> & { | |
}; | ||
// End: Extracted from stylelint | ||
|
||
type Walker<N extends PostCSSNode> = (node: N) => false | void; | ||
|
||
export type PolarisMigrator = ( | ||
primaryOption: true, | ||
secondaryOptions: { | ||
options: {[key: string]: any}; | ||
methods: { | ||
report: (report: Report) => void; | ||
flushReports: () => void; | ||
getReportsForNode: (node: PostCSSNode) => Report[] | undefined; | ||
each: <T extends Container>(root: T, walker: Walker<PostCSSNode>) => void; | ||
walk: <T extends Container>(root: T, walker: Walker<PostCSSNode>) => void; | ||
walkComments: <T extends Container>( | ||
root: T, | ||
walker: Walker<PostCSSComment>, | ||
) => void; | ||
walkAtRules: <T extends Container>( | ||
root: T, | ||
atRuleWalker: Walker<AtRule>, | ||
) => void; | ||
walkDecls: <T extends Container>( | ||
root: T, | ||
declWalker: Walker<Declaration>, | ||
) => void; | ||
walkRules: <T extends Container>( | ||
root: T, | ||
ruleWalker: Walker<PostCSSRule>, | ||
) => void; | ||
}; | ||
}, | ||
context: PluginContext, | ||
|
@@ -376,18 +404,49 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | |
|
||
for (const report of reportsForNode) { | ||
node.before( | ||
report.severity === 'suggestion' | ||
? createInlineComment(report.message) | ||
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. Suggestions are now inserted as part of the walker function, so this code can remove the conditional. |
||
: createInlineComment(`${report.severity}: ${report.message}`, { | ||
prose: true, | ||
}), | ||
createInlineComment(`${report.severity}: ${report.message}`, { | ||
prose: true, | ||
}), | ||
); | ||
} | ||
} | ||
reports.clear(); | ||
}; | ||
|
||
const getReportsForNode = (node: PostCSSNode) => reports.get(node); | ||
function createWalker<T extends PostCSSNode>(args: { | ||
walker: (node: T) => false | void; | ||
serialiseSuggestion: (node: T) => string; | ||
}): (node: T) => false | void { | ||
const {walker, serialiseSuggestion} = args; | ||
|
||
return (node: T) => { | ||
let oldNode: T; | ||
if (context.fix) { | ||
oldNode = node.clone(); | ||
} | ||
|
||
const result = walker(node); | ||
|
||
const isPartialFix = | ||
context.fix && | ||
reports.has(node) && | ||
node.toString() !== oldNode!.toString(); | ||
|
||
flushReportsAsComments(); | ||
|
||
// Our migrations have an opinion on partial fixes (when multiple | ||
// issues are found in a single node, and some but not all can be | ||
// fixed): We dump out the partial fix result as a comment | ||
// immediately above the node. | ||
if (isPartialFix) { | ||
node.before(createInlineComment(serialiseSuggestion(node))); | ||
// Undo changes | ||
node.replaceWith(oldNode!); | ||
} | ||
|
||
return result; | ||
}; | ||
} | ||
|
||
return ruleFn( | ||
primary, | ||
|
@@ -399,8 +458,54 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | |
options: secondaryOptions, | ||
methods: { | ||
report: addDedupedReport, | ||
flushReports: flushReportsAsComments, | ||
getReportsForNode, | ||
each(root, walker) { | ||
root.each( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => node.toString(), | ||
}), | ||
); | ||
}, | ||
walk(root, walker) { | ||
root.walk( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => node.toString(), | ||
}), | ||
); | ||
}, | ||
walkAtRules(root, walker) { | ||
root.walkAtRules( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => `@${node.name} ${node.params}`, | ||
}), | ||
); | ||
}, | ||
walkComments(root, walker) { | ||
root.walkComments( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => node.text, | ||
}), | ||
); | ||
}, | ||
walkDecls(root, walker) { | ||
root.walkDecls( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => `${node.prop}: ${node.value}`, | ||
}), | ||
); | ||
}, | ||
walkRules(root, walker) { | ||
root.walkRules( | ||
createWalker({ | ||
walker, | ||
serialiseSuggestion: (node) => node.selector, | ||
}), | ||
); | ||
}, | ||
}, | ||
}, | ||
context, | ||
|
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.
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.
This logic is now conveniently hidden away from the migration code, making it cleaner and easier to follow along.