forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stylelint] Add invalid properties rule (opensearch-project#4374)
* Add invalid properties rule Signed-off-by: Matt Provost <[email protected]> * Update changelog Signed-off-by: Matt Provost <[email protected]> * Rename old variable Signed-off-by: Matt Provost <[email protected]> * Add types for configs Signed-off-by: Matt Provost <[email protected]> * Rename rule to no_restricted_properties Signed-off-by: Matt Provost <[email protected]> * Refactor duplicate functions into generic one Signed-off-by: Matt Provost <[email protected]> * Add type definitions Signed-off-by: Matt Provost <[email protected]> * Add some documentation about supported config types Signed-off-by: Matt Provost <[email protected]> * Update changelog Signed-off-by: Matt Provost <[email protected]> * Optchain instead of unwrapping source file Signed-off-by: Matt Provost <[email protected]> --------- Signed-off-by: Matt Provost <[email protected]>
- Loading branch information
Showing
10 changed files
with
190 additions
and
15 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
17 changes: 17 additions & 0 deletions
17
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"font-family": { | ||
"approved": [ | ||
"src/plugins/discover/public/application/_discover.scss", | ||
"src/plugins/maps_legacy/public/map/_leaflet_overrides.scss", | ||
"src/plugins/maps_legacy/public/map/_legend.scss", | ||
"src/plugins/opensearch_dashboards_legacy/public/font_awesome/font_awesome.scss", | ||
"src/plugins/opensearch_dashboards_react/public/markdown/_markdown.scss", | ||
"packages/osd-ui-framework/src/components/tool_bar/_tool_bar_search.scss", | ||
"packages/osd-ui-framework/src/global_styling/mixins/_global_mixins.scss", | ||
"src/plugins/data/public/ui/typeahead/_suggestion.scss", | ||
"src/plugins/vis_type_timeseries/public/application/components/_error.scss", | ||
"packages/osd-ui-framework/src/components/form/check_box/_check_box.scss", | ||
"src/plugins/discover/public/application/components/doc_viewer/doc_viewer.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
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
87 changes: 87 additions & 0 deletions
87
packages/osd-stylelint-plugin-stylelint/src/rules/no_restricted_properties/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,87 @@ | ||
/* | ||
* 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_properties'; | ||
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 propertyRule = getRuleFromConfig(rules, decl.prop); | ||
if (!propertyRule) { | ||
return; | ||
} | ||
|
||
let shouldReport = false; | ||
|
||
const file = postcssRoot.source?.input.file; | ||
if (!file) { | ||
return; | ||
} | ||
|
||
const approvedFiles = propertyRule.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(`Usage of property "${decl.prop}" is not allowed.`) | ||
); | ||
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