Skip to content

Commit dbf8fe2

Browse files
committed
Merge pull request estools#253 from adamjmcgrath/esprima-2.x
Esprima 2.x import/export statements
2 parents 12a4319 + f2879cd commit dbf8fe2

19 files changed

Lines changed: 5810 additions & 13 deletions

escodegen.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,15 @@
12651265
return result;
12661266
},
12671267

1268+
ExportDefaultDeclaration: function (stmt, flags) {
1269+
stmt.default = true;
1270+
return this.ExportDeclaration(stmt, flags);
1271+
},
1272+
1273+
ExportNamedDeclaration: function (stmt, flags) {
1274+
return this.ExportDeclaration(stmt, flags);
1275+
},
1276+
12681277
ExpressionStatement: function (stmt, flags) {
12691278
var result, fragment;
12701279

@@ -2236,13 +2245,14 @@
22362245
},
22372246

22382247
ImportDefaultSpecifier: function (expr, precedence, flags) {
2239-
return generateIdentifier(expr.id);
2248+
return generateIdentifier(expr.id || expr.local);
22402249
},
22412250

22422251
ImportNamespaceSpecifier: function (expr, precedence, flags) {
22432252
var result = ['*'];
2244-
if (expr.id) {
2245-
result.push(space + 'as' + noEmptySpace() + generateIdentifier(expr.id));
2253+
var id = expr.id || expr.local;
2254+
if (id) {
2255+
result.push(space + 'as' + noEmptySpace() + generateIdentifier(id));
22462256
}
22472257
return result;
22482258
},
@@ -2252,7 +2262,7 @@
22522262
},
22532263

22542264
ExportSpecifier: function (expr, precedence, flags) {
2255-
var result = [ expr.id.name ];
2265+
var result = [ (expr.id || expr.local).name ];
22562266
if (expr.name) {
22572267
result.push(noEmptySpace() + 'as' + noEmptySpace() + generateIdentifier(expr.name));
22582268
}

test/3rdparty/esprima-2.5.0.js

Lines changed: 5658 additions & 0 deletions
Large diffs are not rendered by default.

test/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
'use strict';
2626

27-
var esprima = require('./3rdparty/esprima'),
27+
var esprima = require('./3rdparty/esprima-1.0.0-dev'),
2828
escodegen = require('./loader'),
2929
chai = require('chai'),
3030
expect = chai.expect,

test/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
'use strict';
2626

2727
var data,
28-
esprima = require('./3rdparty/esprima'),
28+
esprima = require('./3rdparty/esprima-1.0.0-dev'),
2929
escodegen = require('./loader'),
3030
chai = require('chai'),
3131
expect = chai.expect;

test/compare-esprima2.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
'use strict';
26+
27+
var fs = require('fs'),
28+
esprima = require('./3rdparty/esprima-2.5.0'),
29+
escodegen = require('./loader'),
30+
chai = require('chai'),
31+
expect = chai.expect,
32+
DIR = 'compare-esprima2';
33+
34+
function test(code, expected) {
35+
var tree, actual, options, StringObject;
36+
37+
// alias, so that JSLint does not complain.
38+
StringObject = String;
39+
40+
options = {
41+
range: true,
42+
loc: false,
43+
tokens: true,
44+
raw: false,
45+
comment: true,
46+
sourceType: 'module'
47+
};
48+
49+
tree = esprima.parse(code, options);
50+
51+
// for UNIX text comment
52+
actual = escodegen.generate(tree).replace(/[\n\r]$/, '') + '\n';
53+
expect(actual).to.be.equal(expected);
54+
}
55+
56+
function testMin(code, expected) {
57+
var tree, tree2, actual, actual2, options, StringObject;
58+
59+
// alias, so that JSLint does not complain.
60+
StringObject = String;
61+
62+
options = {
63+
range: true,
64+
loc: false,
65+
tokens: true,
66+
raw: false,
67+
sourceType: 'module'
68+
};
69+
70+
tree = esprima.parse(code, options);
71+
72+
// for UNIX text comment
73+
actual = escodegen.generate(tree, {
74+
format: escodegen.FORMAT_MINIFY,
75+
raw: false
76+
}).replace(/[\n\r]$/, '') + '\n';
77+
expect(actual).to.be.equal(expected);
78+
79+
// And ensure that minified value is exactly equal.
80+
tree2 = esprima.parse(actual, options);
81+
actual2 = escodegen.generate(tree2, {
82+
format: escodegen.FORMAT_MINIFY,
83+
raw: false
84+
}).replace(/[\n\r]$/, '') + '\n';
85+
expect(actual2).to.be.equal(actual);
86+
}
87+
88+
describe('compare esprima 2 test', function () {
89+
fs.readdirSync(__dirname + '/' + DIR).sort().forEach(function(file) {
90+
var code, expected, exp, min;
91+
if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) {
92+
it(file, function () {
93+
exp = file.replace(/\.js$/, '.expected.js');
94+
min = file.replace(/\.js$/, '.expected.min.js');
95+
code = fs.readFileSync(__dirname + '/' + DIR + '/' + file, 'utf-8');
96+
expected = fs.readFileSync(__dirname + '/' + DIR + '/' + exp, 'utf-8');
97+
test(code, expected);
98+
if (fs.existsSync(__dirname + '/' + DIR + '/' + min)) {
99+
expected = fs.readFileSync(__dirname + '/' + DIR + '/' + min, 'utf-8');
100+
testMin(code, expected);
101+
}
102+
});
103+
}
104+
});
105+
});
106+
/* vim: set sw=4 ts=4 et tw=80 : */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default function a() {
2+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function a(){}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function a () { }
2+
// export default var i = 20;
3+
// export default const K = 20;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import foo from 'foo';
2+
import foo, * as foo from 'foo';
3+
import * as foo from 'foo';
4+
import ok, {
5+
foo,
6+
test,
7+
logging
8+
} from 'foo';

0 commit comments

Comments
 (0)