Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint 9 #34

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
],
"devDependencies": {
"@release-it/conventional-changelog": "8.0.1",
"@types/eslint": "^8.56.10",
"@types/jest": "27.0.2",
"@types/node": "16.11.7",
"@typescript-eslint/eslint-plugin": "5.3.1",
"@typescript-eslint/parser": "5.3.1",
"auto-changelog": "2.4.0",
"eslint": "8.57.0",
"eslint": "^9.0.0",
"jest": "27.3.1",
"prettier": "2.4.1",
"release-it": "17.0.1",
Expand Down
59 changes: 8 additions & 51 deletions src/prefer-arrow-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
} from './config';
import rule from './prefer-arrow-functions';

// TODO: Remove this once Jest starts supporting modern JavaScript
globalThis.structuredClone ??= obj => JSON.parse(JSON.stringify(obj));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like ESLint now uses structuredClone so we need to "mock" it for Jest because Jest doesn't support that yet.


const alwaysValid = [
{
code: 'var foo = (bar) => bar;',
Expand Down Expand Up @@ -492,14 +495,6 @@ const invalidAndHasBlockStatement = [
code: 'async function foo(a) { console.log(3); }',
output: 'const foo = async (a) => { console.log(3); };',
},
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new RuleTester by ESLint fails if you have duplicated tests, so I had to remove a bunch of duplicated tests.

code: 'function foo(a) { console.log(3); }',
output: 'const foo = (a) => { console.log(3); };',
},
{
code: 'async function foo(a) { console.log(3); }',
output: 'const foo = async (a) => { console.log(3); };',
},

// https://github.com/JamieMason/eslint-plugin-prefer-arrow-functions/issues/28
{
Expand Down Expand Up @@ -576,22 +571,6 @@ const invalidAndHasBlockStatement = [
code: 'var foo = async function() { console.log({a: false}); }',
output: 'var foo = async () => { console.log({a: false}); }',
},
{
code: 'var foo = function() { console.log({a: false}); }',
output: 'var foo = () => { console.log({a: false}); }',
},
{
code: 'var foo = async function() { console.log({a: false}); }',
output: 'var foo = async () => { console.log({a: false}); }',
},
{
code: 'function foo(a) { console.log({a: false}); }',
output: 'const foo = (a) => { console.log({a: false}); };',
},
{
code: 'async function foo(a) { console.log({a: false}); }',
output: 'const foo = async (a) => { console.log({a: false}); };',
},
{
code: 'function foo(a) { console.log({a: false}); }',
output: 'const foo = (a) => { console.log({a: false}); };',
Expand All @@ -614,22 +593,6 @@ const invalidAndHasBlockStatement = [
code: 'var foo = async function() {\n console.log("World");\n}',
output: 'var foo = async () => {\n console.log("World");\n}',
},
{
code: 'var foo = function() {\n console.log("World");\n}',
output: 'var foo = () => {\n console.log("World");\n}',
},
{
code: 'var foo = async function() {\n console.log("World");\n}',
output: 'var foo = async () => {\n console.log("World");\n}',
},
{
code: 'function foo(a) {\n console.log(3);\n}',
output: 'const foo = (a) => {\n console.log(3);\n};',
},
{
code: 'async function foo(a) {\n console.log(3);\n}',
output: 'const foo = async (a) => {\n console.log(3);\n};',
},
{
code: 'function foo(a) {\n console.log(3);\n}',
output: 'const foo = (a) => {\n console.log(3);\n};',
Expand Down Expand Up @@ -750,7 +713,6 @@ const validWhenAllowNamedFunctions = [
{
// Make sure "allowNamedFunctions" works with typescript
code: '() => { function foo(a: string): string { return `bar ${a}`;} }',
parser: require.resolve('@typescript-eslint/parser'),
},
];

Expand All @@ -768,10 +730,6 @@ const invalidWhenAllowNamedFunctions = [
code: '() => { var foo = function() { return () => "bar"; }; }',
output: '() => { var foo = () => () => "bar"; }',
},
{
code: '() => { var foo = function() { return "bar"; }; }',
output: '() => { var foo = () => "bar"; }',
},
{
code: 'module.exports = () => { var foo = function() { return "bar"; }; }',
output: 'module.exports = () => { var foo = () => "bar"; }',
Expand Down Expand Up @@ -809,16 +767,15 @@ const invalidWhenAllowNamedFunctions = [
output: `function foo(a: string): () => string {
return () => \`bar \${a}\`;
}`,
parser: require.resolve('@typescript-eslint/parser'),
},
];

const ruleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaFeatures: { jsx: false },
ecmaVersion: 8,
sourceType: 'module',
// TODO: Remove this `languageOptions` once the tests are updated for flat config:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the tests are updated to support flat config, this can go back to the original values. The new default for RuleTester expects flat config tests.

languageOptions: {
ecmaVersion: 5, // Original value was 8 here
parser: require('@typescript-eslint/parser'),
sourceType: 'script' // Original value was 'module' here
},
});

Expand Down
12 changes: 6 additions & 6 deletions src/prefer-arrow-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
const classPropertiesAllowed = getOption('classPropertiesAllowed');
const disallowPrototype = getOption('disallowPrototype');
const returnStyle = getOption('returnStyle');
const sourceCode = context.getSourceCode();
const { sourceCode } = context;

const isBlockStatementWithSingleReturn = (node) => {
return (
Expand Down Expand Up @@ -193,8 +193,8 @@ export default {
};

const isPrototypeAssignment = (node) => {
return context
.getAncestors()
return sourceCode
Copy link
Contributor Author

@loucyx loucyx Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the "key" changes, made following the official documentation.

.getAncestors(node)
.reverse()
.some((ancestor) => {
const isPropertyOfReplacementPrototypeObject =
Expand All @@ -216,8 +216,8 @@ export default {
};

const isWithinClassBody = (node) => {
return context
.getAncestors()
return sourceCode
.getAncestors(node)
.reverse()
.some((ancestor) => {
return ancestor.type === 'ClassBody';
Expand Down Expand Up @@ -334,4 +334,4 @@ export default {
},
};
},
};
} as const;
Loading
Loading