-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stylelint] Add no_restricted_values linter rule (#4413)
* Add no_restricted_values linter rule Signed-off-by: Matt Provost <[email protected]> * Update changelog Signed-off-by: Matt Provost <[email protected]> * Add explanation for file based configs Signed-off-by: Matt Provost <[email protected]> * Update error messages Signed-off-by: Matt Provost <[email protected]> --------- Signed-off-by: Matt Provost <[email protected]> Signed-off-by: Matt Provost <[email protected]> Co-authored-by: Ashwin P Chandran <[email protected]> Co-authored-by: Anan Zhuang <[email protected]> Co-authored-by: Josh Romero <[email protected]>
- Loading branch information
1 parent
86a3f1e
commit 06a4903
Showing
10 changed files
with
151 additions
and
12 deletions.
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