Skip to content

Commit

Permalink
fix: add ESLint v8 compatible built-in rule imports
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 25, 2021
1 parent 55a3b87 commit 986a5ac
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/rules/noUnusedExpressions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// A wrapper around ESLint's core rule no-unused-expressions, additionally ignores type cast
// expressions.

import coreNOE from 'eslint/lib/rules/no-unused-expressions';
import {
getBuiltinRule,
} from '../utilities/getBuiltinRule';

const meta = coreNOE.meta;
const noUnusedExpressionsRule = getBuiltinRule('no-unused-expressions');

const meta = noUnusedExpressionsRule.meta;

const create = (context) => {
const coreChecks = coreNOE.create(context);
const coreChecks = noUnusedExpressionsRule.create(context);

return {
ExpressionStatement (node) {
Expand Down
15 changes: 15 additions & 0 deletions src/utilities/getBuiltinRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable import/no-dynamic-require */

/**
* Adopted from https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/rules/utils/get-builtin-rule.js.
*/
export const getBuiltinRule = (id) => {
// TODO: Remove this when we drop support for ESLint 7
const eslintVersion = require('eslint/package.json').version;
/* istanbul ignore next */
if (eslintVersion.startsWith('7.')) {
return require(`eslint/lib/rules/${id}`);
}

return require('eslint/use-at-your-own-risk').builtinRules.get(id);
};
6 changes: 5 additions & 1 deletion tests/rules/assertions/defineFlowType.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {
RuleTester,
} from 'eslint';
import noUndefRule from 'eslint/lib/rules/no-undef';
import {
getBuiltinRule,
} from '../../../src/utilities/getBuiltinRule';

const noUndefRule = getBuiltinRule('no-undef');

const VALID_WITH_DEFINE_FLOW_TYPE = [
{
Expand Down
6 changes: 5 additions & 1 deletion tests/rules/assertions/useFlowType.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {
RuleTester,
} from 'eslint';
import noUnusedVarsRule from 'eslint/lib/rules/no-unused-vars';
import useFlowType from '../../../src/rules/useFlowType';
import {
getBuiltinRule,
} from '../../../src/utilities/getBuiltinRule';

const noUnusedVarsRule = getBuiltinRule('no-unused-vars');

const VALID_WITH_USE_FLOW_TYPE = [
{
Expand Down

0 comments on commit 986a5ac

Please sign in to comment.