-
Notifications
You must be signed in to change notification settings - Fork 893
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 (#4374)
* Add invalid properties rule Signed-off-by: Matt Provost <provomat@amazon.com> * Update changelog Signed-off-by: Matt Provost <provomat@amazon.com> * Rename old variable Signed-off-by: Matt Provost <provomat@amazon.com> * Add types for configs Signed-off-by: Matt Provost <provomat@amazon.com> * Rename rule to no_restricted_properties Signed-off-by: Matt Provost <provomat@amazon.com> * Refactor duplicate functions into generic one Signed-off-by: Matt Provost <provomat@amazon.com> * Add type definitions Signed-off-by: Matt Provost <provomat@amazon.com> * Add some documentation about supported config types Signed-off-by: Matt Provost <provomat@amazon.com> * Update changelog Signed-off-by: Matt Provost <provomat@amazon.com> * Optchain instead of unwrapping source file Signed-off-by: Matt Provost <provomat@amazon.com> --------- Signed-off-by: Matt Provost <provomat@amazon.com> (cherry picked from commit d285ecb) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
- Loading branch information
1 parent
addc22d
commit d5d41f5
Showing
9 changed files
with
189 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
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