Skip to content
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
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Upgrade the backport workflow ([#4343](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4343))
- [Lint] Add custom stylelint rules and config to prevent unintended style overrides ([#4290](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4290))
- [Lint] Add stylelint rule to define properties that are restricted from being used ([#4374](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4374))
- [Lint] Add stylelint rule to define values that are restricted from being used ([#4413](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4413))

### 📝 Documentation

Expand Down
22 changes: 15 additions & 7 deletions packages/osd-stylelint-config/.stylelintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,34 @@ module.exports = {
],

rules: {
'@osd/stylelint/no_restricted_properties': [
'@osd/stylelint/no_modifying_global_selectors': [
{
config: "./../../../osd-stylelint-config/config/restricted_properties.json"
config: "./../../../osd-stylelint-config/config/global_selectors.json"
},
{
severity: "error"
}
],
'@osd/stylelint/no_modifying_global_selectors': [
'@osd/stylelint/no_custom_colors': [
{
config: "./../../../osd-stylelint-config/config/global_selectors.json"
config: './../../../osd-stylelint-config/config/colors.json'
},
],
'@osd/stylelint/no_restricted_properties': [
{
config: "./../../../osd-stylelint-config/config/restricted_properties.json"
},
{
severity: "error"
}
],
'@osd/stylelint/no_custom_colors': [
'@osd/stylelint/no_restricted_values': [
{
config: './../../../osd-stylelint-config/config/colors.json'
config: "./../../../osd-stylelint-config/config/restricted_values.json"
},
]
{
severity: "error"
}
],
}
}
23 changes: 23 additions & 0 deletions packages/osd-stylelint-config/config/restricted_values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"/#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?/": {
"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)\\([^)]*\\)/": {
BSFishy marked this conversation as resolved.
Show resolved Hide resolved
"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"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
* GitHub history for details.
*/

import noRestrictedProperties from './no_restricted_properties';
import noCustomColors from './no_custom_colors';
import noModifyingGlobalSelectors from './no_modifying_global_selectors';
import noRestrictedProperties from './no_restricted_properties';
import noRestrictedValues from './no_restricted_values';

// eslint-disable-next-line import/no-default-export
export default {
no_custom_colors: noCustomColors,
no_modifying_global_selectors: noModifyingGlobalSelectors,
no_restricted_properties: noRestrictedProperties,
no_restricted_values: noRestrictedValues,
};
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_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(`Usage of value "${decl.value}" is not allowed.`)
BSFishy marked this conversation as resolved.
Show resolved Hide resolved
);
report(reportInfo);
});
};
};

ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;

// eslint-disable-next-line import/no-default-export
export default ruleFunction;