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

Commit a5f0a1a

Browse files
markeloghzoo
authored andcommitted
Fix: js-file - make parser not confuse token types
Fixes #1855 Closes gh-1864
1 parent 997e0a9 commit a5f0a1a

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

lib/js-file.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ JsFile.prototype = {
929929
},
930930

931931
/**
932-
* Temporary fix (I hope, two years and counting :-) for esprima tokenizer
932+
* Temporary fix (I hope, two years and counting :-) for esprima/babylon tokenizer
933933
* (https://github.com/jquery/esprima/issues/317)
934934
* Fixes #83, #180
935935
*
@@ -938,11 +938,14 @@ JsFile.prototype = {
938938
_fixEsprimaIdentifiers: function() {
939939
var _this = this;
940940

941-
this.iterateNodesByType(['Property', 'MemberExpression'], function(node) {
941+
this.iterateNodesByType(['Property', 'MethodDefinition', 'MemberExpression'], function(node) {
942942
switch (node.type) {
943943
case 'Property':
944944
convertKeywordToIdentifierIfRequired(node.key);
945945
break;
946+
case 'MethodDefinition':
947+
convertKeywordToIdentifierIfRequired(node.key);
948+
break;
946949
case 'MemberExpression':
947950
convertKeywordToIdentifierIfRequired(node.property);
948951
break;
@@ -951,6 +954,7 @@ JsFile.prototype = {
951954

952955
function convertKeywordToIdentifierIfRequired(node) {
953956
var token = _this.getTokenByRangeStart(node.range[0]);
957+
954958
if (token.type === 'Keyword') {
955959
token.type = 'Identifier';
956960
}

test/specs/js-file.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
var fs = require('fs');
2+
13
var expect = require('chai').expect;
4+
var sinon = require('sinon');
5+
26
var esprima = require('esprima');
37
var babelJscs = require('babel-jscs');
4-
var JsFile = require('../../lib/js-file');
5-
var sinon = require('sinon');
6-
var fs = require('fs');
8+
79
var assign = require('lodash').assign;
10+
var keywords = Object.keys(require('reserved-words').KEYWORDS[6]);
11+
12+
var JsFile = require('../../lib/js-file');
813

914
describe('js-file', function() {
1015

@@ -62,6 +67,24 @@ describe('js-file', function() {
6267
});
6368
});
6469

70+
it('should affect method definition tokens', function() {
71+
var str = keywords.map(function(token) {
72+
return token + '() {}';
73+
});
74+
75+
str = 'class test {\n' + str.join('\n') + '\n}';
76+
77+
createJsFile(str).getTokens().forEach(function(token, i) {
78+
79+
// Exclude first "class"
80+
if (i === 0) {
81+
return;
82+
}
83+
84+
expect(token.type).to.not.equal('Keyword');
85+
});
86+
});
87+
6588
it('should affect member access tokens', function() {
6689
var str = 'o.break(); o.export(); o.return(); o.case(); o.for(); o.switch(); o.comment();' +
6790
'o.function(); o.this(); o.continue(); o.if(); o.typeof(); o.default(); o.import();' +

0 commit comments

Comments
 (0)