Skip to content

Commit e5982ef

Browse files
authored
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
1 parent 75f65eb commit e5982ef

File tree

8 files changed

+110
-69
lines changed

8 files changed

+110
-69
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; EditorConfig file: https://EditorConfig.org
2+
; Install the "EditorConfig" plugin into your editor to use
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[*.{json,md}]
15+
indent_size = 2

.eslintignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/node_modules
22
/tests/fixtures
3-
/tools/*
4-
!/tools/update-ecma-version-tests.js
53
/dist
4+
tools/create-test-example.js

.eslintrc.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module.exports = {
66
env: {
77
es2020: true
88
},
9+
settings: {
10+
jsdoc: {
11+
mode: "typescript"
12+
}
13+
},
914
parserOptions: {
1015
ecmaVersion: 2020,
1116
sourceType: "module"

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ _test.js
99
.nyc_output
1010
.eslint-release-info.json
1111
yarn.lock
12-
package-lock.json
12+
package-lock.json

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@
4040
"@rollup/plugin-json": "^4.1.0",
4141
"@rollup/plugin-node-resolve": "^11.2.0",
4242
"c8": "^7.11.0",
43-
"chai": "^4.3.4",
44-
"eslint": "^7.22.0",
43+
"chai": "^4.3.6",
44+
"eslint": "^8.13.0",
4545
"eslint-config-eslint": "^7.0.0",
46-
"eslint-plugin-jsdoc": "^32.2.0",
46+
"eslint-plugin-jsdoc": "^39.2.4",
4747
"eslint-plugin-node": "^11.1.0",
4848
"eslint-release": "^3.2.0",
49-
"esprima": "latest",
5049
"esprima-fb": "^8001.2001.0-dev-harmony-fb",
51-
"json-diff": "^0.5.4",
52-
"mocha": "^8.3.1",
50+
"mocha": "^9.2.2",
5351
"npm-run-all": "^4.1.5",
5452
"rollup": "^2.41.2",
5553
"shelljs": "^0.3.0"
@@ -67,7 +65,7 @@
6765
"unit:esm": "c8 mocha --color --reporter progress --timeout 30000 'tests/lib/**/*.js'",
6866
"unit:cjs": "mocha --color --reporter progress --timeout 30000 tests/lib/commonjs.cjs",
6967
"test": "npm-run-all -p unit lint",
70-
"lint": "eslint \"*.?(c)js\" lib/ tests/lib/",
68+
"lint": "eslint .",
7169
"fixlint": "npm run lint -- --fix",
7270
"build": "rollup -c rollup.config.js",
7371
"update-version": "node tools/update-version.js",

