-
Notifications
You must be signed in to change notification settings - Fork 817
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 invalid properties rule #4374
Merged
manasvinibs
merged 11 commits into
opensearch-project:main
from
BSFishy:stylelint/invalid_properties
Jun 27, 2023
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
124ad4b
Add invalid properties rule
BSFishy cad3fb5
Update changelog
BSFishy 8167606
Rename old variable
BSFishy 5809957
Add types for configs
BSFishy 7e2915a
Rename rule to no_restricted_properties
BSFishy 264cd7f
Refactor duplicate functions into generic one
BSFishy 0c04fd8
Add type definitions
BSFishy 4eec399
Add some documentation about supported config types
BSFishy 32571ee
Update changelog
BSFishy 3551db1
Optchain instead of unwrapping source file
BSFishy cbe92e4
Merge branch 'main' into stylelint/invalid_properties
BSFishy 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
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
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
/* | ||
* 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 = ( | ||
primaryOption: Record<string, any>, | ||
secondaryOptionObject: Record<string, any>, | ||
context | ||
) => { | ||
return (postcssRoot: any, postcssResult: any) => { | ||
const validOptions = isValidOptions(postcssResult, ruleName, primaryOption); | ||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
const rules: FileBasedConfig = getRulesFromConfig(primaryOption.config); | ||
|
||
const isAutoFixing = Boolean(context.fix); | ||
|
||
postcssRoot.walkDecls((decl: any) => { | ||
const propertyRule = getRuleFromConfig(rules, decl.prop); | ||
if (!propertyRule) { | ||
return; | ||
} | ||
|
||
let shouldReport = false; | ||
|
||
const file = postcssRoot.source.input.file; | ||
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
Oops, something went wrong.
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.
Can we rephrase this to be clearer? It seems like the change is that we can now declare CSS properties that can only be specified by allowlisted files. Something along those lines would be better for someone scanning the release notes without context.