Skip to content

Commit

Permalink
Merge pull request #2691 from exadel-inc/chore/unite-eslint-rules
Browse files Browse the repository at this point in the history
chore(lint): unite eslint rules
  • Loading branch information
ala-n authored Oct 8, 2024
2 parents b3bb84b + c9d9fe4 commit 08e4ba6
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 162 deletions.
25 changes: 7 additions & 18 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,13 @@ However, you still have the option to manually manage the rules if needed.

The ESLint plugin provides a separate rule for each deprecated utility within the ESL project, that's considered to be deprecated. Below is the list of them:

- `@exadel/esl/deprecated-4-alert-action-params` - Rule for deprecated `AlertActionParams` alias for `ESLAlertActionParams`.
- `@exadel/esl/deprecated-4-generate-uid` - Rule for deprecated `generateUId` alias for `randUID`.
- `@exadel/esl/deprecated-4-deep-compare` - Rule for deprecated `deepCompare` alias for `isEqual`.
- `@exadel/esl/deprecated-4-event-utils` - Rule for deprecated `EventUtils` alias for `ESLEventUtils`.
- `@exadel/esl/deprecated-4-panel-action-params` - Rule for deprecated `PanelActionParams` alias for `ESLPanelActionParams`.
- `@exadel/esl/deprecated-4-popup-action-params` - Rule for deprecated `PopupActionParams` alias for `ESLPopupActionParams`.
- `@exadel/esl/deprecated-4-traversing-query` - Rule for deprecated `TraversingQuery` alias for `ESLTraversingQuery`.
- `@exadel/esl/deprecated-4-toggleable-action-params` - Rule for deprecated `ToggleableActionParams` alias for `ESLToggleableActionParams`.
- `@exadel/esl/deprecated-4-tooltip-action-params` - Rule for deprecated `TooltipActionParams` alias for `ESLTooltipActionParams`.

- `@exadel/esl/deprecated-4-media-rule-list-parse` - Rule for deprecated `ESLMediaRuleList.parse` alias for `ESLMediaRuleList.parseQuery` or `ESLMediaRuleList.parseTuple`.

- `@exadel/esl/deprecated-4-base-decorators-path` - Rule for deprecated `@attr`, `@prop`, `@boolAttr`, `@jsonAttr`, `@listen` import paths.

- `@exadel/esl/deprecated-5-alert-action-params` - Rule for deprecated `AlertActionParams` alias for `ESLAlertActionParams`.
- `@exadel/esl/deprecated-5-panel-action-params` - Rule for deprecated `PanelActionParams` alias for `ESLPanelActionParams`.
- `@exadel/esl/deprecated-5-popup-action-params` - Rule for deprecated `PopupActionParams` alias for `ESLPopupActionParams`.
- `@exadel/esl/deprecated-5-tooltip-action-params` - Rule for deprecated `TooltipActionParams` alias for `ESLTooltipActionParams`.
- `@exadel/esl/deprecated-4-aliases` - Rule for deprecated aliases

- `@exadel/esl/deprecated-4-methods` - Rule for deprecated methods.

- `@exadel/esl/deprecated-4-import` - Rule for deprecated import paths.

- `@exadel/esl/deprecated-5-aliases` - Rule for deprecated aliases

These rules can be configured manually inside the `rules` section of your ESLint configuration file.

