Skip to content

Commit

Permalink
feat(lint): add deprecation warning regarding incorrect direct import…
Browse files Browse the repository at this point in the history
…s for `attr`, `prop`, `jsonAttr`, `boolAttr`, `listen`
  • Loading branch information
ala-n committed May 23, 2024
1 parent 6f59414 commit e592048
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
43 changes: 43 additions & 0 deletions eslint/src/core/deprecated-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type * as ESTree from 'estree';
import type {Rule} from 'eslint';

const meta: Rule.RuleModule['meta'] = {
type: 'suggestion',
docs: {
description: 'replace deprecated path with a recommend one or use the library root & tree shaking',
recommended: true
}
};

export interface ESLintDeprecationImportCfg {
/** Alias name */
alias: string;
/** Deprecated name */
deprecationPath: string;
/** Recommended path */
recommendedPath: string;
}

type ImportNode = ESTree.ImportSpecifier & Rule.NodeParentExtension;

/** Builds deprecation rule from {@link ESLintDeprecationCfg} object */
export function buildRule(configs: ESLintDeprecationImportCfg | ESLintDeprecationImportCfg[]): Rule.RuleModule {
configs = Array.isArray(configs) ? configs : [configs];
const create = (context: Rule.RuleContext): Rule.RuleListener => ({
ImportSpecifier(node: ImportNode): null {
const importedValue = node.imported;
const importedSource = (node.parent as ESTree.ImportDeclaration).source.value;

for (const cfg of configs) {
if (importedValue.name === cfg.alias && importedSource === cfg.deprecationPath) {
context.report({
node,
message: `[ESL Lint]: Deprecated path for ${cfg.alias}, use ${cfg.recommendedPath} instead`
});
}
}
return null;
}
});
return {meta, create};
}
1 change: 1 addition & 0 deletions eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const configs = {
},
'default': {
rules: {
...buildDefault(DEPRECATED_4_RULES, 'error'),
...buildDefault(DEPRECATED_5_RULES, 'warn')
}
}
Expand Down
7 changes: 6 additions & 1 deletion eslint/src/rules/4/all.rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import deprecatedEventUtils from './deprecated.event-utils';
import deprecatedTraversingQuery from './deprecated.traversing-query';
import deprecatedToggleableActionParams from './deprecated.toggleable-action-params';

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

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/toggleable-action-params': deprecatedToggleableActionParams,
// Paths
'deprecated-4/base-decorators-path': deprecatedBaseDecoratorsPath
};
57 changes: 57 additions & 0 deletions eslint/src/rules/4/deprecated.base-decorators-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {buildRule} from '../../core/deprecated-path';

/**
* Rule for deprecated `@exadel/esl/modules/esl-base-element/core` path to import {@link attr}, originally from `@exadel/esl/modules/esl-utils/decorators/attr`.
*/
export default buildRule([
{
alias: 'attr',
deprecationPath: '@exadel/esl/modules/esl-base-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'prop',
deprecationPath: '@exadel/esl/modules/esl-base-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'boolAttr',
deprecationPath: '@exadel/esl/modules/esl-base-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'jsonAttr',
deprecationPath: '@exadel/esl/modules/esl-base-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'listen',
deprecationPath: '@exadel/esl/modules/esl-base-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'attr',
deprecationPath: '@exadel/esl/modules/esl-mixin-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'prop',
deprecationPath: '@exadel/esl/modules/esl-mixin-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'boolAttr',
deprecationPath: '@exadel/esl/modules/esl-mixin-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'jsonAttr',
deprecationPath: '@exadel/esl/modules/esl-mixin-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
},
{
alias: 'listen',
deprecationPath: '@exadel/esl/modules/esl-mixin-element/core',
recommendedPath: '@exadel/esl/modules/esl-utils/decorators'
}
]);

0 comments on commit e592048

Please sign in to comment.