diff --git a/.eslintignore b/.eslintignore index c516d7511b7..6ea478bc2bf 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,6 +3,7 @@ /.chromium /build /built_assets +/bwc_tmp /config/apm.dev.js /data /html_docs diff --git a/.stylelintrc.yml b/.stylelintrc.yml index 8a70ee1dd00..ad81fc065f7 100644 --- a/.stylelintrc.yml +++ b/.stylelintrc.yml @@ -1,5 +1,6 @@ extends: - stylelint-config-standard-scss + - '@osd/stylelint-config' rules: # while we still use node-sass, only legacy rgb() notation is allowed color-function-notation: "legacy" diff --git a/CHANGELOG.md b/CHANGELOG.md index ad80150085c..c7bea3edfd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Upgrade yarn version to be compatible with @opensearch-project/opensearch ([#3443](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3443)) - Add an achievement badger to the PR ([#3721](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3721)) - 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)) ### 📝 Documentation diff --git a/package.json b/package.json index 30e6756ca10..dbe2e7302ff 100644 --- a/package.json +++ b/package.json @@ -248,6 +248,8 @@ "@osd/optimizer": "1.0.0", "@osd/plugin-generator": "1.0.0", "@osd/pm": "1.0.0", + "@osd/stylelint-config": "1.0.0", + "@osd/stylelint-plugin-stylelint": "1.0.0", "@osd/telemetry-tools": "1.0.0", "@osd/test": "1.0.0", "@osd/test-subj-selector": "0.2.1", diff --git a/packages/osd-stylelint-config/.stylelintrc.js b/packages/osd-stylelint-config/.stylelintrc.js new file mode 100644 index 00000000000..6a4e2099f5a --- /dev/null +++ b/packages/osd-stylelint-config/.stylelintrc.js @@ -0,0 +1,32 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +module.exports = { + plugins: [ + '@osd/stylelint-plugin-stylelint', + ], + + rules: { + '@osd/stylelint/no_modifying_global_selectors': [ + { + config: "./../../../osd-stylelint-config/config/global_selectors.json" + }, + { + severity: "error" + } + ], + '@osd/stylelint/no_custom_colors': [ + { + config: './../../../osd-stylelint-config/config/colors.json' + }, + ] + } +} diff --git a/packages/osd-stylelint-config/README.md b/packages/osd-stylelint-config/README.md new file mode 100644 index 00000000000..68d7354626f --- /dev/null +++ b/packages/osd-stylelint-config/README.md @@ -0,0 +1,45 @@ +# osd-stylelint-config + +The Stylelint config used by OpenSearch Dashboards. This package is created to enable the modification of rules with the JSON files in `config/` and any consumer of the `@osd/stylelint-plugin-stylelint` can just extend this config within its `.stylelintrc`. + +The ideal state being that this package is maintained externally from the OpenSearch Dashboards repo so that the configs can be modified with rules without being blocked by processes within OpenSearch Dashboards. + +Issue: +https://github.com/opensearch-project/oui/issues/845 + +## Usage + +To use this stylelint config, just install the peer dependencies and reference it +in your `.stylelintrc`: + +```javascript +{ + extends: [ + '@osd/stylelint-config' + ] +} +``` + +This JSON configs that are sent through the compliance engine are expected to follow the schema: + +```json +{ + "cssProperty": { + "regexOfSelector": [ + { + "approved": "OUICompliantValue1", + "rejected": [ + "OUIViolationValue1", + "OUIViolationValue2", + ] + }, + { + "approved": "OUICompliantValue2", + "rejected": [ + "OUIViolationValue3" + ] + } + ] + } +} +``` diff --git a/packages/osd-stylelint-config/config/colors.json b/packages/osd-stylelint-config/config/colors.json new file mode 100644 index 00000000000..70728fa1227 --- /dev/null +++ b/packages/osd-stylelint-config/config/colors.json @@ -0,0 +1,18 @@ +{ + "background-color": { + "buttonTODOTHISANEXAMPLE": [ + { + "approved": "$euiColorPrimary", + "rejected": [ + "blueTODOTHISANEXAMPLE" + ] + }, + { + "approved": "$euiColorWarning", + "rejected": [ + "redTODOTHISANEXAMPLE" + ] + } + ] + } +} diff --git a/packages/osd-stylelint-config/config/global_selectors.json b/packages/osd-stylelint-config/config/global_selectors.json new file mode 100644 index 00000000000..19451066878 --- /dev/null +++ b/packages/osd-stylelint-config/config/global_selectors.json @@ -0,0 +1,30 @@ +{ + "#opensearch-dashboards-body": { + "approved": [ + "src/core/public/rendering/_base.scss" + ] + }, + ".app-wrapper": { + "approved": [ + "src/core/public/rendering/_base.scss" + ] + }, + "/\\.[eo]ui/": { + "approved": [ + "examples/expressions_example/public/index.scss", + "src/core/public/styles/_base.scss", + "src/plugins/vis_default_editor/public/_sidebar.scss", + "src/core/public/core_app/styles/_mixins.scss", + "src/core/public/chrome/ui/header/_index.scss", + "src/core/public/chrome/ui/header/header_breadcrumbs.scss", + "src/core/public/chrome/ui/header/home_loader.scss", + "src/plugins/data/public/ui/filter_bar/_global_filter_item.scss", + "src/plugins/home/public/application/components/_synopsis.scss", + "src/plugins/vis_builder/public/application/components/searchable_dropdown.scss", + "src/plugins/vis_builder/public/application/components/side_nav.scss", + "packages/osd-ui-framework/src/components/button/button_group/_button_group.scss", + "src/plugins/discover/public/application/components/sidebar/discover_sidebar.scss", + "src/plugins/discover/public/application/angular/doc_table/components/table_row/_open.scss" + ] + } +} diff --git a/packages/osd-stylelint-config/package.json b/packages/osd-stylelint-config/package.json new file mode 100644 index 00000000000..750a1bf207d --- /dev/null +++ b/packages/osd-stylelint-config/package.json @@ -0,0 +1,15 @@ +{ + "name": "@osd/stylelint-config", + "version": "1.0.0", + "description": "The stylelint config used by OpenSearch Dashboards", + "main": ".stylelintrc.js", + "private": true, + "license": "Apache-2.0", + "opensearchDashboards": { + "devOnly": true + }, + "peerDependencies": { + "@osd/stylelint-plugin-stylelint": "1.0.0", + "stylelint": "^14.5.2" + } +} diff --git a/packages/osd-stylelint-plugin-stylelint/README.md b/packages/osd-stylelint-plugin-stylelint/README.md new file mode 100644 index 00000000000..e6879cf1a9f --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/README.md @@ -0,0 +1,18 @@ +# Custom Stylelint rules for OpenSearch Dashboards + +This package contains custom Stylelint rules used for OpenSearch Dashboards development. Rules are defined in `src/rules` and are the actual logic of the custom rules which are built around parsing a JSON config file passed in and applying the rules defined within there. + +This package can work with `@osd/stylelint-config` which houses the JSON config files to be passed into this plugin. The goal being to seperate out OUI compliance rules which can be modified by anyone versus the actual OUI compliance engine which should be only modified by developers. + +## Audit styles that are related to custom rules + +Setting: +``` +export OUI_AUDIT_ENABLED=true +``` + +Enables the ability to capture styling that potentially can be subject to compliance when there is a rule that interacts with the styling. + +For example, if a style attempts to set a button to have a `background-color: $incorrectVariable()` and the JSON config passed to the compliance engine does not explicitly reject the `$incorrectVariable()` being applied to a button's background color then the linter will pass. But it will output this if the environment variable is set to `true`. + +The goal being that setting this environment variable to output a list that can then be added to the JSON config which we can feed back into the compliance engine. diff --git a/packages/osd-stylelint-plugin-stylelint/package.json b/packages/osd-stylelint-plugin-stylelint/package.json new file mode 100644 index 00000000000..2d2a6e661d5 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/package.json @@ -0,0 +1,22 @@ +{ + "name": "@osd/stylelint-plugin-stylelint", + "main": "./target/index.js", + "types": "./target/index.d.ts", + "version": "1.0.0", + "private": true, + "license": "Apache-2.0", + "scripts": { + "build": "tsc", + "osd:bootstrap": "yarn build" + }, + "opensearchDashboards": { + "devOnly": true + }, + "peerDependencies": { + "stylelint": "^14.5.2" + }, + "devDependencies": { + "typescript": "4.0.2", + "tsd": "^0.21.0" + } +} diff --git a/packages/osd-stylelint-plugin-stylelint/src/index.ts b/packages/osd-stylelint-plugin-stylelint/src/index.ts new file mode 100644 index 00000000000..db919d008f9 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/index.ts @@ -0,0 +1,22 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import stylelint from 'stylelint'; +import rules from './rules'; + +export const NAMESPACE = '@osd/stylelint'; + +const rulesPlugins = Object.keys(rules).map((ruleName: string) => { + return stylelint.createPlugin(`${NAMESPACE}/${ruleName}`, rules[ruleName]); +}); + +// eslint-disable-next-line import/no-default-export +export default rulesPlugins; diff --git a/packages/osd-stylelint-plugin-stylelint/src/rules/index.ts b/packages/osd-stylelint-plugin-stylelint/src/rules/index.ts new file mode 100644 index 00000000000..0f09ff62b23 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/rules/index.ts @@ -0,0 +1,19 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import noCustomColors from './no_custom_colors'; +import noModifyingGlobalSelectors from './no_modifying_global_selectors'; + +// eslint-disable-next-line import/no-default-export +export default { + no_custom_colors: noCustomColors, + no_modifying_global_selectors: noModifyingGlobalSelectors, +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/rules/no_custom_colors/index.ts b/packages/osd-stylelint-plugin-stylelint/src/rules/no_custom_colors/index.ts new file mode 100644 index 00000000000..b8061f7ff9b --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/rules/no_custom_colors/index.ts @@ -0,0 +1,106 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import stylelint from 'stylelint'; +import { NAMESPACE } from '../..'; +import { + ComplianceEngine, + getTrackedMessage, + getUntrackedMessage, + getNotCompliantMessage, + getRulesFromConfig, + isColorProperty, + isValidOptions, +} from '../../utils'; + +const isOuiAuditEnabled = Boolean(process.env.OUI_AUDIT_ENABLED); + +const { ruleMessages, report } = stylelint.utils; +const engine = ComplianceEngine.default; + +const ruleName = 'no_custom_colors'; +const messages = ruleMessages(ruleName, { + expected: (message) => `${message}`, +}); + +const ruleFunction = ( + primaryOption: Record, + secondaryOptionObject: Record, + context +) => { + return (postcssRoot: any, postcssResult: any) => { + const validOptions = isValidOptions(postcssResult, ruleName, primaryOption); + if (!validOptions) { + return; + } + + const rules = getRulesFromConfig(primaryOption.config); + + const isAutoFixing = Boolean(context.fix); + + postcssRoot.walkDecls((decl: any) => { + if (!isColorProperty(decl.prop)) { + return; + } + + let shouldReport = false; + + const nodeInfo = { + selector: decl.parent.selector, + prop: decl.prop, + value: decl.value, + }; + + const reportInfo = { + ruleName: `${NAMESPACE}/${ruleName}`, + result: postcssResult, + node: decl, + message: '', + }; + + if (isOuiAuditEnabled && !engine.isTracked(rules, nodeInfo)) { + reportInfo.message = messages.expected(getUntrackedMessage(nodeInfo)); + report(reportInfo); + return; + } + + const ruleObject = engine.getComplianceRule(rules, nodeInfo); + + if (!ruleObject) { + if (isOuiAuditEnabled) { + reportInfo.message = messages.expected(getTrackedMessage(nodeInfo)); + report(reportInfo); + } + return; + } + + shouldReport = !ruleObject.isComplaint; + + if (shouldReport && isAutoFixing) { + decl.value = ruleObject.expected; + return; + } + + if (!shouldReport) { + return; + } + + reportInfo.message = messages.expected(getNotCompliantMessage(ruleObject.message)); + report(reportInfo); + }); + }; +}; + +ruleFunction.ruleName = ruleName; +ruleFunction.messages = messages; + +// eslint-disable-next-line import/no-default-export +export default ruleFunction; diff --git a/packages/osd-stylelint-plugin-stylelint/src/rules/no_modifying_global_selectors/index.ts b/packages/osd-stylelint-plugin-stylelint/src/rules/no_modifying_global_selectors/index.ts new file mode 100644 index 00000000000..2c6ee035aec --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/rules/no_modifying_global_selectors/index.ts @@ -0,0 +1,88 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import stylelint from 'stylelint'; +import { NAMESPACE } from '../..'; +import { + getNotCompliantMessage, + getRulesFromConfig, + isValidOptions, + getSelectorRule, +} from '../../utils'; + +const { ruleMessages, report } = stylelint.utils; + +const ruleName = 'no_modifying_global_selectors'; +const messages = ruleMessages(ruleName, { + expected: (message) => `${message}`, +}); + +const ruleFunction = ( + primaryOption: Record, + secondaryOptionObject: Record, + context +) => { + return (postcssRoot: any, postcssResult: any) => { + const validOptions = isValidOptions(postcssResult, ruleName, primaryOption); + if (!validOptions) { + return; + } + + const rules = getRulesFromConfig(primaryOption.config); + + const isAutoFixing = Boolean(context.fix); + + postcssRoot.walkRules((rule: any) => { + const selectorRule = getSelectorRule(rules, rule); + if (!selectorRule) { + return; + } + + let shouldReport = false; + + const file = postcssRoot.source.input.file; + const approvedFiles = selectorRule.approved; + + const reportInfo = { + ruleName: `${NAMESPACE}/${ruleName}`, + result: postcssResult, + node: rule, + message: '', + }; + + if (approvedFiles) { + shouldReport = !approvedFiles.some((inspectedFile) => { + return file.includes(inspectedFile); + }); + } + + if (shouldReport && isAutoFixing) { + rule.remove(); + return; + } + + if (!shouldReport) { + return; + } + + reportInfo.message = messages.expected( + getNotCompliantMessage(`Modifying global selector "${rule.selector}" not allowed.`) + ); + report(reportInfo); + }); + }; +}; + +ruleFunction.ruleName = ruleName; +ruleFunction.messages = messages; + +// eslint-disable-next-line import/no-default-export +export default ruleFunction; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/compliance_engine.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/compliance_engine.ts new file mode 100644 index 00000000000..dcdbcf0cc16 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/compliance_engine.ts @@ -0,0 +1,77 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +export interface ComplianceRule { + isComplaint: boolean; + actual: string; + expected: string; + message: string; +} + +const getRule = (rules: JSON, nodeInfo: { selector: string; prop: string }) => { + const rule = rules[nodeInfo.prop]; + if (!rule) { + return undefined; + } + + if (!nodeInfo.selector) { + return undefined; + } + + const ruleKey = Object.keys(rule).find((key) => { + const regex = new RegExp(key, 'gi'); + return nodeInfo.selector.match(regex); + }); + + if (!ruleKey) { + return undefined; + } + + return rule[ruleKey]; +}; + +const isTracked = (rules: JSON, nodeInfo: { selector: string; prop: string }) => { + return getRule(rules, nodeInfo) !== undefined; +}; + +const getComplianceRule = ( + rules: JSON, + nodeInfo: { selector: string; prop: string; value: string } +): ComplianceRule | undefined => { + const rule = getRule(rules, nodeInfo); + + if (!rule) { + return undefined; + } + + const ruleObject = rule.find((object) => { + if (object.approved.includes(nodeInfo.value) || object.rejected.includes(nodeInfo.value)) { + return object; + } + }); + + if (!ruleObject) { + return undefined; + } + + return { + isComplaint: !ruleObject.rejected.includes(nodeInfo.value), + actual: nodeInfo.value, + expected: ruleObject.approved, + message: `${nodeInfo.selector} expected: ${ruleObject.approved} - actual: ${nodeInfo.value}`, + }; +}; + +// eslint-disable-next-line import/no-default-export +export default { + isTracked, + getComplianceRule, +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/extract_regex.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/extract_regex.ts new file mode 100644 index 00000000000..937b486ad94 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/extract_regex.ts @@ -0,0 +1,22 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export const extractRegex = (value: string) => { + if (!value.startsWith('/')) { + return undefined; + } + + const split = value.split('/'); + split.shift(); + + const flags = split.pop(); + if (split.length === 0) { + return undefined; + } + + const pattern = split.join('/'); + + return new RegExp(pattern, flags); +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/get_message.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/get_message.ts new file mode 100644 index 00000000000..65e236aaffa --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/get_message.ts @@ -0,0 +1,18 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +export const getUntrackedMessage = (nodeInfo: { selector: string; prop: string; value: string }) => + `Untracked: "${nodeInfo.selector}.${nodeInfo.prop}: ${nodeInfo.value}"`; + +export const getTrackedMessage = (nodeInfo: { selector: string; prop: string; value: string }) => + `Tracked but missing approval: "${nodeInfo.selector}.${nodeInfo.prop}: ${nodeInfo.value}"`; + +export const getNotCompliantMessage = (message: string) => `${message}`; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/get_rules_from_config.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/get_rules_from_config.ts new file mode 100644 index 00000000000..dc13230fa6b --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/get_rules_from_config.ts @@ -0,0 +1,29 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import path from 'path'; +import { readFileSync } from 'fs'; +import { matches } from './matches'; + +export const getRulesFromConfig = (configPath: string) => { + const filePath = path.resolve(__dirname, configPath); + return JSON.parse(readFileSync(filePath, 'utf-8')); +}; + +export const getSelectorRule = (rules: any, rule: any) => { + for (const configRule of Object.keys(rules)) { + if (matches(configRule, rule.selector)) { + return rules[configRule]; + } + } + + return undefined; +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/index.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/index.ts new file mode 100644 index 00000000000..ef60132e86b --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/index.ts @@ -0,0 +1,18 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +export * as ComplianceEngine from './compliance_engine'; +export * from './extract_regex'; +export * from './get_message'; +export * from './get_rules_from_config'; +export * from './is_color_property'; +export * from './is_valid_options'; +export * from './matches'; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/is_color_property.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/is_color_property.ts new file mode 100644 index 00000000000..d52cb43cfc3 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/is_color_property.ts @@ -0,0 +1,60 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +const COLOR_PROPERTIES = [ + 'all', + 'animation', + 'animation-name', + 'background', + 'background-color', + 'background-image', + 'border', + 'border-bottom', + 'border-bottom-color', + 'border-color', + 'border-image', + 'border-image-source', + 'border-left', + 'border-left-color', + 'border-right', + 'border-right-color', + 'border-top', + 'border-top-color', + 'box-shadow', + 'caret-color', + 'color', + 'column-rule', + 'column-rule-color', + 'content', + 'cursor', + 'filter', + 'list-style', + 'list-style-image', + 'outline', + 'outline-color', + 'text-decoration', + 'text-decoration-color', + 'text-shadow', + 'mask-image', + 'shape-outside', + 'mask-border-source', +]; + +/** + * This is intended to check a list of defined properties + * within a style and see if it's potentially modifying a property + * that can have a color. Stylelint crawls styles and will check + * each one, therefore this is to optimize the linter to + * skip any property that does not impact colors. + */ +export const isColorProperty = (prop: string) => { + return COLOR_PROPERTIES.includes(prop); +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/is_valid_options.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/is_valid_options.ts new file mode 100644 index 00000000000..15fae8862ea --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/is_valid_options.ts @@ -0,0 +1,27 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import stylelint from 'stylelint'; + +const { validateOptions } = stylelint.utils; + +export const isValidOptions = ( + postcssResult: any, + ruleName: string, + primaryOption: Record +) => { + return validateOptions(postcssResult, ruleName, { + actual: primaryOption, + possible: { + config: [(input) => typeof input === 'string'], + }, + }); +}; diff --git a/packages/osd-stylelint-plugin-stylelint/src/utils/matches.ts b/packages/osd-stylelint-plugin-stylelint/src/utils/matches.ts new file mode 100644 index 00000000000..02cc327615b --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/src/utils/matches.ts @@ -0,0 +1,15 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { extractRegex } from './extract_regex'; + +export const matches = (matcher: string, value: string) => { + const regex = extractRegex(matcher); + if (!regex) { + return value === matcher; + } + + return regex.test(value); +}; diff --git a/packages/osd-stylelint-plugin-stylelint/tsconfig.json b/packages/osd-stylelint-plugin-stylelint/tsconfig.json new file mode 100644 index 00000000000..d7e31cada44 --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "./target", + "outDir": "./target", + "stripInternal": true, + "declarationMap": true, + "types": ["jest", "node"], + "noImplicitAny": false, + }, + "include": ["./src/**/*.ts"], + "exclude": ["target"] +} diff --git a/packages/osd-stylelint-plugin-stylelint/yarn.lock b/packages/osd-stylelint-plugin-stylelint/yarn.lock new file mode 120000 index 00000000000..3f82ebc9cdb --- /dev/null +++ b/packages/osd-stylelint-plugin-stylelint/yarn.lock @@ -0,0 +1 @@ +../../yarn.lock \ No newline at end of file