Skip to content

Commit

Permalink
Add regex matching and OUI modification lint
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Provost <[email protected]>
  • Loading branch information
BSFishy committed Jun 20, 2023
1 parent 2bbea93 commit cad253a
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
20 changes: 19 additions & 1 deletion packages/osd-stylelint-config/config/global_selectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,23 @@
"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"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

import stylelint from 'stylelint';
import { NAMESPACE } from '../..';
import { getNotCompliantMessage, getRulesFromConfig, isValidOptions } from '../../utils';
import {
getNotCompliantMessage,
getRulesFromConfig,
isValidOptions,
getSelectorRule,
} from '../../utils';

const { ruleMessages, report } = stylelint.utils;

Expand All @@ -36,7 +41,7 @@ const ruleFunction = (
const isAutoFixing = Boolean(context.fix);

postcssRoot.walkRules((rule: any) => {
const selectorRule = rules[rule.selector];
const selectorRule = getSelectorRule(rules, rule);
if (!selectorRule) {
return;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/osd-stylelint-plugin-stylelint/src/utils/extract_regex.ts
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@

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;
};
2 changes: 2 additions & 0 deletions packages/osd-stylelint-plugin-stylelint/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/

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';
15 changes: 15 additions & 0 deletions packages/osd-stylelint-plugin-stylelint/src/utils/matches.ts
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit cad253a

Please sign in to comment.