-
Notifications
You must be signed in to change notification settings - Fork 953
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
[Stylelint] Add no_restricted_values linter rule #4413
Merged
joshuarrrr
merged 12 commits into
opensearch-project:main
from
BSFishy:stylelint/no_restricted_values
Aug 17, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f575699
Add no_restricted_values linter rule
BSFishy 1da3f9f
Update changelog
BSFishy b308da3
Merge branch 'main' into stylelint/no_restricted_values
ashwin-pc 30cd3c5
Merge branch 'main' into stylelint/no_restricted_values
BSFishy 90a2610
Merge branch 'main' into stylelint/no_restricted_values
ananzh 5914d2c
Add explanation for file based configs
BSFishy deb14f1
Merge branch 'stylelint/no_restricted_values' of github.com:BSFishy/O…
BSFishy c4ec0d6
Merge branch 'main' into stylelint/no_restricted_values
BSFishy b5956e8
Update error messages
BSFishy d9f8836
Merge branch 'stylelint/no_restricted_values' of github.com:BSFishy/O…
BSFishy f1dede4
Merge branch 'main' into stylelint/no_restricted_values
BSFishy c6460e5
Merge branch 'main' into stylelint/no_restricted_values
joshuarrrr 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 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
1 change: 1 addition & 0 deletions
1
packages/osd-stylelint-config/config/restricted_properties.json
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
25 changes: 25 additions & 0 deletions
25
packages/osd-stylelint-config/config/restricted_values.json
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,25 @@ | ||
{ | ||
"/#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?/": { | ||
"explanation": "All colors should be inherited from the OUI theme, not hard-coded. Either remove the custom color rule or use OUI SASS variables instead.", | ||
"approved": [ | ||
"src/core/public/_variables.scss", | ||
"src/core/public/styles/_ace_overrides.scss", | ||
"packages/osd-ui-framework/src/components/tool_bar/_tool_bar_search.scss", | ||
"packages/osd-ui-framework/src/components/view/_index.scss", | ||
"src/core/public/chrome/ui/header/header_breadcrumbs.scss", | ||
"src/plugins/vis_type_timeseries/public/application/components/vis_types/_vis_types.scss", | ||
"src/plugins/vis_type_vislib/public/vislib/visualizations/point_series/_labels.scss" | ||
] | ||
}, | ||
"/(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\\([^)]*\\)/": { | ||
"explanation": "All colors should be inherited from the OUI theme, not hard-coded. Either remove the custom color rule or use OUI SASS variables instead.", | ||
"approved": [ | ||
"src/core/public/styles/_ace_overrides.scss", | ||
"src/plugins/opensearch_dashboards_react/public/markdown/_markdown.scss", | ||
"packages/osd-ui-framework/src/components/info_panel/_info_panel.scss", | ||
"packages/osd-ui-framework/src/components/local_nav/_local_menu.scss", | ||
"packages/osd-ui-framework/src/global_styling/mixins/_shadow.scss", | ||
"packages/osd-ui-framework/src/global_styling/variables/_colors.scss" | ||
] | ||
} | ||
} |
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
90 changes: 90 additions & 0 deletions
90
packages/osd-stylelint-plugin-stylelint/src/rules/no_restricted_values/index.ts
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,90 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import stylelint from 'stylelint'; | ||
import { NAMESPACE } from '../..'; | ||
import { | ||
getNotCompliantMessage, | ||
getRuleFromConfig, | ||
getRulesFromConfig, | ||
isValidOptions, | ||
FileBasedConfig, | ||
} from '../../utils'; | ||
|
||
const { ruleMessages, report } = stylelint.utils; | ||
|
||
const ruleName = 'no_restricted_values'; | ||
const messages = ruleMessages(ruleName, { | ||
expected: (message) => `${message}`, | ||
}); | ||
|
||
const ruleFunction: stylelint.Rule = ( | ||
primaryOption: Record<string, any>, | ||
secondaryOptionObject: Record<string, any>, | ||
context | ||
) => { | ||
return (postcssRoot, postcssResult) => { | ||
const validOptions = isValidOptions(postcssResult, ruleName, primaryOption); | ||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
const rules: FileBasedConfig = getRulesFromConfig(primaryOption.config); | ||
|
||
const isAutoFixing = Boolean(context.fix); | ||
|
||
postcssRoot.walkDecls((decl) => { | ||
const valueRule = getRuleFromConfig(rules, decl.value); | ||
if (!valueRule) { | ||
return; | ||
} | ||
|
||
let shouldReport = false; | ||
|
||
const file = postcssRoot.source?.input.file; | ||
if (!file) { | ||
return; | ||
} | ||
|
||
const approvedFiles = valueRule.approved; | ||
|
||
const reportInfo = { | ||
ruleName: `${NAMESPACE}/${ruleName}`, | ||
result: postcssResult, | ||
node: decl, | ||
message: '', | ||
}; | ||
|
||
if (approvedFiles) { | ||
shouldReport = !approvedFiles.some((inspectedFile) => { | ||
return file.includes(inspectedFile); | ||
}); | ||
} | ||
|
||
if (shouldReport && isAutoFixing) { | ||
decl.remove(); | ||
return; | ||
} | ||
|
||
if (!shouldReport) { | ||
return; | ||
} | ||
|
||
reportInfo.message = messages.expected( | ||
getNotCompliantMessage( | ||
`Using the value "${decl.value}" is not allowed.`, | ||
valueRule.explanation | ||
) | ||
); | ||
report(reportInfo); | ||
}); | ||
}; | ||
}; | ||
|
||
ruleFunction.ruleName = ruleName; | ||
ruleFunction.messages = messages; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default ruleFunction; |
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
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 implementation assumes
message
ends with some natural separator, i.e. with a period. Is that good enough, or should it use some more complex logic?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.
Yeah, let's take this opportunity to standardize the formatting of these messages. We currently have 2 pieces:
Here's what we'd output based on the the current PR and rule definitions:
Suggested:
The difference is mostly to put them all in the same verb tense and make the explanations actionable and understandable to a dev who may not even know such restrictions exist. I'm fine with the sentence formats for now - it's straightforward enough.