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

chore: lint tool files, add editorconfig, update devDeps. #545

Merged
merged 4 commits into from Apr 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .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
3 changes: 1 addition & 2 deletions .eslintignore
@@ -1,5 +1,4 @@
/node_modules
/tests/fixtures
/tools/*
!/tools/update-ecma-version-tests.js
/dist
tools/create-test-example.js
5 changes: 5 additions & 0 deletions .eslintrc.cjs
Expand Up @@ -6,6 +6,11 @@ module.exports = {
env: {
es2020: true
},
settings: {
jsdoc: {
mode: "typescript"
}
},
parserOptions: {
ecmaVersion: 2020,
sourceType: "module"
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -9,4 +9,4 @@ _test.js
.nyc_output
.eslint-release-info.json
yarn.lock
package-lock.json
package-lock.json
12 changes: 5 additions & 7 deletions package.json
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
76 changes: 42 additions & 34 deletions 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
Expand All @@ -15,20 +16,46 @@
//------------------------------------------------------------------------------

import shelljs from "shelljs";
import { parse } from "../espree.js"
import { parse } from "../espree.js";
import path from "path";
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");
Expand All @@ -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);

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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])
})
}
}
65 changes: 40 additions & 25 deletions tools/update-tests.js
Expand Up @@ -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);
}
Expand All @@ -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,
Expand All @@ -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);
});
1 change: 1 addition & 0 deletions tools/update-version.js
Expand Up @@ -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`);