Expand Down
22 changes: 14 additions & 8 deletions eslint-plugin/src/core/deprecated-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@ export interface ESLintDeprecationCfg {
type ImportNode = ESTree.ImportSpecifier & Rule.NodeParentExtension;

/** Builds deprecation rule from {@link ESLintDeprecationCfg} object */
export function buildRule({deprecation, alias}: ESLintDeprecationCfg): Rule.RuleModule {
export function buildRule(configs: ESLintDeprecationCfg | ESLintDeprecationCfg[]): Rule.RuleModule {
configs = Array.isArray(configs) ? configs : [configs];
const create = (context: Rule.RuleContext): Rule.RuleListener => ({
ImportSpecifier(node: ImportNode): null {
const importedValue = node.imported;
if (importedValue.type === 'Identifier' && importedValue.name === deprecation) {
context.report({
node,
message: `[ESL Lint]: Deprecated alias ${deprecation} for ${alias}`,
fix: buildFixer(node, context, alias)
});
}
if (importedValue.type !== 'Identifier') return null;

configs.forEach((config) => {
if (importedValue.name === config.deprecation) {
context.report({
node,
message: `[ESL Lint]: Deprecated alias ${config.deprecation} for ${config.alias}`,
fix: buildFixer(node, context, config.alias)
});
}
});
return null;
}
});

return {meta, create};
}

Expand Down
15 changes: 13 additions & 2 deletions eslint-plugin/src/core/deprecated-class-method.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {buildLoggingRule} from './log';
import type * as ESTree from 'estree';
import type {Rule} from 'eslint';

Expand All @@ -22,16 +23,26 @@ export interface ESLintDeprecationStaticMethodCfg {
deprecatedMethod: string;
/** Function that returns recommended method */
getReplacementMethod: (expression: ESTree.CallExpression) => ESLintReplacementMethodCfg | string;
/** Data that indicates whether the rule should be skipped */
skipOn?: {actual: boolean, message: string};
}

type StaticMethodNode = ESTree.MemberExpression & Rule.NodeParentExtension;

/** Builds deprecation rule from {@link ESLintDeprecationStaticMethodCfg} object */
export function buildRule(config: ESLintDeprecationStaticMethodCfg): Rule.RuleModule {
export function buildRule(configs: ESLintDeprecationStaticMethodCfg | ESLintDeprecationStaticMethodCfg[]): Rule.RuleModule {
configs = (Array.isArray(configs) ? configs : [configs]).filter((config) => {
const {skipOn} = config;
if (!skipOn || skipOn.actual) return true;
buildLoggingRule(skipOn.message);
});

const create = (context: Rule.RuleContext): Rule.RuleListener => {
return {
MemberExpression(node: StaticMethodNode): null {
if (isDeprecatedMethod(node, config)) handleCallExpression(node, context, config);
configs.forEach((config) => {
if (isDeprecatedMethod(node, config)) handleCallExpression(node, context, config);
});
return null;
}
};
Expand Down
22 changes: 6 additions & 16 deletions eslint-plugin/src/rules/4/all.rules.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import deprecatedGenerateUid from './deprecated.generate-uid';
import deprecatedDeepCompare from './deprecated.deep-compare';
import deprecatedEventUtils from './deprecated.event-utils';
import deprecatedTraversingQuery from './deprecated.traversing-query';
import deprecatedToggleableActionParams from './deprecated.toggleable-action-params';
import deprecatedAliases from './deprecated.aliases';

import deprecatedBaseDecoratorsPath from './deprecated.base-decorators-path';
import deprecatedMethods from './deprecated.class-methods';

import deprecatedMediaRuleListParse from './deprecated.media-rule-list-parse';
import deprecatedImports from './deprecated.import';

export default {
// Aliases
'deprecated-4-generate-uid': deprecatedGenerateUid,
'deprecated-4-deep-compare': deprecatedDeepCompare,
'deprecated-4-event-utils': deprecatedEventUtils,
'deprecated-4-traversing-query': deprecatedTraversingQuery,
'deprecated-4-toggleable-action-params': deprecatedToggleableActionParams,
'deprecated-4-media-rule-list-parse': deprecatedMediaRuleListParse,
// Paths
'deprecated-4-base-decorators-path': deprecatedBaseDecoratorsPath
'deprecated-4-aliases': deprecatedAliases,
'deprecated-4-class-methods': deprecatedMethods,
'deprecated-4-import': deprecatedImports
};
23 changes: 23 additions & 0 deletions eslint-plugin/src/rules/4/deprecated.aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {buildRule} from '../../core/deprecated-alias';

/**
* Rule for deprecated aliases
*/
export default buildRule([
{
alias: 'ESLEventUtils',
deprecation: 'EventUtils'
}, {
alias: 'isEqual',
deprecation: 'deepCompare'
}, {
alias: 'ESLToggleableActionParams',
deprecation: 'ToggleableActionParams'
}, {
alias: 'randUID',
deprecation: 'generateUId'
}, {
alias: 'ESLTraversingQuery',
deprecation: 'TraversingQuery'
}
]);
29 changes: 29 additions & 0 deletions eslint-plugin/src/rules/4/deprecated.class-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {lte} from 'semver';
import {ESL_PACKAGE_VERSION} from '../../core/check-version';
import {buildRule} from '../../core/deprecated-class-method';

import type * as ESTree from 'estree';
import type {ESLintReplacementMethodCfg} from '../../core/deprecated-class-method';

const AVAILABLE_SINCE = '5.0.0-beta.24';
const isActual = lte(ESL_PACKAGE_VERSION, AVAILABLE_SINCE);

/**
* Rule for deprecated methods
*/
export default buildRule({
className: 'ESLMediaRuleList',
deprecatedMethod: 'parse',
getReplacementMethod: (expression): ESLintReplacementMethodCfg | string => {
const args = expression.arguments;
const isLiteral = (node: ESTree.Expression | ESTree.SpreadElement): boolean => node?.type === 'Literal' || node?.type === 'TemplateLiteral';
if (expression.type === 'CallExpression' && args.length === 1) return 'parseQuery';
if (expression.type === 'CallExpression' && args.length === 2 && isLiteral(args[1])) return 'parseTuple';
if (expression.type === 'CallExpression' && args.length === 3) return 'parseTuple';
return {message: 'ESLMediaRuleList.parseQuery or ESLMediaRuleList.parseTuple'};
},
skipOn: {
actual: isActual,
message: `'ESLMediaRuleList.parse' was updated in v${AVAILABLE_SINCE}. Rule 'deprecated-4/media-rule-list-parse' is skipped.`
}
});
9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/4/deprecated.deep-compare.ts

This file was deleted.

9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/4/deprecated.event-utils.ts

This file was deleted.

9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/4/deprecated.generate-uid.ts

This file was deleted.

28 changes: 0 additions & 28 deletions eslint-plugin/src/rules/4/deprecated.media-rule-list-parse.ts

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/4/deprecated.traversing-query.ts

This file was deleted.

10 changes: 2 additions & 8 deletions eslint-plugin/src/rules/5/all.rules.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import deprecatedAlertActionParams from './deprecated.alert-action-params';
import deprecatedPanelActionParams from './deprecated.panel-action-params';
import deprecatedPopupActionParams from './deprecated.popup-action-params';
import deprecatedTooltipActionParams from './deprecated.tooltip-action-params';
import deprecatedAliases from './deprecated.aliases';

export default {
'deprecated-5-alert-action-params': deprecatedAlertActionParams,
'deprecated-5-panel-action-params': deprecatedPanelActionParams,
'deprecated-5-popup-action-params': deprecatedPopupActionParams,
'deprecated-5-tooltip-action-params': deprecatedTooltipActionParams
'deprecated-5-aliases': deprecatedAliases,
};
9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/5/deprecated.alert-action-params.ts

This file was deleted.

20 changes: 20 additions & 0 deletions eslint-plugin/src/rules/5/deprecated.aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {buildRule} from '../../core/deprecated-alias';

/**
* Rule for deprecated aliases
*/
export default buildRule([
{
alias: 'ESLAlertActionParams',
deprecation: 'AlertActionParams'
}, {
alias: 'ESLPanelActionParams',
deprecation: 'PanelActionParams'
}, {
alias: 'ESLPopupActionParams',
deprecation: 'PopupActionParams'
}, {
alias: 'ESLTooltipActionParams',
deprecation: 'TooltipActionParams'
}
]);
9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/5/deprecated.panel-action-params.ts

This file was deleted.

9 changes: 0 additions & 9 deletions eslint-plugin/src/rules/5/deprecated.popup-action-params.ts

This file was deleted.

10 changes: 0 additions & 10 deletions eslint-plugin/src/rules/5/deprecated.tooltip-action-params.ts

This file was deleted.

0 comments on commit 08e4ba6

Please sign in to comment.