tools/create-test.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable node/no-process-exit */
12
/**
23
* @fileoverview A simple script to help generate test cases
34
* @author Nicholas C. Zakas
@@ -15,20 +16,46 @@
1516
//------------------------------------------------------------------------------
1617

1718
import shelljs from "shelljs";
18-
import { parse } from "../espree.js"
19+
import { parse } from "../espree.js";
1920
import path from "path";
2021
import { fileURLToPath } from "url";
2122

2223
//------------------------------------------------------------------------------
2324
// Initialization
2425
//------------------------------------------------------------------------------
2526

27+
// eslint-disable-next-line no-underscore-dangle -- Conventional
2628
const __dirname = path.dirname(fileURLToPath(import.meta.url));
27-
var PATTERN = /\/\*!espree\-section:\s*[a-z\d\-]+\*\//gi;
29+
const PATTERN = /\/\*!espree-section:\s*[a-z\d-]+\*\//giu;
2830

29-
var filename = process.argv[2],
31+
const filename = process.argv[2],
3032
codeFilename = process.argv[3];
3133

34+
/**
35+
* @typedef {{start: number, end: number}} StartEnd
36+
*/
37+
38+
/**
39+
* acorn adds these "start" and "end" properties
40+
* which we don't officially support, we remove
41+
* them before creating our test fixtures
42+
* @param {StartEnd[]} o The array or object to modify
43+
* @returns {void}
44+
*/
45+
function recursivelyRemoveStartAndEnd(o) {
46+
if (Array.isArray(o)) {
47+
o.forEach(recursivelyRemoveStartAndEnd);
48+
return;
49+
}
50+
if (o && typeof o === "object") {
51+
delete o.start;
52+
delete o.end;
53+
Object.keys(o).filter(key => key !== "loc").forEach(key => {
54+
recursivelyRemoveStartAndEnd(o[key]);
55+
});
56+
}
57+
}
58+
3259
if (!codeFilename) {
3360
console.error("Missing code to generate tests for");
3461
console.error("Usage: node create-test.js ecma-features/binaryLiterals/ file_with_code.js");
@@ -41,7 +68,7 @@ if (!filename) {
4168
process.exit(1);
4269
}
4370

44-
var rawCode = shelljs.cat(codeFilename),
71+
const rawCode = shelljs.cat(codeFilename),
4572
code = rawCode.split(PATTERN),
4673
sections = rawCode.match(PATTERN);
4774

@@ -53,13 +80,13 @@ if (!sections || sections.length !== code.length) {
5380
process.exit(1);
5481
}
5582

56-
code.forEach(function(source, index) {
83+
code.forEach((source, index) => {
5784

58-
var fullFilename = filename + "/" + (sections[index].substring(18, sections[index].length - 2).trim()),
59-
testSourceFilename = path.resolve(__dirname, "../tests/fixtures/" + fullFilename + ".src.js"),
60-
testResultFilename = path.resolve(__dirname, "../tests/fixtures/" + fullFilename + ".result.js");
85+
const fullFilename = `${filename}/${sections[index].slice(18, sections[index].length - 2).trim()}`,
86+
testSourceFilename = path.resolve(__dirname, `../tests/fixtures/${fullFilename}.src.js`),
87+
testResultFilename = path.resolve(__dirname, `../tests/fixtures/${fullFilename}.result.js`);
6188

62-
var result,
89+
let result,
6390
sourceCode = source.trim();
6491

6592
// 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) {
77104
ecmaFeatures: {
78105
experimentalObjectRestSpread: true
79106
},
80-
sourceType: 'script', // change as needed
107+
sourceType: "script", // change as needed
81108
loc: true,
82109
range: true,
83110
tokens: true
@@ -91,32 +118,13 @@ code.forEach(function(source, index) {
91118
};
92119
}
93120

94-
recursivelyRemoveStartAndEnd(result)
121+
recursivelyRemoveStartAndEnd(result);
95122

96123
sourceCode.to(testSourceFilename);
97124

98-
let resultCode = `export default ${JSON.stringify(result, (key, value) => {
99-
return (typeof value === "bigint") ? `bigint<${value}n>` : value;
100-
}, 4)};`;
101-
resultCode = resultCode.replace(/"bigint<(\d+n)>"/g, "$1");
125+
let resultCode = `export default ${JSON.stringify(result, (key, value) =>
126+
((typeof value === "bigint") ? `bigint<${value}n>` : value), 4)};`;
127+
128+
resultCode = resultCode.replace(/"bigint<(\d+n)>"/gu, "$1");
102129
resultCode.to(testResultFilename);
103130
});
104-
105-
// acorn adds these "start" and "end" properties
106-
// which we don't officially support, we we remove
107-
// them before creating our test fixtures
108-
function recursivelyRemoveStartAndEnd(o) {
109-
if (Array.isArray(o)) {
110-
o.forEach(recursivelyRemoveStartAndEnd)
111-
return
112-
}
113-
if (o && typeof o === 'object') {
114-
delete o.start
115-
delete o.end
116-
Object.keys(o).filter(function(key) {
117-
return key !== 'loc'
118-
}).forEach(function(key) {
119-
recursivelyRemoveStartAndEnd(o[key])
120-
})
121-
}
122-
}

tools/update-tests.js

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,38 @@ import { fileURLToPath } from "url";
2121
// Helpers
2222
//------------------------------------------------------------------------------
2323

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

27+
/**
28+
* Gets test file names
29+
* @param {string} directory The directory
30+
* @returns {string[]} The file names
31+
*/
2632
function getTestFilenames(directory) {
27-
return shelljs.find(directory).filter(function(filename) {
28-
return filename.indexOf(".src.js") > -1;
29-
}).map(function(filename) {
30-
return filename.substring(directory.length - 1, filename.length - 7); // strip off ".src.js"
31-
});
33+
return shelljs.find(directory).filter(filename =>
34+
filename.indexOf(".src.js") > -1).map(filename =>
35+
filename.slice(directory.length - 1, filename.length - 7)); // strip off ".src.js"
3236
}
3337

38+
/**
39+
* Gets library file names
40+
* @param {string} directory The directory
41+
* @returns {string[]} The file names
42+
*/
3443
function getLibraryFilenames(directory) {
35-
return shelljs.find(directory).filter(function(filename) {
36-
return filename.indexOf(".js") > -1 && filename.indexOf(".result.js") === -1;
37-
}).map(function(filename) {
38-
return filename.substring(directory.length - 1); // strip off directory
39-
});
44+
return shelljs.find(directory).filter(filename =>
45+
filename.indexOf(".js") > -1 &&
46+
filename.indexOf(".result.js") === -1).map(filename =>
47+
filename.slice(directory.length - 1)); // strip off directory
4048
}
4149

50+
/**
51+
* Outputs the result.
52+
* @param {any} result The result
53+
* @param {string} testResultFilename Test result file name
54+
* @returns {void}
55+
*/
4256
function outputResult(result, testResultFilename) {
4357
`export default ${tester.getAstCode(result)};`.to(testResultFilename);
4458
}
@@ -47,29 +61,30 @@ function outputResult(result, testResultFilename) {
4761
// Setup
4862
//------------------------------------------------------------------------------
4963

50-
var FIXTURES_DIR = "./tests/fixtures/ecma-features",
64+
const FIXTURES_DIR = "./tests/fixtures/ecma-features",
5165
LIBRARIES_DIR = "./tests/fixtures/libraries";
5266

53-
var testFiles = getTestFilenames(FIXTURES_DIR),
67+
const testFiles = getTestFilenames(FIXTURES_DIR),
5468
libraryFiles = getLibraryFilenames(LIBRARIES_DIR);
5569

56-
libraryFiles.forEach(function(filename) {
57-
var testResultFilename = path.resolve(__dirname, "..", LIBRARIES_DIR, filename) + ".result.json",
58-
code = shelljs.cat(path.resolve(LIBRARIES_DIR, filename)),
59-
result = getExpectedResult(code, {
60-
loc: true,
61-
range: true,
62-
tokens: true
63-
});
70+
libraryFiles.forEach(filename => {
71+
const testResultFilename = `${path.resolve(__dirname, "..", LIBRARIES_DIR, filename)}.result.json`,
72+
code = shelljs.cat(path.resolve(LIBRARIES_DIR, filename));
73+
let result = tester.getExpectedResult(code, {
74+
loc: true,
75+
range: true,
76+
tokens: true
77+
});
78+
6479
JSON.stringify(result).to(testResultFilename);
6580
result = null;
6681
});
6782

6883
// update all tests in ecma-features
69-
testFiles.forEach(function(filename) {
84+
testFiles.forEach(filename => {
7085

71-
var feature = path.dirname(filename),
72-
code = shelljs.cat(path.resolve(FIXTURES_DIR, filename) + ".src.js"),
86+
const feature = path.dirname(filename),
87+
code = shelljs.cat(`${path.resolve(FIXTURES_DIR, filename)}.src.js`),
7388
config = {
7489
loc: true,
7590
range: true,
@@ -79,8 +94,8 @@ testFiles.forEach(function(filename) {
7994
};
8095

8196
config.ecmaFeatures[feature] = true;
82-
var testResultFilename = path.resolve(__dirname, "..", FIXTURES_DIR, filename) + ".result.js";
83-
var result = getExpectedResult(code, config);
97+
const testResultFilename = `${path.resolve(__dirname, "..", FIXTURES_DIR, filename)}.result.js`;
98+
const result = tester.getExpectedResult(code, config);
8499

85100
outputResult(result, testResultFilename);
86101
});

tools/update-version.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ import fs from "fs";
1414
*/
1515

1616
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8"));
17+
1718
fs.writeFileSync("lib/version.js", `const version = "${pkg.version}";\n\nexport default version;\n`);

0 commit comments

Comments
 (0)