Skip to content

Commit

Permalink
refactor: remove unnecessary stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 19, 2019
1 parent 190f9bc commit 87e1dac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
33 changes: 19 additions & 14 deletions src/__tests__/exceptions.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import {throws} from './util/helpers';

// Unclosed elements
throws('unclosed string', 'a[href="wow]');
throws('unclosed comment', '/* oops');
throws('unclosed pseudo element', 'button::');
throws('unclosed pseudo class', 'a:');
throws('unclosed string', 'a[href="wow]', 'Unclosed quote');
throws('unclosed comment', '/* oops', 'Unclosed comment');
throws('unclosed pseudo element', 'button::', 'Expected a pseudo-class or pseudo-element.');
throws('unclosed pseudo class', 'a:', 'Expected a pseudo-class or pseudo-element.');
throws('unclosed attribute selector', '[name="james"][href');

throws('no opening parenthesis', ')');
throws('no opening parenthesis (2)', ':global.foo)');
throws('no opening parenthesis (3)', 'h1:not(h2:not(h3)))');
throws('no opening parenthesis', ')', 'Expected an opening parenthesis.');
throws('no opening parenthesis (2)', ':global.foo)', 'Expected an opening parenthesis.');
throws('no opening parenthesis (3)', 'h1:not(h2:not(h3)))', 'Expected an opening parenthesis.');

throws('no opening square bracket', ']');
throws('no opening square bracket (2)', ':global.foo]');
throws('no opening square bracket (3)', '[global]]');
throws('no opening square bracket', ']', 'Expected an opening square bracket.');
throws('no opening square bracket (2)', ':global.foo]', 'Expected an opening square bracket.');
throws('no opening square bracket (3)', '[global]]', 'Expected an opening square bracket.');

throws('bad pseudo element', 'button::"after"');
throws('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])');
throws('bad pseudo element', 'button::"after"', 'Expected a pseudo-class or pseudo-element.');
throws('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])', 'Expected a closing parenthesis.');

throws('bad syntax', '-moz-osx-font-smoothing: grayscale');
throws('bad syntax (2)', '! .body');
throws('bad syntax', '-moz-osx-font-smoothing: grayscale', 'Expected a pseudo-class or pseudo-element.');
throws('bad syntax (2)', '! .body', 'Unexpected \'!\'. Escaping special characters with \\ may help.');

throws('missing backslash for semicolon', '.;');
throws('missing backslash for semicolon (2)', '.\;');
throws('unexpected / foo', '-Option\/root', "Unexpected '/'. Escaping special characters with \\ may help.");
throws('bang in selector', '.foo !optional', "Unexpected '!'. Escaping special characters with \\ may help.");

throws('misplaced parenthesis', ':not(', 'Expected a closing parenthesis.');
throws('misplaced parenthesis (2)', ':not)', 'Expected an opening parenthesis.');
throws('misplaced parenthesis (3)', ':not((', 'Expected a closing parenthesis.');
throws('misplaced parenthesis (4)', ':not))', 'Expected an opening parenthesis.');
33 changes: 13 additions & 20 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,14 @@ export default class Parser {
});
}

missingParenthesis () {
missingOpeningParenthesis () {
return this.expected('opening parenthesis', this.currToken[TOKEN.START_POS]);
}

missingClosingParenthesis () {
return this.expected('closing parenthesis', this.currToken[TOKEN.START_POS]);
}

missingSquareBracket () {
return this.expected('opening square bracket', this.currToken[TOKEN.START_POS]);
}
Expand Down Expand Up @@ -702,6 +706,10 @@ export default class Parser {
parenValue += this.parseParenthesisToken(this.currToken);
this.position ++;
}
if (unbalanced) {
this.position --;
return this.missingClosingParenthesis();
}
if (last) {
last.appendToPropertyAndEscape("value", parenValue, parenValue);
} else {
Expand All @@ -718,7 +726,8 @@ export default class Parser {
}
}
if (unbalanced) {
return this.expected('closing parenthesis', this.currToken[TOKEN.START_POS]);
this.position --;
return this.missingClosingParenthesis();
}
}

Expand All @@ -733,22 +742,13 @@ export default class Parser {
return this.expected(['pseudo-class', 'pseudo-element'], this.position - 1);
}
if (this.currToken[TOKEN.TYPE] === tokens.word) {
this.splitWord(false, (first, length) => {
this.splitWord(false, (first) => {
pseudoStr += first;
this.newNode(new Pseudo({
value: pseudoStr,
source: getTokenSourceSpan(startingToken, this.currToken),
sourceIndex: startingToken[TOKEN.START_POS],
}));
if (
length > 1 &&
this.nextToken &&
this.nextToken[TOKEN.TYPE] === tokens.openParenthesis
) {
this.error('Misplaced parenthesis.', {
index: this.nextToken[TOKEN.START_POS],
});
}
});
} else {
return this.expected(['pseudo-class', 'pseudo-element'], this.currToken[TOKEN.START_POS]);
Expand Down Expand Up @@ -813,13 +813,6 @@ export default class Parser {
this.position ++;
let current = this.content();
word += current;
if (current.lastIndexOf('\\') === current.length - 1) {
let next = this.nextToken;
if (next && next[TOKEN.TYPE] === tokens.space) {
word += this.requiredSpace(this.content(next));
this.position ++;
}
}
nextToken = this.nextToken;
}
const hasClass = indexesOf(word, '.').filter(i => word[i - 1] !== '\\');
Expand Down Expand Up @@ -905,7 +898,7 @@ export default class Parser {
break;
case tokens.closeParenthesis:
if (throwOnParenthesis) {
this.missingParenthesis();
this.missingOpeningParenthesis();
}
break;
case tokens.openSquare:
Expand Down

0 comments on commit 87e1dac

Please sign in to comment.