From 3744059b17dbac9dc4c804049f2f12b286f09817 Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Tue, 17 Nov 2020 08:59:59 -0800 Subject: [PATCH 1/3] Upgrade: acorn 8.0.4 --- package.json | 2 +- tests/lib/acorn-after-espree.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 806ba95e..215deab0 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "license": "BSD-2-Clause", "dependencies": { - "acorn": "^7.4.0", + "acorn": "^8.0.4", "acorn-jsx": "^5.2.0", "eslint-visitor-keys": "^1.3.0" }, diff --git a/tests/lib/acorn-after-espree.js b/tests/lib/acorn-after-espree.js index 0c72e2c3..4db3e86d 100644 --- a/tests/lib/acorn-after-espree.js +++ b/tests/lib/acorn-after-espree.js @@ -19,10 +19,10 @@ const acorn = require("acorn"), describe("acorn", () => { it("acorn.parse() should work after espree was loaded.", () => { - const before = acorn.parse("var foo = bar /*world*/;"); + const before = acorn.parse("var foo = bar /*world*/;", {ecmaVersion: 5}); require("../../espree"); - const after = acorn.parse("var foo = bar /*world*/;"); + const after = acorn.parse("var foo = bar /*world*/;", {ecmaVersion: 5}); assert.deepStrictEqual(after, before); }); From 750323a1979570db5ff69989ab47c5414e1bf5fd Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Tue, 17 Nov 2020 09:07:26 -0800 Subject: [PATCH 2/3] Fix: linting errors in acorn tests --- tests/lib/acorn-after-espree.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/acorn-after-espree.js b/tests/lib/acorn-after-espree.js index 4db3e86d..9350ef1c 100644 --- a/tests/lib/acorn-after-espree.js +++ b/tests/lib/acorn-after-espree.js @@ -19,10 +19,10 @@ const acorn = require("acorn"), describe("acorn", () => { it("acorn.parse() should work after espree was loaded.", () => { - const before = acorn.parse("var foo = bar /*world*/;", {ecmaVersion: 5}); + const before = acorn.parse("var foo = bar /*world*/;", { ecmaVersion: 5 }); require("../../espree"); - const after = acorn.parse("var foo = bar /*world*/;", {ecmaVersion: 5}); + const after = acorn.parse("var foo = bar /*world*/;", { ecmaVersion: 5 }); assert.deepStrictEqual(after, before); }); From 82c232dc9fd47a67ca0baaa5e1622fe1fb17196e Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Wed, 18 Nov 2020 08:14:58 -0800 Subject: [PATCH 3/3] Build: generate es module and switch build system from browserify to rollup --- espree.js | 34 +++++++++++++++++----------------- lib/ast-node-types.js | 4 +--- lib/espree.js | 7 +++---- lib/features.js | 4 +--- lib/options.js | 18 +++--------------- lib/token-translator.js | 4 +--- lib/version.js | 1 + lib/visitor-keys.js | 3 +-- package.json | 12 +++++++++++- rollup.config.js | 31 +++++++++++++++++++++++++++++++ 10 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 lib/version.js create mode 100644 rollup.config.js diff --git a/espree.js b/espree.js index d8069528..6c37eb18 100644 --- a/espree.js +++ b/espree.js @@ -56,13 +56,15 @@ */ /* eslint no-undefined:0, no-use-before-define: 0 */ -"use strict"; -const acorn = require("acorn"); -const jsx = require("acorn-jsx"); -const astNodeTypes = require("./lib/ast-node-types"); -const espree = require("./lib/espree"); -const { getLatestEcmaVersion, getSupportedEcmaVersions } = require("./lib/options"); +import * as acorn from "acorn"; +import jsx from "acorn-jsx"; +import astNodeTypes from "./lib/ast-node-types.js"; +import espree from "./lib/espree.js"; +import { getLatestEcmaVersion, getSupportedEcmaVersions } from "./lib/options.js"; +import espreeVersion from './lib/version.js'; +import visitorKeys from 'eslint-visitor-keys'; + // To initialize lazily. const parsers = { @@ -106,7 +108,7 @@ const parsers = { * @throws {SyntaxError} If the input code is invalid. * @private */ -function tokenize(code, options) { +export function tokenize(code, options) { const Parser = parsers.get(options); // Ensure to collect tokens. @@ -128,7 +130,7 @@ function tokenize(code, options) { * @returns {ASTNode} The "Program" AST node. * @throws {SyntaxError} If the input code is invalid. */ -function parse(code, options) { +export function parse(code, options) { const Parser = parsers.get(options); return new Parser(options, code).parse(); @@ -138,15 +140,12 @@ function parse(code, options) { // Public //------------------------------------------------------------------------------ -exports.version = require("./package.json").version; - -exports.tokenize = tokenize; +export const version = espreeVersion; -exports.parse = parse; // Deep copy. /* istanbul ignore next */ -exports.Syntax = (function() { +export const Syntax = (function() { let name, types = {}; @@ -168,10 +167,11 @@ exports.Syntax = (function() { }()); /* istanbul ignore next */ -exports.VisitorKeys = (function() { - return require("eslint-visitor-keys").KEYS; +export const VisitorKeys = (function() { + //return require("eslint-visitor-keys").KEYS; + return visitorKeys.KEYS; }()); -exports.latestEcmaVersion = getLatestEcmaVersion(); +export const latestEcmaVersion = getLatestEcmaVersion(); -exports.supportedEcmaVersions = getSupportedEcmaVersions(); +export const supportedEcmaVersions = getSupportedEcmaVersions(); diff --git a/lib/ast-node-types.js b/lib/ast-node-types.js index 2844024d..9f7c4e35 100644 --- a/lib/ast-node-types.js +++ b/lib/ast-node-types.js @@ -3,8 +3,6 @@ * @author Nicholas C. Zakas */ -"use strict"; - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -15,7 +13,7 @@ // Public //------------------------------------------------------------------------------ -module.exports = { +export default { AssignmentExpression: "AssignmentExpression", AssignmentPattern: "AssignmentPattern", ArrayExpression: "ArrayExpression", diff --git a/lib/espree.js b/lib/espree.js index 50ca6e42..9ca2ea55 100644 --- a/lib/espree.js +++ b/lib/espree.js @@ -1,8 +1,7 @@ -"use strict"; /* eslint-disable no-param-reassign*/ -const TokenTranslator = require("./token-translator"); -const { normalizeOptions } = require("./options"); +import TokenTranslator from "./token-translator.js"; +import { normalizeOptions } from "./options.js"; const STATE = Symbol("espree's internal state"); const ESPRIMA_FINISH_NODE = Symbol("espree's esprimaFinishNode"); @@ -41,7 +40,7 @@ function convertAcornCommentToEsprimaComment(block, text, start, end, startLoc, return comment; } -module.exports = () => Parser => { +export default () => Parser => { const tokTypes = Object.assign({}, Parser.acorn.tokTypes); if (Parser.acornJsx) { diff --git a/lib/features.js b/lib/features.js index d1ad5f85..31467d28 100644 --- a/lib/features.js +++ b/lib/features.js @@ -4,8 +4,6 @@ * @author Nicholas C. Zakas */ -"use strict"; - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -16,7 +14,7 @@ // Public //------------------------------------------------------------------------------ -module.exports = { +export default { // React JSX parsing jsx: false, diff --git a/lib/options.js b/lib/options.js index eddca200..855d5187 100644 --- a/lib/options.js +++ b/lib/options.js @@ -3,8 +3,6 @@ * @author Kai Cataldo */ -"use strict"; - //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ @@ -67,7 +65,7 @@ function normalizeSourceType(sourceType = "script") { * @throws {Error} throw an error if found invalid option. * @returns {Object} normalized options */ -function normalizeOptions(options) { +export function normalizeOptions(options) { const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion); const sourceType = normalizeSourceType(options.sourceType); const ranges = options.range === true; @@ -83,7 +81,7 @@ function normalizeOptions(options) { * Get the latest ECMAScript version supported by Espree. * @returns {number} The latest ECMAScript version. */ -function getLatestEcmaVersion() { +export function getLatestEcmaVersion() { return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1]; } @@ -91,16 +89,6 @@ function getLatestEcmaVersion() { * Get the list of ECMAScript versions supported by Espree. * @returns {number[]} An array containing the supported ECMAScript versions. */ -function getSupportedEcmaVersions() { +export function getSupportedEcmaVersions() { return [...SUPPORTED_VERSIONS]; } - -//------------------------------------------------------------------------------ -// Public -//------------------------------------------------------------------------------ - -module.exports = { - normalizeOptions, - getLatestEcmaVersion, - getSupportedEcmaVersions -}; diff --git a/lib/token-translator.js b/lib/token-translator.js index f06c7c07..1e03ae66 100644 --- a/lib/token-translator.js +++ b/lib/token-translator.js @@ -4,8 +4,6 @@ */ /* eslint no-underscore-dangle: 0 */ -"use strict"; - //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ @@ -260,4 +258,4 @@ TokenTranslator.prototype = { // Public //------------------------------------------------------------------------------ -module.exports = TokenTranslator; +export default TokenTranslator; diff --git a/lib/version.js b/lib/version.js new file mode 100644 index 00000000..73859fe1 --- /dev/null +++ b/lib/version.js @@ -0,0 +1 @@ +export default version = '7.3.0' \ No newline at end of file diff --git a/lib/visitor-keys.js b/lib/visitor-keys.js index 4216864f..15ef85e8 100644 --- a/lib/visitor-keys.js +++ b/lib/visitor-keys.js @@ -25,7 +25,6 @@ * SOFTWARE. */ -"use strict"; //------------------------------------------------------------------------------ // Requirements @@ -37,7 +36,7 @@ // Public //------------------------------------------------------------------------------ -module.exports = { +export default { // ECMAScript AssignmentExpression: ["left", "right"], diff --git a/package.json b/package.json index 215deab0..3f0b7369 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,12 @@ "description": "An Esprima-compatible JavaScript parser built on Acorn", "author": "Nicholas C. Zakas ", "homepage": "https://github.com/eslint/espree", - "main": "espree.js", + "main": "dist/espree.cjs", + "module": "espree.js", + "exports": { + "import": "espree.js", + "require": "./dist/espree.cjs" + }, "version": "7.3.0", "files": [ "lib", @@ -23,6 +28,9 @@ "eslint-visitor-keys": "^1.3.0" }, "devDependencies": { + "@rollup/plugin-commonjs": "^16.0.0", + "@rollup/plugin-json": "^4.1.0", + "@rollup/plugin-node-resolve": "^10.0.0", "browserify": "^16.5.0", "chai": "^4.2.0", "eslint": "^6.0.1", @@ -36,6 +44,7 @@ "mocha": "^6.2.0", "nyc": "^14.1.1", "regenerate": "^1.4.0", + "rollup": "^2.33.3", "shelljs": "^0.3.0", "shelljs-nodecli": "^0.1.1", "unicode-6.3.0": "^0.7.5" @@ -54,6 +63,7 @@ "lint": "node Makefile.js lint", "fixlint": "node Makefile.js lint --fix", "sync-docs": "node Makefile.js docs", + "rollup": "rollup -c rollup.config.js", "browserify": "node Makefile.js browserify", "generate-release": "eslint-generate-release", "generate-alpharelease": "eslint-generate-prerelease alpha", diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 00000000..1f5f01e0 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,31 @@ +import commonjs from '@rollup/plugin-commonjs'; +import resolve from '@rollup/plugin-node-resolve'; +import json from '@rollup/plugin-json'; +import fs from 'fs'; + + +const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8')); +fs.writeFileSync('lib/version.js', `export default version = '${pkg.version}'`); + +export default [ + { + input: "espree.js", + output: { + file: "dist/espree.js", + format: "umd", + name: "espree", + sourcemap: true + }, + plugins: [ commonjs(), resolve(), json() ] + }, + { + input: "espree.js", + external: ["acorn", "acorn-jsx", "eslint-visitor-keys"], + output: { + file: "dist/espree.cjs", + format: "cjs", + sourcemap: true + }, + plugins: [ ] + } +]