@sumup-oss/eslint-plugin-circuit-ui
Version:
ESLint rules to lint Circuit UI.
64 lines (63 loc) • 2.68 kB
JavaScript
/**
* Copyright 2023, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
const createRule = ESLintUtils.RuleCreator((name) => `https://github.com/sumup-oss/circuit-ui/tree/main/packages/eslint-plugin-circuit-ui/${name}`);
const mappings = [
{
components: ['Body'],
props: ['variant'],
alternative: 'Use the new `color` prop instead of the `alert`, `confirm` and `subtle` variants. Use the new `weight` prop instead of the `highlight` variant. Use custom CSS for the `quote` variant.',
},
];
export const noDeprecatedProps = createRule({
name: 'no-deprecated-props',
meta: {
type: 'suggestion',
schema: [],
docs: {
description: 'Deprecated component props should be removed or replaced',
recommended: 'warn',
},
messages: {
deprecated: "The {{component}}'s `{{prop}}` prop has been deprecated. {{alternative}}",
},
},
defaultOptions: [],
create(context) {
return mappings.reduce((visitors, config) => {
config.components.forEach((component) => {
visitors[`JSXElement[openingElement.name.name="${component}"]`] = (node) => {
const { props, alternative } = config;
node.openingElement.attributes.forEach((attribute) => {
if (attribute.type !== TSESTree.AST_NODE_TYPES.JSXAttribute ||
attribute.name.type !== TSESTree.AST_NODE_TYPES.JSXIdentifier) {
return;
}
const prop = attribute.name.name;
if (!props.includes(prop)) {
return;
}
context.report({
node: attribute,
messageId: 'deprecated',
data: { component, prop, alternative },
});
});
};
});
return visitors;
}, {});
},
});