From e5982ef11cc546826fc76467c93dbb33e981550c Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 21 Apr 2022 08:26:30 +0800 Subject: [PATCH] chore: lint tool files, add editorconfig, update devDeps. (#545) * chore: add editorconfig * chore: lint tool files and in TS mode * chore: update test and linting devDeps. * chore: fix update tests tool to use defined variable Also: - chore: further linting --- .editorconfig | 15 ++++++++ .eslintignore | 3 +- .eslintrc.cjs | 5 +++ .gitignore | 2 +- package.json | 12 +++---- tools/create-test.js | 76 +++++++++++++++++++++++------------------ tools/update-tests.js | 65 +++++++++++++++++++++-------------- tools/update-version.js | 1 + 8 files changed, 110 insertions(+), 69 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..d3db7987 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +; EditorConfig file: https://EditorConfig.org +; Install the "EditorConfig" plugin into your editor to use + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.{json,md}] +indent_size = 2 diff --git a/.eslintignore b/.eslintignore index 91e18159..e269a9ac 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,5 +1,4 @@ /node_modules /tests/fixtures -/tools/* -!/tools/update-ecma-version-tests.js /dist +tools/create-test-example.js diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 93fc39dc..10c67363 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -6,6 +6,11 @@ module.exports = { env: { es2020: true }, + settings: { + jsdoc: { + mode: "typescript" + } + }, parserOptions: { ecmaVersion: 2020, sourceType: "module" diff --git a/.gitignore b/.gitignore index 97375681..c9e42c6d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ _test.js .nyc_output .eslint-release-info.json yarn.lock -package-lock.json \ No newline at end of file +package-lock.json diff --git a/package.json b/package.json index 2b21d3f8..78005f5c 100644 --- a/package.json +++ b/package.json @@ -40,16 +40,14 @@ "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^11.2.0", "c8": "^7.11.0", - "chai": "^4.3.4", - "eslint": "^7.22.0", + "chai": "^4.3.6", + "eslint": "^8.13.0", "eslint-config-eslint": "^7.0.0", - "eslint-plugin-jsdoc": "^32.2.0", + "eslint-plugin-jsdoc": "^39.2.4", "eslint-plugin-node": "^11.1.0", "eslint-release": "^3.2.0", - "esprima": "latest", "esprima-fb": "^8001.2001.0-dev-harmony-fb", - "json-diff": "^0.5.4", - "mocha": "^8.3.1", + "mocha": "^9.2.2", "npm-run-all": "^4.1.5", "rollup": "^2.41.2", "shelljs": "^0.3.0" @@ -67,7 +65,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 \"*.?(c)js\" lib/ tests/lib/", + "lint": "eslint .", "fixlint": "npm run lint -- --fix", "build": "rollup -c rollup.config.js", "update-version": "node tools/update-version.js", diff --git a/tools/create-test.js b/tools/create-test.js index 30971c3c..72adf0d2 100644 --- a/tools/create-test.js +++ b/tools/create-test.js @@ -1,3 +1,4 @@ +/* eslint-disable node/no-process-exit */ /** * @fileoverview A simple script to help generate test cases * @author Nicholas C. Zakas @@ -15,7 +16,7 @@ //------------------------------------------------------------------------------ import shelljs from "shelljs"; -import { parse } from "../espree.js" +import { parse } from "../espree.js"; import path from "path"; import { fileURLToPath } from "url"; @@ -23,12 +24,38 @@ import { fileURLToPath } from "url"; // Initialization //------------------------------------------------------------------------------ +// eslint-disable-next-line no-underscore-dangle -- Conventional const __dirname = path.dirname(fileURLToPath(import.meta.url)); -var PATTERN = /\/\*!espree\-section:\s*[a-z\d\-]+\*\//gi; +const PATTERN = /\/\*!espree-section:\s*[a-z\d-]+\*\//giu; -var filename = process.argv[2], +const filename = process.argv[2], codeFilename = process.argv[3]; +/** + * @typedef {{start: number, end: number}} StartEnd + */ + +/** + * acorn adds these "start" and "end" properties + * which we don't officially support, we remove + * them before creating our test fixtures + * @param {StartEnd[]} o The array or object to modify + * @returns {void} + */ +function recursivelyRemoveStartAndEnd(o) { + if (Array.isArray(o)) { + o.forEach(recursivelyRemoveStartAndEnd); + return; + } + if (o && typeof o === "object") { + delete o.start; + delete o.end; + Object.keys(o).filter(key => key !== "loc").forEach(key => { + recursivelyRemoveStartAndEnd(o[key]); + }); + } +} + if (!codeFilename) { console.error("Missing code to generate tests for"); console.error("Usage: node create-test.js ecma-features/binaryLiterals/ file_with_code.js"); @@ -41,7 +68,7 @@ if (!filename) { process.exit(1); } -var rawCode = shelljs.cat(codeFilename), +const rawCode = shelljs.cat(codeFilename), code = rawCode.split(PATTERN), sections = rawCode.match(PATTERN); @@ -53,13 +80,13 @@ if (!sections || sections.length !== code.length) { process.exit(1); } -code.forEach(function(source, index) { +code.forEach((source, index) => { - var fullFilename = filename + "/" + (sections[index].substring(18, sections[index].length - 2).trim()), - testSourceFilename = path.resolve(__dirname, "../tests/fixtures/" + fullFilename + ".src.js"), - testResultFilename = path.resolve(__dirname, "../tests/fixtures/" + fullFilename + ".result.js"); + const fullFilename = `${filename}/${sections[index].slice(18, sections[index].length - 2).trim()}`, + testSourceFilename = path.resolve(__dirname, `../tests/fixtures/${fullFilename}.src.js`), + testResultFilename = path.resolve(__dirname, `../tests/fixtures/${fullFilename}.result.js`); - var result, + let result, sourceCode = source.trim(); // add an extra semicolon if there's not already one at the end - helps normalize empty lines at end of input @@ -77,7 +104,7 @@ code.forEach(function(source, index) { ecmaFeatures: { experimentalObjectRestSpread: true }, - sourceType: 'script', // change as needed + sourceType: "script", // change as needed loc: true, range: true, tokens: true @@ -91,32 +118,13 @@ code.forEach(function(source, index) { }; } - recursivelyRemoveStartAndEnd(result) + recursivelyRemoveStartAndEnd(result); sourceCode.to(testSourceFilename); - let resultCode = `export default ${JSON.stringify(result, (key, value) => { - return (typeof value === "bigint") ? `bigint<${value}n>` : value; - }, 4)};`; - resultCode = resultCode.replace(/"bigint<(\d+n)>"/g, "$1"); + let resultCode = `export default ${JSON.stringify(result, (key, value) => + ((typeof value === "bigint") ? `bigint<${value}n>` : value), 4)};`; + + resultCode = resultCode.replace(/"bigint<(\d+n)>"/gu, "$1"); resultCode.to(testResultFilename); }); - -// acorn adds these "start" and "end" properties -// which we don't officially support, we we remove -// them before creating our test fixtures -function recursivelyRemoveStartAndEnd(o) { - if (Array.isArray(o)) { - o.forEach(recursivelyRemoveStartAndEnd) - return - } - if (o && typeof o === 'object') { - delete o.start - delete o.end - Object.keys(o).filter(function(key) { - return key !== 'loc' - }).forEach(function(key) { - recursivelyRemoveStartAndEnd(o[key]) - }) - } -} diff --git a/tools/update-tests.js b/tools/update-tests.js index 2957255f..670cee3f 100644 --- a/tools/update-tests.js +++ b/tools/update-tests.js @@ -21,24 +21,38 @@ import { fileURLToPath } from "url"; // Helpers //------------------------------------------------------------------------------ +// eslint-disable-next-line no-underscore-dangle -- Conventional const __dirname = path.dirname(fileURLToPath(import.meta.url)); +/** + * Gets test file names + * @param {string} directory The directory + * @returns {string[]} The file names + */ function getTestFilenames(directory) { - return shelljs.find(directory).filter(function(filename) { - return filename.indexOf(".src.js") > -1; - }).map(function(filename) { - return filename.substring(directory.length - 1, filename.length - 7); // strip off ".src.js" - }); + return shelljs.find(directory).filter(filename => + filename.indexOf(".src.js") > -1).map(filename => + filename.slice(directory.length - 1, filename.length - 7)); // strip off ".src.js" } +/** + * Gets library file names + * @param {string} directory The directory + * @returns {string[]} The file names + */ function getLibraryFilenames(directory) { - return shelljs.find(directory).filter(function(filename) { - return filename.indexOf(".js") > -1 && filename.indexOf(".result.js") === -1; - }).map(function(filename) { - return filename.substring(directory.length - 1); // strip off directory - }); + return shelljs.find(directory).filter(filename => + filename.indexOf(".js") > -1 && + filename.indexOf(".result.js") === -1).map(filename => + filename.slice(directory.length - 1)); // strip off directory } +/** + * Outputs the result. + * @param {any} result The result + * @param {string} testResultFilename Test result file name + * @returns {void} + */ function outputResult(result, testResultFilename) { `export default ${tester.getAstCode(result)};`.to(testResultFilename); } @@ -47,29 +61,30 @@ function outputResult(result, testResultFilename) { // Setup //------------------------------------------------------------------------------ -var FIXTURES_DIR = "./tests/fixtures/ecma-features", +const FIXTURES_DIR = "./tests/fixtures/ecma-features", LIBRARIES_DIR = "./tests/fixtures/libraries"; -var testFiles = getTestFilenames(FIXTURES_DIR), +const testFiles = getTestFilenames(FIXTURES_DIR), libraryFiles = getLibraryFilenames(LIBRARIES_DIR); -libraryFiles.forEach(function(filename) { - var testResultFilename = path.resolve(__dirname, "..", LIBRARIES_DIR, filename) + ".result.json", - code = shelljs.cat(path.resolve(LIBRARIES_DIR, filename)), - result = getExpectedResult(code, { - loc: true, - range: true, - tokens: true - }); +libraryFiles.forEach(filename => { + const testResultFilename = `${path.resolve(__dirname, "..", LIBRARIES_DIR, filename)}.result.json`, + code = shelljs.cat(path.resolve(LIBRARIES_DIR, filename)); + let result = tester.getExpectedResult(code, { + loc: true, + range: true, + tokens: true + }); + JSON.stringify(result).to(testResultFilename); result = null; }); // update all tests in ecma-features -testFiles.forEach(function(filename) { +testFiles.forEach(filename => { - var feature = path.dirname(filename), - code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.js"), + const feature = path.dirname(filename), + code = shelljs.cat(`${path.resolve(FIXTURES_DIR, filename)}.src.js`), config = { loc: true, range: true, @@ -79,8 +94,8 @@ testFiles.forEach(function(filename) { }; config.ecmaFeatures[feature] = true; - var testResultFilename = path.resolve(__dirname, "..", FIXTURES_DIR, filename) + ".result.js"; - var result = getExpectedResult(code, config); + const testResultFilename = `${path.resolve(__dirname, "..", FIXTURES_DIR, filename)}.result.js`; + const result = tester.getExpectedResult(code, config); outputResult(result, testResultFilename); }); diff --git a/tools/update-version.js b/tools/update-version.js index 052c561d..977bcaff 100644 --- a/tools/update-version.js +++ b/tools/update-version.js @@ -14,4 +14,5 @@ import fs from "fs"; */ const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8")); + fs.writeFileSync("lib/version.js", `const version = "${pkg.version}";\n\nexport default version;\n`);