Skip to content

Commit

Permalink
Merge pull request #2682 from exadel-inc/epic/eslint-clenup
Browse files Browse the repository at this point in the history
feat(lint): adapt eslint plugin to be used with ESLint 9
  • Loading branch information
ala-n authored Sep 27, 2024
2 parents 2901355 + 3345564 commit 2f27f9d
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 122 deletions.
5 changes: 4 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ module.exports = [
...require('./linting/eslint.config.stylistic'),
...require('./linting/eslint.config.editorconfig'),
...require('./linting/eslint.config.import'),
...require('./linting/eslint.config.tsdoc')
...require('./linting/eslint.config.tsdoc'),

// ESL ESLint Plugin
...require('@exadel/eslint-plugin-esl').configs.recommended
];
1 change: 0 additions & 1 deletion eslint/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testEnvironment: 'jsdom',
testRegex: '/test/(.+)\\.test\\.ts$',
moduleFileExtensions: ['ts', 'js', 'json']
};
6 changes: 3 additions & 3 deletions eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {
"prepare": "npm run build",
"test": "",
"test": "jest",
"clean": "rimraf dist",
"build": "npm run clean && tsc --project tsconfig.build.json"
},
Expand All @@ -27,7 +27,7 @@
"@types/eslint": "^9.6.1"
},
"peerDependencies": {
"@exadel/esl": "*",
"eslint": ">=8.0.0"
"@exadel/esl": "^5.0.0 || ^5.0.0-beta.0",
"eslint": ">=8.0.0 || >=9.0.0"
}
}
4 changes: 3 additions & 1 deletion eslint/src/core/deprecated-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function buildRule({deprecation, alias}: ESLintDeprecationCfg): Rule.Rule
const create = (context: Rule.RuleContext): Rule.RuleListener => ({
ImportSpecifier(node: ImportNode): null {
const importedValue = node.imported;
if (importedValue.name === deprecation) {
if (importedValue.type === 'Identifier' && importedValue.name === deprecation) {
context.report({
node,
message: `[ESL Lint]: Deprecated alias ${deprecation} for ${alias}`,
Expand Down Expand Up @@ -61,6 +61,8 @@ function buildFixer(node: ImportNode, context: Rule.RuleContext, alias: string):
// eslint-disable-next-line sonarjs/cognitive-complexity
function getIdentifierRanges(importNode: ImportNode, context: Rule.RuleContext): (AST.Range | undefined)[] {
const root = findRoot(importNode);
if (importNode.imported.type !== 'Identifier' || !root) return [];

const {name} = importNode.imported;
const identifiers = findAllBy(context, root, {type: 'Identifier', name});

Expand Down
2 changes: 2 additions & 0 deletions eslint/src/core/deprecated-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export function buildRule(configs: ESLintDeprecationImportCfg | ESLintDeprecatio
const create = (context: Rule.RuleContext): Rule.RuleListener => ({
ImportSpecifier(node: ImportNode): null {
const importedValue = node.imported;
if (importedValue.type !== 'Identifier') return null;

const importedSource = (node.parent as ESTree.ImportDeclaration).source.value;

for (const cfg of configs) {
Expand Down
25 changes: 22 additions & 3 deletions eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import type {Rule} from 'eslint';

export type logLevel = 'warn' | 'error';

const buildDefault = (definition: Record<string, Rule.RuleModule>, level: logLevel): Record<string, logLevel> => {
const buildDefault = (definition: Record<string, Rule.RuleModule>, level: logLevel, prefix = '@exadel/esl'): Record<string, logLevel> => {
const config: Record<string, logLevel> = {};
for (const name of Object.keys(definition)) {
const rule = `@exadel/esl/${name}`;
config[rule] = level;
config[`${prefix}/${name}`] = level;
}
return config;
};
Expand All @@ -36,3 +35,23 @@ export const configs = {
}
}
};

const plugin = {
rules,
configs
};

// ESLint 9 compatibility
Object.assign(plugin.configs, {
'recommended': [{
plugins: {
'@exadel/esl': plugin
},
rules: {
...buildDefault(DEPRECATED_4_RULES, 'error'),
...buildDefault(DEPRECATED_5_RULES, 'warn')
}
}]
});

export default plugin;
14 changes: 7 additions & 7 deletions eslint/src/rules/4/all.rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import deprecatedMediaRuleListParse from './deprecated.media-rule-list-parse';

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,
'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-base-decorators-path': deprecatedBaseDecoratorsPath
};
8 changes: 4 additions & 4 deletions eslint/src/rules/5/all.rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import deprecatedPopupActionParams from './deprecated.popup-action-params';
import deprecatedTooltipActionParams from './deprecated.tooltip-action-params';

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-alert-action-params': deprecatedAlertActionParams,
'deprecated-5-panel-action-params': deprecatedPanelActionParams,
'deprecated-5-popup-action-params': deprecatedPopupActionParams,
'deprecated-5-tooltip-action-params': deprecatedTooltipActionParams
};
5 changes: 1 addition & 4 deletions eslint/test/deprecated-alias.invalid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ describe('ESL Migration Rules: Deprecated Alias: invalid', () => {
deprecation: 'DeprecatedClassName',
});

const ruleTester = new RuleTester({
// @ts-ignore
parser: require.resolve('@typescript-eslint/parser')
});
const ruleTester = new RuleTester();

ruleTester.run('../base-rules/deprecated-alias', rule, {valid: [], invalid: INVALID_CASES});
});
8 changes: 2 additions & 6 deletions eslint/test/deprecated-alias.valid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ describe('ESL Migration Rules: Deprecated Alias: valid', () => {
deprecation: 'DeprecatedClassName',
});

const ruleTester = new RuleTester({
// @ts-ignore
parser: require.resolve('@typescript-eslint/parser')
});

ruleTester.run('../base-rules/deprecated-alias', rule, {valid: VALID_CASES, invalid: []});
const ruleTester = new RuleTester();
ruleTester.run('deprecated-alias', rule, {valid: VALID_CASES, invalid: []});
});
74 changes: 2 additions & 72 deletions eslint/test/deprecated-class-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ const INVALID_CASES_TEST_CLASS = [
`,
errors: [
'[ESL Lint]: Deprecated static method TestClass.oldMethod, use newMethodNoArgs instead'
],
output: `
TestClass.oldMethod();
`
]
}, {
code: `
TestClass.oldMethod(1, () => {});
Expand Down Expand Up @@ -97,70 +94,6 @@ const INVALID_CASES_TEST_CLASS = [
}
];

const INVALID_CASES_RULE_LIST = [
{
code: `
const t = ESLMediaRuleList.parse;
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseQuery or ESLMediaRuleList.parseTuple instead'
],
output: `
const t = ESLMediaRuleList.parse;
`
}, {
code: `
ESLMediaRuleList.parse;
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseQuery or ESLMediaRuleList.parseTuple instead'
],
output: `
ESLMediaRuleList.parse;
`
}, {
code: `
ESLMediaRuleList.parse('1 | 2');
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseQuery instead'
],
output: `
ESLMediaRuleList.parseQuery('1 | 2');
`
}, {
code: `
ESLMediaRuleList.parse('1 | 2', '3|4');
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseTuple instead'
],
output: `
ESLMediaRuleList.parseTuple('1 | 2', '3|4');
`
}, {
code: `
ESLMediaRuleList.parse('1 | 2', \`3|4\`);
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseTuple instead'
],
output: `
ESLMediaRuleList.parseTuple('1 | 2', \`3|4\`);
`
}, {
code: `
ESLMediaRuleList.parse('1 | 2', '3|4', String);
`,
errors: [
'[ESL Lint]: Deprecated static method ESLMediaRuleList.parse, use ESLMediaRuleList.parseTuple instead'
],
output: `
ESLMediaRuleList.parseTuple('1 | 2', '3|4', String);
`
}
];

describe('ESL Migration Rules: Deprecated Static Method: valid', () => {
const rule = buildRule({
className: 'TestClass',
Expand All @@ -179,10 +112,7 @@ describe('ESL Migration Rules: Deprecated Static Method: valid', () => {
}
});

const ruleTester = new RuleTester({
// @ts-ignore
parser: require.resolve('@typescript-eslint/parser')
});
const ruleTester = new RuleTester();

ruleTester.run('deprecated-static-method', rule, {valid: VALID_CASES, invalid: INVALID_CASES_TEST_CLASS});
});
40 changes: 21 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"concurrently": "^9.0.1",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"eslint": "^9.11.0",
"eslint": "^9.11.1",
"eslint-plugin-editorconfig": "^4.0.3",
"eslint-plugin-import-x": "^4.3.0",
"eslint-plugin-sonarjs": "^1.0.4",
Expand Down

0 comments on commit 2f27f9d

Please sign in to comment.