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

Use FlatESLint in package test #2249

Merged
merged 1 commit into from Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 6 additions & 69 deletions test/package.mjs
Expand Up @@ -2,10 +2,12 @@ import fs, {promises as fsAsync} from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import test from 'ava';
import {ESLint} from 'eslint';
import eslintExperimentalApis from 'eslint/use-at-your-own-risk';
import * as eslintrc from '@eslint/eslintrc';
import eslintPluginUnicorn from '../index.js';

const {FlatESLint} = eslintExperimentalApis;

let ruleFiles;

test.before(async () => {
Expand Down Expand Up @@ -76,13 +78,10 @@ test('Every rule is defined in index file in alphabetical order', t => {

test('validate configuration', async t => {
const results = await Promise.all(
Object.entries(eslintPluginUnicorn.configs).filter(([name]) => !name.startsWith('flat/')).map(async ([name, config]) => {
const eslint = new ESLint({
Object.entries(eslintPluginUnicorn.configs).filter(([name]) => name.startsWith('flat/')).map(async ([name, config]) => {
const eslint = new FlatESLint({
baseConfig: config,
useEslintrc: false,
plugins: {
unicorn: eslintPluginUnicorn,
},
overrideConfigFile: true,
});

const result = await eslint.calculateConfigForFile('dummy.js');
Expand All @@ -98,68 +97,6 @@ test('validate configuration', async t => {
`Configuration for "${name}" is invalid.`,
);
}

// `env`
{
// https://github.com/eslint/eslint/blob/32ac37a76b2e009a8f106229bc7732671d358189/conf/globals.js#L19
const testObjects = [
'undefinedGlobalObject',
// `es3`
'Array',
// `es5`
'JSON',
// `es2015`(`es6`)
'Promise',
// `es2021`
'WeakRef',
];
const baseOptions = {
useEslintrc: false,
plugins: {
unicorn: eslintPluginUnicorn,
},
overrideConfig: {
rules: {
'no-undef': 'error',
},
},
};
const getUndefinedGlobals = async options => {
const [{messages}] = await new ESLint({...baseOptions, ...options}).lintText(testObjects.join(';\n'));
return messages.map(({message}) => message.match(/^'(?<object>.*)' is not defined\.$/).groups.object);
};

t.deepEqual(await getUndefinedGlobals(), ['undefinedGlobalObject', 'Promise', 'WeakRef']);
t.deepEqual(await getUndefinedGlobals({baseConfig: eslintPluginUnicorn.configs.recommended}), ['undefinedGlobalObject']);

const availableEnvironments = [...eslintrc.Legacy.environments.keys()].filter(name => /^es\d+$/.test(name));
const recommendedEnvironments = Object.keys(eslintPluginUnicorn.configs.recommended.env);
t.is(recommendedEnvironments.length, 1);
t.is(
availableEnvironments.at(-1),
recommendedEnvironments[0],
'env should be the latest es version',
);
}

// `sourceType`
{
const text = 'import fs from "node:fs";';
const baseOptions = {
useEslintrc: false,
plugins: {
unicorn: eslintPluginUnicorn,
},
};
const runEslint = async options => {
const [{messages}] = await new ESLint({...baseOptions, ...options}).lintText(text);
return messages;
};

const [{message}] = await runEslint();
t.is(message, 'Parsing error: The keyword \'import\' is reserved');
t.deepEqual(await runEslint({baseConfig: eslintPluginUnicorn.configs.recommended}), []);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FlatESLint uses the latest syntax by default now, I guess we should just remove these.

Copy link
Owner

Choose a reason for hiding this comment

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

👍

});

test('Every rule has valid meta.type', t => {
Expand Down