Skip to content

Commit 7976cdb

Browse files
committed
ESLint passing
1 parent bae0548 commit 7976cdb

17 files changed

Lines changed: 162 additions & 86 deletions

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ rules:
88
no-constant-condition:
99
- error
1010
- checkLoops: false
11-
newline-after-var: off
1211
no-extra-parens:
1312
- error
1413
- all
1514
- nestedBinaryExpressions: false
15+
no-undefined: off

src/Lexer.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ class Lexer {
170170
return false;
171171
}
172172

173-
174173
// scan up to the next delimiter
175174
scanToNextDelimiter() {
176175
// if already at delimiter
@@ -190,16 +189,20 @@ class Lexer {
190189
// scan pass the next delimiter
191190
scanNextDelimiter() {
192191
const delimiter = this.isAtDelimiter() || '';
192+
193193
this.pointer = this.pointer + delimiter.length;
194194
}
195195

196196
*tokens() {
197197
while (this.pointer < this.input.length) {
198198
if (this.state === STATE_TEXT_LITERAL) {
199199
const startIndex = this.pointer;
200+
200201
this.scanTextLiteral();
201202
this.state = STATE_TAG;
203+
202204
const value = this.input.substring(startIndex, this.pointer);
205+
203206
if (!value.length) {
204207
continue;
205208
}
@@ -213,12 +216,15 @@ class Lexer {
213216
}
214217
else if (this.state === STATE_TAG || this.state === STATE_END_STRING) {
215218
let type;
219+
216220
this.skipWhitespace();
221+
217222
let startIndex = this.pointer;
218223

219224
// scan until next delimiter
220225
this.scanToNextDelimiter();
221226
let value = this.input.substring(startIndex, this.pointer);
227+
222228
if (value.length) {
223229
type = statementTypeLookup[value];
224230
if (type) {
@@ -300,6 +306,7 @@ class Lexer {
300306
}
301307
else if (this.state === STATE_STRING) {
302308
const startIndex = this.pointer;
309+
303310
this.scanString();
304311
this.state = STATE_END_STRING;
305312
yield {

src/Parser.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const ParserError = require('./error/Parser');
44

55
const {
66
TOKEN_TYPE_VALUE,
7-
TOKEN_TYPE_STATEMENT,
87
TOKEN_TYPE_UNARY_OPERATOR,
98
TOKEN_TYPE_BINARY_OPERATOR,
109

@@ -117,6 +116,7 @@ class Parser {
117116
}
118117

119118
const token = this.nextTokens[n - 1];
119+
120120
return types ? Parser.checkTokenType(token, types) : token;
121121
}
122122

@@ -129,6 +129,7 @@ class Parser {
129129

130130
popToken() {
131131
const token = this.tokens.next();
132+
132133
if (token.done) {
133134
throw new ParserError('Unexpected end of tokens');
134135
}
@@ -137,6 +138,7 @@ class Parser {
137138

138139
generateAST() {
139140
const ast = this.generateRootNode();
141+
140142
this.getExpectedToken(TOKEN_STRUCTURE_EOF);
141143
return ast;
142144
}
@@ -180,6 +182,7 @@ class Parser {
180182

181183
generateLiteral() {
182184
const token = this.getExpectedToken(TOKEN_STRUCTURE_TEXT_LITERAL);
185+
183186
return {
184187
type: PARSER_TYPE_TEXT_LITERAL,
185188
value: token.value
@@ -188,9 +191,9 @@ class Parser {
188191

189192
generateStatement() {
190193
const token = this.lookahead(2, [TOKEN_STATEMENT_IF, TOKEN_STATEMENT_INCLUDE]);
194+
191195
return (
192-
token.subType === TOKEN_STATEMENT_IF
193-
? this.generateBranchStatement() : this.generateIncludeStatement()
196+
token.subType === TOKEN_STATEMENT_IF ? this.generateBranchStatement() : this.generateIncludeStatement()
194197
);
195198
}
196199

@@ -218,13 +221,15 @@ class Parser {
218221
]);
219222

220223
let condition;
224+
221225
if (token.subType !== TOKEN_STATEMENT_ELSE) {
222226
condition = this.generateExpression();
223227
}
224228
// else it's an ELSE token
225229

226230
this.getExpectedToken(TOKEN_BOUNDARY_TAG_END);
227231
const consequent = this.generateRootNode();
232+
228233
branches.push({
229234
condition, consequent
230235
});
@@ -250,6 +255,7 @@ class Parser {
250255
]);
251256

252257
let value;
258+
253259
if (token.subType === TOKEN_VALUE_VARIABLE) {
254260
value = {
255261
type: PARSER_TYPE_VARIABLE,
@@ -281,6 +287,7 @@ class Parser {
281287

282288
while (true) {
283289
let token = this.lookahead();
290+
284291
if (
285292
token.type !== TOKEN_TYPE_BINARY_OPERATOR
286293
|| PrecedenceTable[token.subType].precedence < minPrecedence
@@ -290,7 +297,7 @@ class Parser {
290297
token = this.getExpectedToken(TOKEN_TYPE_BINARY_OPERATOR);
291298
const precedenceInfo = PrecedenceTable[token.subType];
292299
const nextPrecedence = precedenceInfo.precedence + 1;
293-
// there currently is no right associative binary operator
300+
// there currently is no right associative binary operator
294301
// const nextPrecedence = precedenceInfo.association === LEFT_ASSOCIATIVE
295302
// ? precedenceInfo.precedence + 1 : precedenceInfo.precedence;
296303

@@ -325,6 +332,7 @@ class Parser {
325332

326333
generateUnaryExpression() {
327334
const token = this.getExpectedToken(TOKEN_TYPE_UNARY_OPERATOR);
335+
328336
return {
329337
type: PARSER_TYPE_UNARY_OPERATOR,
330338
operator: token.subType,
@@ -334,7 +342,9 @@ class Parser {
334342

335343
generateBracketExpression() {
336344
this.getExpectedToken(TOKEN_BOUNDARY_BRACKET_OPEN);
345+
337346
const expression = this.generateExpression();
347+
338348
this.getExpectedToken(TOKEN_BOUNDARY_BRACKET_CLOSE);
339349
return expression;
340350
}
@@ -343,26 +353,28 @@ class Parser {
343353
const token = this.getExpectedToken([
344354
TOKEN_TYPE_VALUE, TOKEN_BOUNDARY_STRING_SINGLE, TOKEN_BOUNDARY_STRING_DOUBLE
345355
]);
356+
346357
if (token.subType === TOKEN_VALUE_VARIABLE) {
347358
return {
348359
type: PARSER_TYPE_VARIABLE,
349360
name: token.value
350361
};
351362
}
352-
363+
353364
let value;
365+
354366
if (token.subType === TOKEN_BOUNDARY_STRING_SINGLE || token.subType === TOKEN_BOUNDARY_STRING_DOUBLE) {
355367
value = this.getExpectedToken(TOKEN_VALUE_STRING).value;
356368
this.getExpectedToken(token.subType);
357369
}
358-
370+
359371
if (token.subType === TOKEN_VALUE_FLOAT) {
360372
value = parseFloat(token.value);
361373
}
362374
else if (token.subType === TOKEN_VALUE_INTEGER) {
363375
value = parseInt(token.value, 10);
364376
}
365-
377+
366378
return {
367379
type: PARSER_TYPE_VALUE,
368380
value

test/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
extends: mitmaro/mocha
3+
rules:
4+
func-names: off
5+
no-undefined: off

test/Lexer/Lexer.isAtDelimiter.spec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const expect = require('chai').expect;
44
const Lexer = require('../../src/Lexer');
55

66
describe('Lexer.isAtDelimiter', function() {
7-
87
// false test cases
98
[
109
{
@@ -50,35 +49,39 @@ describe('Lexer.isAtDelimiter', function() {
5049
].forEach((testCase) => {
5150
it(`should return false with ${testCase.description}`, function() {
5251
const lexer = new Lexer(testCase.input);
52+
5353
expect(lexer.isAtDelimiter()).to.be.false;
5454
});
5555
});
56-
56+
5757
[
5858
'(', ')', '"', '\'', '!', '>', '<',
5959
'{{', '}}', '==', '!=', '&&', '||', '>=', '<=',
6060
'===', '!=='
6161
].forEach((delimiter) => {
6262
it(`should return ${delimiter} delimiter`, function() {
6363
const lexer = new Lexer(delimiter);
64+
6465
expect(lexer.isAtDelimiter()).to.be.equal(delimiter);
6566
});
6667
});
67-
68+
6869
// the following need to be tested special because they are special characters
6970
it(`should return space delimiter`, function() {
7071
const lexer = new Lexer(' ');
72+
7173
expect(lexer.isAtDelimiter()).to.be.equal(' ');
7274
});
73-
75+
7476
it(`should return tab delimiter`, function() {
7577
const lexer = new Lexer('\t');
78+
7679
expect(lexer.isAtDelimiter()).to.be.equal('\t');
7780
});
78-
81+
7982
it(`should return newline delimiter`, function() {
8083
const lexer = new Lexer('\n');
84+
8185
expect(lexer.isAtDelimiter()).to.be.equal('\n');
8286
});
83-
8487
});

test/Lexer/Lexer.scanNextDelimiter.spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,34 @@ const Lexer = require('../../src/Lexer');
66
describe('Lexer.scanNextDelimiter', function() {
77
it('should scan with empty string', function() {
88
const lexer = new Lexer('');
9+
910
lexer.scanNextDelimiter();
1011
expect(lexer.pointer).to.equal(0);
1112
});
12-
13+
1314
it('should scan with non-delimiter', function() {
1415
const lexer = new Lexer('aaa');
16+
1517
lexer.scanNextDelimiter();
1618
expect(lexer.pointer).to.equal(0);
1719
});
18-
20+
1921
it('should scan with single character delimiter', function() {
2022
const lexer = new Lexer('(');
23+
2124
lexer.scanNextDelimiter();
2225
expect(lexer.pointer).to.equal(1);
2326
});
24-
27+
2528
it('should scan with double character delimiter', function() {
2629
const lexer = new Lexer('==');
30+
2731
lexer.scanNextDelimiter();
2832
expect(lexer.pointer).to.equal(2);
2933
});
3034
it('should scan with triple character delimiter', function() {
3135
const lexer = new Lexer('===');
36+
3237
lexer.scanNextDelimiter();
3338
expect(lexer.pointer).to.equal(3);
3439
});

0 commit comments

Comments
 (0)