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

refactor: switch to eslint flat config #577

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

38 changes: 0 additions & 38 deletions .eslintrc.cjs

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dist
npm-debug.log
_test.js
.idea
.vscode
.nyc_output
.eslint-release-info.json
yarn.lock
Expand Down
54 changes: 54 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import eslintConfigESLint from "eslint-config-eslint";
import nodeRecommendedModule from "eslint-plugin-n/configs/recommended-module.js";
import nodeRecommendedScript from "eslint-plugin-n/configs/recommended-script.js";
import globals from "globals";

export default [
{
ignores: [
"tests/fixtures/",
"dist/",
"coverage/",
"tools/create-test-example.js"
]
},

// eslintConfigESLint[3] is eslint-plugin-n's recommended-script config
...eslintConfigESLint.slice(0, 3),
{
files: ["**/*.js"],
...nodeRecommendedModule
},
{
files: ["**/*.cjs"],
...nodeRecommendedScript
},
...eslintConfigESLint.slice(4),

{
settings: {
jsdoc: {
mode: "typescript"
}
}
},
Comment on lines +29 to +34
Copy link
Member

@aladdin-add aladdin-add Jul 4, 2023

Choose a reason for hiding this comment

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

I've made a PR to move it to eslint-config-eslint.

eslint/eslint#17338

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

We'll remove it from here when we release eslint-config-eslint v9.

{
files: ["tests/lib/**"],
languageOptions: {
globals: {
...globals.mocha
}
}
},
{
files: ["tools/**"],
rules: {
"no-console": "off",
"n/no-process-exit": "off",
"n/no-unsupported-features/es-syntax": ["error", {
version: ">=16.0.0",
ignores: ["modules"]
}]
}
}
];
7 changes: 5 additions & 2 deletions espree.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable jsdoc/no-multi-asterisks -- needed to preserve original formatting of licences */

/**
* @fileoverview Main Espree file that converts Acorn into Esprima output.
*
Expand Down Expand Up @@ -54,7 +56,8 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* eslint no-undefined:0, no-use-before-define: 0 */

/* eslint-enable jsdoc/no-multi-asterisks -- needed to preserve original formatting of licences */

