Skip to content

Commit

Permalink
edge cases and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Oct 4, 2016
1 parent 28c99a1 commit 5abec29
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/.vscode/
/tmp/
/coverage/
2 changes: 1 addition & 1 deletion lib/parser/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function sourceFragment(error, extraLines) {

if (column > MAX_LINE_LENGTH) {
cutLeft = column - OFFSET_CORRECTION + 3;
column = OFFSET_CORRECTION;
column = OFFSET_CORRECTION - 2;
}

for (var i = startLine; i <= endLine; i++) {
Expand Down
146 changes: 61 additions & 85 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var needPositions;
var filename;
var scanner;

var DESCENDANT_COMBINATOR = ' ';
var DESCENDANT_COMBINATOR = {};
var SPACE_NODE = { type: 'Space' };

var WHITESPACE = TokenType.Whitespace;
Expand Down Expand Up @@ -155,6 +155,8 @@ function getAtruleExpression() {
sequence: sequence
};

readSC();

scan:
while (!scanner.eof) {
switch (scanner.tokenType) {
Expand Down Expand Up @@ -193,9 +195,7 @@ function getAtruleExpression() {

if (wasSpace) {
wasSpace = false;
if (sequence.head !== null) { // ignore spaces in the beginning
sequence.appendData(SPACE_NODE);
}
sequence.appendData(SPACE_NODE);
}

sequence.appendData(child);
Expand All @@ -215,25 +215,21 @@ function getAtrule() {
block: null
};

if (!scanner.eof) {
switch (scanner.tokenType) {
case SEMICOLON:
scanner.next(); // {
break;

case LEFTCURLYBRACKET:
scanner.next(); // {
// at-rule expression can ends with semicolon, left curly bracket or eof
switch (scanner.tokenType) {
case SEMICOLON:
scanner.next(); // ;
break;

node.block = isBlockAtrule()
? getBlock()
: getStylesheet(true);
case LEFTCURLYBRACKET:
scanner.next(); // {

scanner.eat(RIGHTCURLYBRACKET);
break;
node.block = isBlockAtrule()
? getBlock()
: getStylesheet(true);

default:
scanner.error('Unexpected input');
}
scanner.eat(RIGHTCURLYBRACKET);
break;
}

return node;
Expand Down Expand Up @@ -304,6 +300,7 @@ function getSelector() {
function getSimpleSelector(nested) {
var sequence = new List();
var combinator = null;
var combinatorOffset = -1;
var child;
var node = {
type: 'SimpleSelector',
Expand All @@ -319,14 +316,14 @@ function getSimpleSelector(nested) {

case LEFTCURLYBRACKET:
if (nested) {
scanner.error('Unexpected input');
scanner.error();
}

break scan;

case RIGHTPARENTHESIS:
if (!nested) {
scanner.error('Unexpected input');
scanner.error();
}

break scan;
Expand All @@ -337,7 +334,8 @@ function getSimpleSelector(nested) {

case WHITESPACE:
if (combinator === null && sequence.head !== null) {
combinator = getCombinator();
combinatorOffset = scanner.tokenStart;
combinator = DESCENDANT_COMBINATOR;
} else {
scanner.next();
}
Expand All @@ -347,10 +345,12 @@ function getSimpleSelector(nested) {
case GREATERTHANSIGN:
case TILDE:
case SOLIDUS:
if (combinator !== null && combinator.name !== DESCENDANT_COMBINATOR) {
if ((sequence.head === null) || // combinator in the beginning
(combinator !== null && combinator !== DESCENDANT_COMBINATOR)) {
scanner.error('Unexpected combinator');
}

combinatorOffset = scanner.tokenStart;
combinator = getCombinator();
continue;

Expand Down Expand Up @@ -382,19 +382,28 @@ function getSimpleSelector(nested) {
break;

default:
scanner.error('Unexpected input');
scanner.error();
}

if (combinator !== null) {
// create descendant combinator on demand to avoid garbage
if (combinator === DESCENDANT_COMBINATOR) {
combinator = {
type: 'Combinator',
info: needPositions ? scanner.getLocation(combinatorOffset, filename) : null,
name: ' '
};
}

sequence.appendData(combinator);
combinator = null;
}

sequence.appendData(child);
}

if (combinator && combinator.name !== DESCENDANT_COMBINATOR) {
scanner.error('Unexpected combinator');
if (combinator !== null && combinator !== DESCENDANT_COMBINATOR) {
scanner.error('Unexpected combinator', combinatorOffset);
}

return node;
Expand Down Expand Up @@ -512,11 +521,12 @@ function getValue(nested, property) {
switch (scanner.tokenType) {
case RIGHTCURLYBRACKET:
case SEMICOLON:
case EXCLAMATIONMARK:
break scan;

case RIGHTPARENTHESIS:
if (!nested) {
scanner.error('Unexpected input');
scanner.error();
}

break scan;
Expand All @@ -543,12 +553,6 @@ function getValue(nested, property) {
child = getParentheses(SCOPE_VALUE);
break;

case EXCLAMATIONMARK:
if (nested) {
scanner.error('Unexpected exclamation mark');
}
break scan;

case STRING:
child = getString();
break;
Expand All @@ -572,9 +576,7 @@ function getValue(nested, property) {

if (wasSpace) {
wasSpace = false;
if (sequence.tail !== null) { // ignore spaces in the beginning
sequence.appendData(SPACE_NODE);
}
sequence.appendData(SPACE_NODE);
}

sequence.appendData(child);
Expand Down Expand Up @@ -618,10 +620,8 @@ function getAny(scope) {
value: number
};

break;

default:
scanner.error('Unexpected input');
scanner.error();
}

var info = getInfo();
Expand Down Expand Up @@ -724,7 +724,6 @@ function getParentheses(scope) {

// left brace
scanner.next();

readSC();

scan:
Expand All @@ -742,10 +741,6 @@ function getParentheses(scope) {
scanner.next();
continue;

case NUMBERSIGN: // ??
child = getHash();
break;

case LEFTPARENTHESIS:
child = getParentheses(scope);
break;
Expand All @@ -757,6 +752,10 @@ function getParentheses(scope) {
child = getOperator();
break;

case NUMBERSIGN:
child = getHash();
break;

case STRING:
child = getString();
break;
Expand All @@ -767,9 +766,7 @@ function getParentheses(scope) {

if (wasSpace) {
wasSpace = false;
if (sequence.head !== null) { // ignore spaces in the beginning
sequence.appendData(SPACE_NODE);
}
sequence.appendData(SPACE_NODE);
}

sequence.appendData(child);
Expand Down Expand Up @@ -813,11 +810,6 @@ function getCombinator() {
var combinator;

switch (scanner.tokenType) {
case WHITESPACE:
combinator = DESCENDANT_COMBINATOR;
scanner.next();
break;

case PLUSSIGN:
case TILDE:
case GREATERTHANSIGN:
Expand All @@ -832,9 +824,6 @@ function getCombinator() {
scanner.expectIdentifier('deep');
scanner.eat(SOLIDUS);
break;

default:
scanner.error('Combinator (+, >, ~, /deep/) is expected');
}

return {
Expand Down Expand Up @@ -867,10 +856,6 @@ function getComment() {

// special reader for units to avoid adjoined IE hacks (i.e. '1px\9')
function readUnit() {
if (scanner.tokenType !== IDENTIFIER) {
scanner.error('Identifier is expected');
}

var unit = scanner.getTokenValue();
var backSlashPos = unit.indexOf('\\');

Expand Down Expand Up @@ -1002,8 +987,8 @@ function getFunctionArguments(scope) {
if (wasSpace) {
wasSpace = false;

// ignore spaces in the beginning and around operator
if (sequence.tail !== null && !nonSpaceOperator && !prevNonSpaceOperator) {
// ignore spaces around operator
if (!nonSpaceOperator && !prevNonSpaceOperator) {
sequence.appendData(SPACE_NODE);
}
}
Expand Down Expand Up @@ -1166,9 +1151,7 @@ function scanUnicodeRange() {
var hexStart = scanner.tokenStart + 1; // skip +
var hexLength = 0;

if (scanner.tokenType === PLUSSIGN || scanner.tokenType === NUMBER) {
scanner.next();
}
scanner.next(); // always PLUSSIGN or NUMBER

if (scanner.tokenType === HYPHENMINUS) {
scanner.next();
Expand Down Expand Up @@ -1226,11 +1209,6 @@ function scanUnicodeRange() {
}
}

// If there are any code points left in text, this is an invalid <urange>,
if (scanner.tokenType === IDENTIFIER) {
scanner.error('Unexpected input');
}

return hexLength;
}

Expand Down Expand Up @@ -1411,7 +1389,7 @@ function getNthSelector() {

if (scanner.tokenType === IDENTIFIER) {
if (!cmpChar(scanner.source, scanner.tokenStart, N)) {
scanner.error('Unexpected input');
scanner.error();
}

sequence.appendData({
Expand Down Expand Up @@ -1457,22 +1435,20 @@ function getNthSelector() {
scanner.next();
readSC();

if (scanner.tokenType === NUMBER) {
if (cmpChar(scanner.source, scanner.tokenStart, PLUSSIGN) ||
cmpChar(scanner.source, scanner.tokenStart, HYPHENMINUS)) {
scanner.error('Unexpected input');
}
if (scanner.tokenType !== NUMBER ||
cmpChar(scanner.source, scanner.tokenStart, PLUSSIGN) ||
cmpChar(scanner.source, scanner.tokenStart, HYPHENMINUS)) {
scanner.error();
}

sequence.appendData({
type: 'Nth',
info: getInfo(),
value: scanner.getTokenValue()
});
sequence.appendData({
type: 'Nth',
info: getInfo(),
value: scanner.getTokenValue()
});

scanner.next();
}
scanner.next();
}

} else {
prefix = '';
scanner.next();
Expand All @@ -1497,7 +1473,7 @@ function getNthSelector() {

// prefix or sign should be specified but not both
if (!(prefix === '' ^ sign === '')) {
scanner.error('Unexpected input');
scanner.error();
}

if (sign) {
Expand Down
Loading

0 comments on commit 5abec29

Please sign in to comment.