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

Build: es module proof of concept DO NOT MERGE :) fixes #457 #458

Closed
wants to merge 3 commits into from
Closed
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
34 changes: 17 additions & 17 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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 = {};

Expand All @@ -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();
4 changes: 1 addition & 3 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* @author Nicholas C. Zakas
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
Expand All @@ -15,7 +13,7 @@
// Public
//------------------------------------------------------------------------------

module.exports = {
export default {
AssignmentExpression: "AssignmentExpression",
AssignmentPattern: "AssignmentPattern",
ArrayExpression: "ArrayExpression",
Expand Down
7 changes: 3 additions & 4 deletions lib/espree.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @author Nicholas C. Zakas
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
Expand All @@ -16,7 +14,7 @@
// Public
//------------------------------------------------------------------------------

module.exports = {
export default {

// React JSX parsing
jsx: false,
Expand Down
18 changes: 3 additions & 15 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* @author Kai Cataldo
*/

"use strict";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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;
Expand All @@ -83,24 +81,14 @@ 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];
}

/**
* 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
};
4 changes: 1 addition & 3 deletions lib/token-translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
/* eslint no-underscore-dangle: 0 */

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -260,4 +258,4 @@ TokenTranslator.prototype = {
// Public
//------------------------------------------------------------------------------

module.exports = TokenTranslator;
export default TokenTranslator;
1 change: 1 addition & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default version = '7.3.0'
3 changes: 1 addition & 2 deletions lib/visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* SOFTWARE.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
Expand All @@ -37,7 +36,7 @@
// Public
//------------------------------------------------------------------------------

module.exports = {
export default {

// ECMAScript
AssignmentExpression: ["left", "right"],
Expand Down
31 changes: 31 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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: [ ]
}
]
4 changes: 2 additions & 2 deletions tests/lib/acorn-after-espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down