import * as acorn from "acorn";
import jsx from "acorn-jsx";
Expand Down Expand Up @@ -111,7 +114,7 @@ export function tokenize(code, options) {

// Ensure to collect tokens.
if (!options || options.tokens !== true) {
options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign
options = Object.assign({}, options, { tokens: true }); // eslint-disable-line no-param-reassign -- stylistic choice
}

return new Parser(options, code).tokenize();
Expand Down
9 changes: 5 additions & 4 deletions lib/espree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-param-reassign*/
/* eslint no-param-reassign: 0 -- stylistic choice */

import TokenTranslator from "./token-translator.js";
import { normalizeOptions } from "./options.js";

Expand Down Expand Up @@ -109,7 +110,7 @@ export default () => Parser => {
allowReturnOutsideFunction: options.allowReturnOutsideFunction,

// Collect tokens
onToken: token => {
onToken(token) {
Comment on lines -112 to +113
Copy link
Member Author

Choose a reason for hiding this comment

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

This is because in the new eslint-config-eslint the option avoidExplicitReturnArrows: true is set.

if (tokenTranslator) {

// Use `tokens`, `ecmaVersion`, and `jsxAttrValueToken` in the state.
Expand All @@ -121,7 +122,7 @@ export default () => Parser => {
},

// Collect comments
onComment: (block, text, start, end, startLoc, endLoc) => {
onComment(block, text, start, end, startLoc, endLoc) {
if (state.comments) {
const comment = convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, endLoc, code);

Expand Down Expand Up @@ -314,7 +315,7 @@ export default () => Parser => {
* on extra so that when tokens are converted, the next token will be switched
* to JSXText via onToken.
*/
jsx_readString(quote) { // eslint-disable-line camelcase
jsx_readString(quote) { // eslint-disable-line camelcase -- required by API
const result = super.jsx_readString(quote);

if (this.type === tokTypes.string) {
Expand Down
12 changes: 5 additions & 7 deletions lib/token-translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @fileoverview Translates tokens between Acorn format and Esprima format.
* @author Nicholas C. Zakas
*/
/* eslint no-underscore-dangle: 0 */

//------------------------------------------------------------------------------
// Requirements
Expand Down Expand Up @@ -180,8 +179,7 @@ TokenTranslator.prototype = {
*/
onToken(token, extra) {

const that = this,
tt = this._acornTokTypes,
const tt = this._acornTokTypes,
tokens = extra.tokens,
templateTokens = this._tokens;

Expand All @@ -191,10 +189,10 @@ TokenTranslator.prototype = {
* @returns {void}
* @private
*/
function translateTemplateTokens() {
tokens.push(convertTemplatePart(that._tokens, that._code));
that._tokens = [];
}
const translateTemplateTokens = () => {
tokens.push(convertTemplatePart(this._tokens, this._code));
this._tokens = [];
};
Comment on lines -194 to +195
Copy link
Member Author

Choose a reason for hiding this comment

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

Small refactor to avoid disabling no-underscore-dangle, and I think this is a common way to avoid that = this since ES6.


if (token.type === tt.eof) {

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
"@rollup/plugin-node-resolve": "^11.2.0",
"c8": "^7.11.0",
"chai": "^4.3.6",
"eslint": "^8.13.0",
"eslint-config-eslint": "^7.0.0",
"eslint-plugin-jsdoc": "^39.2.4",
"eslint-plugin-node": "^11.1.0",
"eslint": "^8.44.0",
"eslint-config-eslint": "^8.0.0",
"eslint-plugin-n": "^16.0.0",
"eslint-release": "^3.2.0",
"esprima-fb": "^8001.2001.0-dev-harmony-fb",
"globals": "^13.20.0",
"lint-staged": "^13.2.0",
"mocha": "^9.2.2",
"npm-run-all": "^4.1.5",
Expand All @@ -71,7 +71,7 @@
"unit:esm": "c8 mocha --color --reporter progress --timeout 30000 'tests/lib/**/*.js'",
"unit:cjs": "mocha --color --reporter progress --timeout 30000 tests/lib/commonjs.cjs",
"test": "npm-run-all -p unit lint",
"lint": "eslint .",
"lint": "eslint . --report-unused-disable-directives",
"fixlint": "npm run lint -- --fix",
"build": "rollup -c rollup.config.js",
"build:debug": "npm run build -- -m",
Expand Down
1 change: 0 additions & 1 deletion tests/lib/acorn-after-espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe("acorn", () => {
it("acorn.parse() should work after espree was loaded.", async () => {
const before = acorn.parse("var foo = bar /*world*/;", { ecmaVersion: 5 });

// eslint-disable-next-line node/no-unsupported-features/es-syntax
await import("../../espree.js");

const after = acorn.parse("var foo = bar /*world*/;", { ecmaVersion: 5 });
Expand Down
1 change: 0 additions & 1 deletion tests/lib/commonjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @author Mike Reinstein
*/

// eslint-disable-next-line strict
"use strict";

//------------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/conditional-regex-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export default function(literalNode) {
if (literalNode.regex) {
try {
literalNode.value = new RegExp(literalNode.regex.pattern, literalNode.regex.flags);
/* eslint-disable no-unused-vars */
} catch (e) {
} catch {
literalNode.value = null;
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/ecma-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { fileURLToPath, pathToFileURL } from "url";
import tester from "./tester.js";


// eslint-disable-next-line no-underscore-dangle
// eslint-disable-next-line no-underscore-dangle -- Conventional
const __dirname = path.dirname(fileURLToPath(import.meta.url));


Expand Down Expand Up @@ -68,7 +68,6 @@ describe("ecmaFeatures", () => {
it(`should parse correctly when ${feature} is ${isPermissive}`, async () => {
config.ecmaFeatures[feature] = isPermissive;

// eslint-disable-next-line node/no-unsupported-features/es-syntax
const expected = await import(`${pathToFileURL(path.resolve(__dirname, "../../", FIXTURES_DIR, filename)).href}.result.js`);

tester.assertMatches(code, config, expected.default);
Expand Down
5 changes: 1 addition & 4 deletions tests/lib/ecma-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import assert from "assert";
import { fileURLToPath, pathToFileURL } from "url";


// eslint-disable-next-line no-underscore-dangle
// eslint-disable-next-line no-underscore-dangle -- Conventional
const __dirname = path.dirname(fileURLToPath(import.meta.url));


Expand Down Expand Up @@ -73,7 +73,6 @@ describe("ecmaVersion", () => {
}

const absolutePath = path.resolve(__dirname, FIXTURES_DIR, filename.slice(1));
// eslint-disable-next-line node/no-unsupported-features/es-syntax
const expected = await import(`${pathToFileURL(absolutePath).href}.result.js`);

tester.assertMatches(code, config, expected.default);
Expand All @@ -97,10 +96,8 @@ describe("ecmaVersion", () => {
let expected;

try {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
expected = await import(`${pathToFileURL(absolutePath).href}.module-result.js`);
} catch {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
expected = await import(`${pathToFileURL(absolutePath).href}.result.js`);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/libraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as espree from "../../espree.js";
import assert from "assert";
import { fileURLToPath } from "url";

// eslint-disable-next-line no-underscore-dangle
// eslint-disable-next-line no-underscore-dangle -- Conventional
const __dirname = path.dirname(fileURLToPath(import.meta.url));


Expand Down
10 changes: 5 additions & 5 deletions tests/lib/tester-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ describe("Tester", () => {

it("should keep RegExp objects", () => {
assert.deepStrictEqual(
// eslint-disable-next-line require-unicode-regexp
// eslint-disable-next-line require-unicode-regexp -- non-unicode regexp is needed for the test
getRaw({ value: /foo/ }),
// eslint-disable-next-line require-unicode-regexp
// eslint-disable-next-line require-unicode-regexp -- non-unicode regexp is needed for the test
{ value: /foo/ }
);
assert.deepStrictEqual(
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("Tester", () => {
function test(ast) {
const code = getAstCode(ast);

// eslint-disable-next-line no-new-func
// eslint-disable-next-line no-new-func -- it's okay to eval code in tests
return Function(`return ${code};`)();
}

Expand Down Expand Up @@ -115,9 +115,9 @@ describe("Tester", () => {

it("should keep RegExp objects", () => {
assert.deepStrictEqual(
// eslint-disable-next-line require-unicode-regexp
// eslint-disable-next-line require-unicode-regexp -- non-unicode regexp is needed for the test
test({ value: /foo/ }),
// eslint-disable-next-line require-unicode-regexp
// eslint-disable-next-line require-unicode-regexp -- non-unicode regexp is needed for the test
{ value: /foo/ }
);
assert.deepStrictEqual(
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ const ReplacedStringLiteralPattern = new RegExp(
* @returns {any} If `value` parameter is a value JSON cannot handle, this
* function returns a string value. If `key` parameter is `"start"` or `"end"`,
* returns `undefined`. Otherwise, returns `value` parameter as is.
* @throws {Error} If `value` is undefined
* @private
*/
function replaceToSaveNonJSONValues(key, value) {

// Remove `start` and `end` properties because of non-standard.
if ((key === "start" || key === "end") && typeof value === "number") {
return undefined; // eslint-disable-line no-undefined
return void 0;
}

// Save the values JSON cannot handle.
Expand Down Expand Up @@ -155,6 +156,7 @@ function getAstCode(ast) {
* @param {Object} parserOptions The parser options to parse.
* @param {Object} [options] The options.
* @param {boolean} [options.rethrowSyntaxError = false] The flag to rethrow syntax errors.
* @throws {Error} If options.rethrowSyntaxError = true, rethrows syntax errors
* @returns {Object} The normalized AST.
*/
function getExpectedResult(jsCodeText, parserOptions, { rethrowSyntaxError = false } = {}) {
Expand Down
1 change: 0 additions & 1 deletion tools/create-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable node/no-process-exit */
/**
* @fileoverview A simple script to help generate test cases
* @author Nicholas C. Zakas
Expand Down
4 changes: 2 additions & 2 deletions tools/sync-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* @fileoverview Build file
* @author nzakas
*/
/* global cp, echo, rm */

/* eslint no-console: 0*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

import "shelljs/make.js";

/* global cp, echo, rm -- declared in `shelljs/make.js` */


//------------------------------------------------------------------------------
// Data
Expand Down
4 changes: 2 additions & 2 deletions tools/update-ecma-version-tests.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @fileoverview Generate `<test-name>.result.js` from `<test-name>.src.js` in
* `tests/fixtures/ecma-version/<number>/**` directory.
* @author Nicholas C. Zakas
* @author Toru Nagashima
*
* Usage:
* node tools/update-ecma-version-tests.js <number>
*
* @author Nicholas C. Zakas
* @author Toru Nagashima
*/
Comment on lines 1 to 10
Copy link
Member Author

Choose a reason for hiding this comment

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

Some eslint-plugin-jsdoc rules were reporting errors in the structure, so I moved the usage description under the file overview.


//------------------------------------------------------------------------------
Expand Down
Loading