-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add stylelint metadata within shim #7541
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
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 | ||
| --- | ||
|
|
||
| Internally setup stylelint metadata for SASS migrations in preparation for switching to stylelint as our migration runner. |
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 |
|---|---|---|
|
|
@@ -258,16 +258,38 @@ interface PluginOptions extends Options, NamespaceOptions {} | |
| interface PluginContext { | ||
| fix: boolean; | ||
| } | ||
|
|
||
| // Extracted from stylelint | ||
| type StylelintRuleBase<P = any, S = any> = ( | ||
| primaryOption: P, | ||
| secondaryOptions: {[key: string]: S}, | ||
| context: PluginContext, | ||
| ) => (root: Root, result: Result) => void; | ||
|
|
||
| interface StylelintRuleMeta { | ||
| url: string; | ||
| deprecated?: boolean; | ||
| fixable?: boolean; | ||
| } | ||
|
|
||
| type StylelintRule<P = any, S = any> = StylelintRuleBase<P, S> & { | ||
| ruleName: string; | ||
| meta?: StylelintRuleMeta; | ||
| }; | ||
| // End: Extracted from stylelint | ||
|
|
||
| export type PolarisMigrator = ( | ||
| primaryOption: true, | ||
| secondaryOptions: PluginOptions, | ||
| context: PluginContext, | ||
| ) => (root: Root, result: Result) => void; | ||
|
|
||
| export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | ||
| // Expose a stylelint-like API for creating sass migrators so we can easily | ||
| // migrate to that tool in the future. | ||
| function convertStylelintRuleToPostcssProcessor(ruleFn: StylelintRule) { | ||
| return (fileInfo: FileInfo, _: API, options: Options) => { | ||
| const plugin: Plugin = { | ||
| postcssPlugin: name, | ||
| postcssPlugin: ruleFn.ruleName, | ||
| // PostCSS will rewalk the AST every time a declaration/rule/etc is | ||
| // mutated by a plugin. This can be useful in some cases, but in ours we | ||
| // only want single-pass behaviour. | ||
|
|
@@ -278,12 +300,14 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | |
| // subsequent passes. | ||
| // 2) Using postcss's Once() plugin callback. | ||
| // | ||
| // We're going with the Once() callback as it's idomatic PostCSS. | ||
| // stylelint also uses `Once()`, so we're able to remove this once we've | ||
| // migrated: | ||
| // https://github.com/stylelint/stylelint/blob/cb425cb/lib/postcssPlugin.js#L22 | ||
| Once(root, {result}) { | ||
| // NOTE: For fullest compatibility with stylelint, we initialise the | ||
| // rule here _inside_ the postcss Once function so multiple passes can | ||
| // be performed without rules accidentally retaining scoped variables, | ||
| // etc. | ||
| // rule here _inside_ the postcss Once function just like stylelint | ||
| // does. This means multiple passes can be performed without rules | ||
| // accidentally retaining scoped variables, etc. | ||
| ruleFn( | ||
| // Normally, this comes from stylelint config, but for this shim we | ||
| // just hard-code it, and instead rely on the "seconary" options | ||
|
|
@@ -301,3 +325,16 @@ export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | |
| }).css; | ||
| }; | ||
| } | ||
|
|
||
| export function createSassMigrator(name: string, ruleFn: PolarisMigrator) { | ||
| const wrappedRule = ruleFn as StylelintRule; | ||
|
|
||
| wrappedRule.ruleName = name; | ||
| wrappedRule.meta = { | ||
| // TODO: link directly to the specific rule | ||
| url: 'https://www.npmjs.com/package/@shopify/stylelint-polaris', | ||
| fixable: true, | ||
| }; | ||
|
|
||
| return convertStylelintRuleToPostcssProcessor(wrappedRule); | ||
|
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. By splitting the shim out into its own function, the entire - return convertStylelintRuleToPostcssProcessor(wrappedRule);
+ return wrappedRule;Without any changes to any of the migrations 🎉 |
||
| } | ||
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 gives us a logical point to inject further functionality (see the next PR where we also shim stylelint's
report()method)