Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit b4532e3

Browse files
committed
JsFile: remove all duplicate tokens in any order.
1 parent 3bd9e7c commit b4532e3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/js-file.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ JsFile.prototype = {
143143
// in duplicate tokens on `export default function() {}`
144144
// (https://code.google.com/p/esprima/issues/detail?id=631)
145145
if (this.getDialect() === 'es6') {
146-
var lastToken;
146+
var tokenHash = {};
147147
this._tree.tokens = this._tree.tokens.filter(function(token, i) {
148-
var prevToken = lastToken;
149-
lastToken = token;
148+
var hashKey = token.range[0] + '_' + token.range[1];
149+
var isDuplicate = tokenHash[hashKey];
150150

151-
return !prevToken || prevToken.range[0] !== token.range[0] || prevToken.range[1] !== token.range[1];
151+
tokenHash[hashKey] = true;
152+
153+
return !isDuplicate;
152154
});
153155
}
154156

test/js-file.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ describe('modules/js-file', function() {
205205
createHarmonyJsFile('export default function() {}').iterateTokenByValue('(', spy);
206206
assert(spy.calledOnce);
207207
});
208+
209+
it('should not have duplicate tokens in es6 export default statements', function() {
210+
var spy = sinon.spy();
211+
createHarmonyJsFile('export default function init() {\n' +
212+
' window.addEventListener(\'fb-flo-reload\', function(ev) {\n' +
213+
' });\n' +
214+
'}').iterateTokenByValue('(', spy);
215+
assert(spy.calledThrice);
216+
});
208217
});
209218

210219
describe('getNodeByRange', function() {

0 commit comments

Comments
 (0)