Skip to content

Commit 6b5cf43

Browse files
committed
Added eslint
- Fixed existing errors - Minor refactoring
1 parent 9fd6098 commit 6b5cf43

16 files changed

Lines changed: 374 additions & 329 deletions

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.nyc_output
2+
coverage
3+
node_modules

.eslintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
root: true
3+
env:
4+
node: true
5+
es6: true
6+
extends: mitmaro
7+
rules:
8+
no-constant-condition:
9+
- error
10+
- checkLoops: false
11+
newline-after-var: off
12+
no-extra-parens:
13+
- error
14+
- all
15+
- nestedBinaryExpressions: false

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"ava": "^0.15.2",
1717
"chalk": "^1.1.3",
1818
"diff": "^2.2.3",
19+
"eslint": "^3.0.0",
20+
"eslint-config-mitmaro": "^1.0.0",
21+
"eslint-plugin-strict-newline": "^1.0.0",
1922
"nyc": "^6.4.4"
2023
},
2124
"nyc": {

src/Lexer.js

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint max-depth: "off" */
12
'use strict';
23

34
const LexerError = require('./error/Lexer');
@@ -30,17 +31,17 @@ const {
3031
TOKEN_STATEMENT_FI,
3132
TOKEN_STATEMENT_INCLUDE,
3233

33-
TOKEN_OPERATOR_EQUALS,
34-
TOKEN_OPERATOR_NOT_EQUALS,
35-
TOKEN_OPERATOR_STRICT_EQUALS,
36-
TOKEN_OPERATOR_STRICT_NOT_EQUALS,
37-
TOKEN_OPERATOR_AND,
38-
TOKEN_OPERATOR_OR,
39-
TOKEN_OPERATOR_NOT,
40-
TOKEN_OPERATOR_GREATER_THAN,
41-
TOKEN_OPERATOR_LESS_THAN,
42-
TOKEN_OPERATOR_GREATER_EQUAL_THAN,
43-
TOKEN_OPERATOR_LESS_EQUAL_THAN
34+
OPERATOR_EQUALS,
35+
OPERATOR_NOT_EQUALS,
36+
OPERATOR_STRICT_EQUALS,
37+
OPERATOR_STRICT_NOT_EQUALS,
38+
OPERATOR_AND,
39+
OPERATOR_OR,
40+
OPERATOR_NOT,
41+
OPERATOR_GREATER_THAN,
42+
OPERATOR_LESS_THAN,
43+
OPERATOR_GREATER_EQUAL_THAN,
44+
OPERATOR_LESS_EQUAL_THAN
4445
} = require('./constants');
4546

4647
const boundaryTypeLookup = {
@@ -53,29 +54,27 @@ const boundaryTypeLookup = {
5354
};
5455

5556
const statementTypeLookup = {
56-
'if': TOKEN_STATEMENT_IF,
57-
'elif': TOKEN_STATEMENT_ELIF,
58-
'else': TOKEN_STATEMENT_ELSE,
59-
'fi': TOKEN_STATEMENT_FI,
60-
'include': TOKEN_STATEMENT_INCLUDE
57+
if: TOKEN_STATEMENT_IF,
58+
elif: TOKEN_STATEMENT_ELIF,
59+
else: TOKEN_STATEMENT_ELSE,
60+
fi: TOKEN_STATEMENT_FI,
61+
include: TOKEN_STATEMENT_INCLUDE
6162
};
6263

6364
const binaryOperatorTypeLookup = {
64-
'==': TOKEN_OPERATOR_EQUALS,
65-
'!=': TOKEN_OPERATOR_NOT_EQUALS,
66-
'===': TOKEN_OPERATOR_STRICT_EQUALS,
67-
'!==': TOKEN_OPERATOR_STRICT_NOT_EQUALS,
68-
'&&': TOKEN_OPERATOR_AND,
69-
'||': TOKEN_OPERATOR_OR,
70-
'>': TOKEN_OPERATOR_GREATER_THAN,
71-
'<': TOKEN_OPERATOR_LESS_THAN,
72-
'>=': TOKEN_OPERATOR_GREATER_EQUAL_THAN,
73-
'<=': TOKEN_OPERATOR_LESS_EQUAL_THAN
65+
'==': OPERATOR_EQUALS,
66+
'!=': OPERATOR_NOT_EQUALS,
67+
'===': OPERATOR_STRICT_EQUALS,
68+
'!==': OPERATOR_STRICT_NOT_EQUALS,
69+
'&&': OPERATOR_AND,
70+
'||': OPERATOR_OR,
71+
'>': OPERATOR_GREATER_THAN,
72+
'<': OPERATOR_LESS_THAN,
73+
'>=': OPERATOR_GREATER_EQUAL_THAN,
74+
'<=': OPERATOR_LESS_EQUAL_THAN
7475
};
7576

76-
const unaryOperatorTypeLookup = {
77-
'!': TOKEN_OPERATOR_NOT
78-
};
77+
const unaryOperatorTypeLookup = { '!': OPERATOR_NOT };
7978

8079
const STATE_TAG = 'TAG';
8180
const STATE_TEXT_LITERAL = 'TEXT';
@@ -86,7 +85,7 @@ const generalDelimiters = [
8685
' ', '\t', '\n'
8786
];
8887

89-
const delimiters = [null, [], [], []];
88+
const delimiters = [null, [], [], [] ];
9089

9190
// build delimiters
9291
Array.prototype.concat(
@@ -95,7 +94,7 @@ Array.prototype.concat(
9594
Object.keys(binaryOperatorTypeLookup),
9695
Object.keys(unaryOperatorTypeLookup)
9796
).forEach((delimiter) => {
98-
delimiters[delimiter.length].push(delimiter)
97+
delimiters[delimiter.length].push(delimiter);
9998
});
10099

101100
class Lexer {
@@ -104,21 +103,21 @@ class Lexer {
104103
this.pointer = 0;
105104
this.input = input;
106105
// length used for cases of checking ahead by one character from pointer
107-
this._lookAheadLength = this.input.length -1;
108-
this._stringDelimiter = null;
106+
this.lookAheadLength = this.input.length - 1;
107+
this.stringDelimiter = null;
109108
}
110109

111110
// skip all whitespace or until EOF reached
112-
_skipWhitespace() {
111+
skipWhitespace() {
113112
while (this.pointer < this.input.length && this.input[this.pointer].trim().length === 0) {
114113
this.pointer++;
115114
}
116115
}
117116

118117
// scan until {{ is found or EOF reached
119-
_scanTextLiteral() {
118+
scanTextLiteral() {
120119
while (
121-
this.pointer < this._lookAheadLength
120+
this.pointer < this.lookAheadLength
122121
&& !(
123122
this.input[this.pointer] === '{'
124123
&& this.input[this.pointer + 1] === '{'
@@ -129,31 +128,28 @@ class Lexer {
129128

130129
// because we look two characters ahead, we need to increment the
131130
// pointer by one if end of input is reached
132-
if (this._lookAheadLength >= 0 && this.pointer >= this._lookAheadLength) {
131+
if (this.lookAheadLength >= 0 && this.pointer >= this.lookAheadLength) {
133132
this.pointer++;
134133
}
135134
}
136135

137-
//
138-
_scanString() {
136+
scanString() {
139137
// if next character is the delimiter we have a zero length string
140-
if (this.input[this.pointer] === this._stringDelimiter) {
138+
if (this.input[this.pointer] === this.stringDelimiter) {
141139
return;
142140
}
143141

144-
while (this.pointer < this._lookAheadLength) {
145-
142+
while (this.pointer < this.lookAheadLength) {
146143
// check ahead for delimiter but only if current isn't an escape
147-
if (this.input[this.pointer + 1] === this._stringDelimiter && this.input[this.pointer] !== '\\') {
144+
if (this.input[this.pointer + 1] === this.stringDelimiter && this.input[this.pointer] !== '\\') {
148145
break;
149146
}
150-
151147
this.pointer++;
152148
}
153149
this.pointer++;
154150
}
155151

156-
_isAtDelimiter() {
152+
isAtDelimiter() {
157153
for (let length = 3; length > 0; length--) {
158154
// if we don't have enough input remaining then stop
159155
if (length + this.pointer > this.input.length) {
@@ -176,32 +172,32 @@ class Lexer {
176172

177173

178174
// scan up to the next delimiter
179-
_scanToNextDelimiter() {
175+
scanToNextDelimiter() {
180176
// if already at delimiter
181-
if (this._isAtDelimiter()) {
177+
if (this.isAtDelimiter()) {
182178
return;
183179
}
184180
this.pointer++;
185181

186182
while (this.pointer < this.input.length) {
187-
if (this._isAtDelimiter()) {
183+
if (this.isAtDelimiter()) {
188184
return;
189185
}
190186
this.pointer++;
191187
}
192188
}
193189

194190
// scan pass the next delimiter
195-
_scanNextDelimiter() {
196-
const delimiter = this._isAtDelimiter() || '';
197-
this.pointer += delimiter.length;
191+
scanNextDelimiter() {
192+
const delimiter = this.isAtDelimiter() || '';
193+
this.pointer = this.pointer + delimiter.length;
198194
}
199195

200-
* tokens() {
196+
*tokens() {
201197
while (this.pointer < this.input.length) {
202198
if (this.state === STATE_TEXT_LITERAL) {
203-
let startIndex = this.pointer;
204-
this._scanTextLiteral();
199+
const startIndex = this.pointer;
200+
this.scanTextLiteral();
205201
this.state = STATE_TAG;
206202
const value = this.input.substring(startIndex, this.pointer);
207203
if (!value.length) {
@@ -217,33 +213,33 @@ class Lexer {
217213
}
218214
else if (this.state === STATE_TAG || this.state === STATE_END_STRING) {
219215
let type;
220-
this._skipWhitespace();
216+
this.skipWhitespace();
221217
let startIndex = this.pointer;
222218

223219
// scan until next delimiter
224-
this._scanToNextDelimiter();
220+
this.scanToNextDelimiter();
225221
let value = this.input.substring(startIndex, this.pointer);
226222
if (value.length) {
227223
type = statementTypeLookup[value];
228224
if (type) {
229225
yield {
230226
type: TOKEN_TYPE_STATEMENT,
231227
subType: type,
232-
value: value,
228+
value,
233229
startIndex,
234230
endIndex: this.pointer
235231
};
236232
continue;
237233
}
238234

239235
// if first digit is a number, it's a numerical value
240-
if ((value[0] >= '0' && value[0] <= '9') || value[0] === '-') {
236+
if ((value[0] >= '0' && value[0] <= '9') || value[0] === '-') {
241237
// floats have a decimal number
242-
if (value.indexOf('.') !== -1) {
243-
type = TOKEN_VALUE_FLOAT;
238+
if (value.indexOf('.') === -1) {
239+
type = TOKEN_VALUE_INTEGER;
244240
}
245241
else {
246-
type = TOKEN_VALUE_INTEGER;
242+
type = TOKEN_VALUE_FLOAT;
247243
}
248244
}
249245
else {
@@ -253,21 +249,21 @@ class Lexer {
253249
yield {
254250
type: TOKEN_TYPE_VALUE,
255251
subType: type,
256-
value: value,
252+
value,
257253
startIndex,
258254
endIndex: this.pointer
259255
};
260256
continue;
261257
}
262258

263259
startIndex = this.pointer;
264-
this._scanNextDelimiter();
260+
this.scanNextDelimiter();
265261
value = this.input.substring(startIndex, this.pointer);
266262
// boundary types are the most complicated
267263
type = boundaryTypeLookup[value];
268264
if (type) {
269265
if (type === TOKEN_BOUNDARY_STRING_DOUBLE || type === TOKEN_BOUNDARY_STRING_SINGLE) {
270-
this._stringDelimiter = value;
266+
this.stringDelimiter = value;
271267
this.state = this.state === STATE_END_STRING ? STATE_TAG : STATE_STRING;
272268
}
273269
else if (type === TOKEN_BOUNDARY_TAG_END) {
@@ -276,7 +272,7 @@ class Lexer {
276272
yield {
277273
type: TOKEN_TYPE_BOUNDARY,
278274
subType: type,
279-
value: value,
275+
value,
280276
startIndex,
281277
endIndex: this.pointer
282278
};
@@ -287,7 +283,7 @@ class Lexer {
287283
yield {
288284
type: TOKEN_TYPE_BINARY_OPERATOR,
289285
subType: type,
290-
value: value,
286+
value,
291287
startIndex,
292288
endIndex: this.pointer
293289
};
@@ -297,22 +293,22 @@ class Lexer {
297293
yield {
298294
type: TOKEN_TYPE_UNARY_OPERATOR,
299295
subType: type,
300-
value: value,
296+
value,
301297
startIndex,
302298
endIndex: this.pointer
303299
};
304300
}
305301
else if (this.state === STATE_STRING) {
306-
let startIndex = this.pointer;
307-
this._scanString();
302+
const startIndex = this.pointer;
303+
this.scanString();
308304
this.state = STATE_END_STRING;
309305
yield {
310306
type: TOKEN_TYPE_VALUE,
311307
subType: TOKEN_VALUE_STRING,
312308
value: this.input.substring(startIndex, this.pointer),
313309
startIndex,
314310
endIndex: this.pointer
315-
}
311+
};
316312
}
317313
else {
318314
throw new LexerError('Invalid state incurred');

src/constants.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ module.exports = {
2525
TOKEN_BOUNDARY_STRING_SINGLE: 'STRING_SINGLE',
2626
TOKEN_BOUNDARY_STRING_DOUBLE: 'STRING_DOUBLE',
2727

28-
TOKEN_OPERATOR_EQUALS: 'EQUALS',
29-
TOKEN_OPERATOR_NOT_EQUALS: 'NOT_EQUALS',
30-
TOKEN_OPERATOR_STRICT_EQUALS: 'STRICT_EQUALS',
31-
TOKEN_OPERATOR_STRICT_NOT_EQUALS: 'STRICT_NOT_EQUALS',
32-
TOKEN_OPERATOR_AND: 'AND',
33-
TOKEN_OPERATOR_OR: 'OR',
34-
TOKEN_OPERATOR_NOT: 'NOT',
35-
TOKEN_OPERATOR_GREATER_THAN: 'GREATER_THAN',
36-
TOKEN_OPERATOR_LESS_THAN: 'LESS_THAN',
37-
TOKEN_OPERATOR_GREATER_EQUAL_THAN: 'GREATER_EQUAL_THAN',
38-
TOKEN_OPERATOR_LESS_EQUAL_THAN: 'LESS_EQUAL_THAN',
28+
OPERATOR_EQUALS: 'EQUALS',
29+
OPERATOR_NOT_EQUALS: 'NOT_EQUALS',
30+
OPERATOR_STRICT_EQUALS: 'STRICT_EQUALS',
31+
OPERATOR_STRICT_NOT_EQUALS: 'STRICT_NOT_EQUALS',
32+
OPERATOR_AND: 'AND',
33+
OPERATOR_OR: 'OR',
34+
OPERATOR_NOT: 'NOT',
35+
OPERATOR_GREATER_THAN: 'GREATER_THAN',
36+
OPERATOR_LESS_THAN: 'LESS_THAN',
37+
OPERATOR_GREATER_EQUAL_THAN: 'GREATER_EQUAL_THAN',
38+
OPERATOR_LESS_EQUAL_THAN: 'LESS_EQUAL_THAN',
3939

4040
TOKEN_VALUE_VARIABLE: 'VARIABLE',
4141
TOKEN_VALUE_STRING: 'STRING',

src/error/Lexer.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
const GeneralError = require('./General');
44

55
class LexerError extends GeneralError {
6-
constructor(message) {
7-
super(message);
8-
}
96
}
107

118
module.exports = LexerError;

0 commit comments

Comments
 (0)