From 608a90cf36a4f8c18b7f4dca40b56e06e0d78f72 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Feb 2022 23:50:26 +0800 Subject: [PATCH] Require ESLint 8 (#1724) --- configs/recommended.js | 2 +- package.json | 4 +-- readme.md | 2 +- test/package.mjs | 63 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/configs/recommended.js b/configs/recommended.js index 6d7182f2c9..1864568d3b 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -1,7 +1,7 @@ 'use strict'; module.exports = { env: { - es6: true, + es2021: true, }, parserOptions: { ecmaVersion: 'latest', diff --git a/package.json b/package.json index bdfee944ed..b16e54e363 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "c8": "^7.11.0", "chalk": "^5.0.0", "enquirer": "^2.3.6", - "eslint": "^8.6.0", + "eslint": "^8.8.0", "eslint-ava-rule-tester": "^4.0.0", "eslint-plugin-eslint-plugin": "^4.1.0", "eslint-plugin-internal-rules": "file:./scripts/internal-rules/", @@ -90,7 +90,7 @@ "xo": "^0.47.0" }, "peerDependencies": { - "eslint": ">=7.32.0" + "eslint": ">=8.8.0" }, "ava": { "files": [ diff --git a/readme.md b/readme.md index 75ca706550..4f930c4536 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,7 @@ Use a [preset config](#preset-configs) or configure each rules in `package.json` "name": "my-awesome-project", "eslintConfig": { "env": { - "es6": true + "es2021": true }, "parserOptions": { "ecmaVersion": "latest", diff --git a/test/package.mjs b/test/package.mjs index f205a60961..1097712f43 100644 --- a/test/package.mjs +++ b/test/package.mjs @@ -2,6 +2,7 @@ import fs, {promises as fsAsync} from 'node:fs'; import path from 'node:path'; import test from 'ava'; import {ESLint} from 'eslint'; +import * as eslintrc from '@eslint/eslintrc'; import eslintPluginUnicorn from '../index.js'; import {RULE_NOTICE_MARK, getRuleNoticesSectionBody} from '../scripts/rule-notices.mjs'; import {RULES_TABLE_MARK, getRulesTable} from '../scripts/rules-table.mjs'; @@ -121,6 +122,68 @@ 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(/^'(?.*)' 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[availableEnvironments.length - 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}), []); + } }); test('Every rule is defined in readme.md usage and list of rules in alphabetical order', async t => {