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

ESM #450

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

ESM #450

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
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.json]
indent_size = 4

[*.yml]
indent_size = 2
9 changes: 9 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules
lib

!.eslintrc.js
coverage

test/*/*.js

dist
69 changes: 69 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

module.exports = {
extends: 'eslint:recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
env: {
es6: true
},
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020
},
overrides: [{
files: ['*-node.js', '.eslintrc.cjs', 'benchmark/**', 'bin/**', 'tools/**'],
env: {
node: true
}
}, {
files: '.eslintrc.js',
parserOptions: {
sourceType: 'script'
},
rules: {
strict: 'error'
}
}, {
files: 'test/**',
globals: {
expect: true
},
env: {
mocha: true
}
}],
rules: {
// 'push-with-multiple-arguments': 2,
/*
'no-unused-vars': [
2,
{
vars: 'all',
args: 'none'
}
],
*/
'no-unused-vars': 0,
'no-prototype-builtins': 0,
'new-cap': [
2,
{
capIsNew: false
}
],
semi: ['error'],
// indent: ['error', 4, { SwitchCase: 1 }],
'prefer-const': ['error'],
// 'no-var': ['error'],
// 'prefer-destructuring': ['error'],
// 'object-shorthand': ['error'],
// 'object-curly-spacing': ['error', 'always'],
// quotes: ['error', 'single'],
// 'quote-props': ['error', 'as-needed'],
'brace-style': ['error', '1tbs', { allowSingleLine: true }]
// 'prefer-template': ['error']
}
};
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ node_modules/
# Cover
.coverage_data/
cover_html/
coverage

.idea

npm-debug.log

dist
15 changes: 0 additions & 15 deletions .npmignore

This file was deleted.

10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
sudo: false
language: node_js
script:
- npm run build
- npm test
node_js:
- "6"
- "8"
- "10"
- 12
- 14
- 16
- 17
35 changes: 22 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@ code generator from [Mozilla's Parser API](https://developer.mozilla.org/en/Spid
AST. See the [online generator](https://estools.github.io/escodegen/demo/index.html)
for a demo.


### Install

Escodegen can be used in a web browser:

<script src="escodegen.browser.js"></script>
```html
<script src="dist/escodegen.umd.js"></script>
```

escodegen.browser.js can be found in tagged revisions on GitHub.
`dist/escodegen.umd.js` can be found in tagged revisions on GitHub.

Or in a Node.js application via npm:

npm install escodegen
```sh
npm install escodegen
```

### Usage

A simple example: the program

escodegen.generate({
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 40 },
right: { type: 'Literal', value: 2 }
});
```js
escodegen.generate({
type: 'BinaryExpression',
operator: '+',
left: { type: 'Literal', value: 40 },
right: { type: 'Literal', value: 2 }
});
```

produces the string `'40 + 2'`.

Expand All @@ -45,13 +50,17 @@ options. To run the tests, execute `npm test` in the root directory.
At first, execute `npm install` to install the all dev dependencies.
After that,

npm run-script build
```sh
npm run-script build
```

will generate `escodegen.browser.js`, which can be used in browser environments.
will generate `dist/escodegen.umd.js`, which can be used in browser environments.

And,

npm run-script build-min
```sh
npm run-script build-min
```

will generate the minified file `escodegen.browser.min.js`.

Expand Down
16 changes: 10 additions & 6 deletions benchmark/asts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var fs = require('fs'),
path = require('path'),
esprima = require('esprima');
import fs from 'fs';
import path from 'path';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

var FILES_PATH = path.join(__dirname, './asts');
// import esprima from 'esprima';

const __dirname = dirname(fileURLToPath(import.meta.url));
const FILES_PATH = path.join(__dirname, './asts');

var FILES = [
'jQuery 1.7.1',
Expand All @@ -22,8 +26,8 @@ function slug(name) {
return name.toLowerCase().replace(/\s/g, '-');
}

module.exports = FILES.map(function (file) {
var astJson = fs.readFileSync(FILES_PATH + '/' + slug(file) + '-ast.json');
export default FILES.map(function (file) {
const astJson = fs.readFileSync(`${FILES_PATH}/${slug(file)}-ast.json`);

return JSON.parse(astJson);
});
15 changes: 7 additions & 8 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var Benchmark = require('benchmark'),
escodegen = require('../'),
old = require('./old.js'),
esotope = require('esotope'),
asts = require('./asts');

import Benchmark from 'benchmark';
import esotope from 'esotope';
import * as escodegen from '../src/escodegen-node.js';
import old from './old.cjs';
import asts from './asts.js';

function cycle(codegen) {
for (var i = 0; i < asts.length; i++)
Expand All @@ -24,15 +23,15 @@ new Benchmark.Suite()
})

.on('start', function () {
console.log('Benchmarking...')
console.log('Benchmarking...');
})

.on('cycle', function (event) {
console.log(event.target.toString());
})

.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log(`Fastest is ${this.filter('fastest').map('name')}`);

console.log('esotope is x' + (this[0].hz / this[1].hz).toFixed(2) + ' times faster vs escodegen.');
})
Expand Down
1 change: 0 additions & 1 deletion benchmark/old.js → benchmark/old.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*global exports:true, require:true, global:true*/
(function () {
'use strict';

Expand Down
Loading