Skip to content

Commit

Permalink
implement Parser#getLocationFromList() and related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Feb 5, 2017
1 parent 1695f8c commit 074f5c6
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 36 deletions.
16 changes: 14 additions & 2 deletions lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Parser.prototype = {
readSC: readSC,
readSequence: sequence.default,

getLocation: function getLocation(start, end) {
getLocation: function(start, end) {
if (this.needPositions) {
return this.scanner.getLocationRange(
start,
Expand All @@ -189,7 +189,19 @@ Parser.prototype = {

return null;
},
parse: function parse(source, options) {
getLocationFromList: function(list) {
if (this.needPositions) {
return this.scanner.getLocationRange(
list.head !== null ? list.first().loc.start.offset : this.scanner.tokenStart,
list.head !== null ? list.last().loc.end.offset : this.scanner.tokenStart,
this.filename
);
}

return null;
},

parse: function(source, options) {
options = options || {};

var context = options.context || 'stylesheet';
Expand Down
9 changes: 2 additions & 7 deletions lib/parser/type/AtruleExpression.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var List = require('../../utils/list');

module.exports = function AtruleExpression(name) {
var start = this.scanner.tokenStart;
var end = start;
var children = null;

name = name.toLowerCase();
Expand All @@ -11,6 +9,7 @@ module.exports = function AtruleExpression(name) {
if (this.atrule.hasOwnProperty(name)) {
if (typeof this.atrule[name].expression === 'function') {
children = this.atrule[name].expression.call(this);

if (children instanceof List === false) {
return children;
}
Expand All @@ -25,13 +24,9 @@ module.exports = function AtruleExpression(name) {
return null;
}

if (this.needPositions) {
end = children.last().loc.end.offset;
}

return {
type: 'AtruleExpression',
loc: this.getLocation(start, end),
loc: this.getLocationFromList(children),
children: children
};
};
2 changes: 1 addition & 1 deletion lib/parser/type/Brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var RIGHTSQUAREBRACKET = TYPE.RightSquareBracket;
// [ ident* ]
module.exports = function Brackets(readSequence) {
var start = this.scanner.tokenStart;
var children;
var children = null;

this.scanner.eat(LEFTSQUAREBRACKET);
children = readSequence.call(this);
Expand Down
3 changes: 1 addition & 2 deletions lib/parser/type/DeclarationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var COMMENT = TYPE.Comment;
var SEMICOLON = TYPE.Semicolon;

module.exports = function DeclarationList() {
var start = this.scanner.tokenStart;
var children = new List();

scan:
Expand All @@ -25,7 +24,7 @@ module.exports = function DeclarationList() {

return {
type: 'DeclarationList',
loc: this.getLocation(start, this.scanner.tokenStart),
loc: this.getLocationFromList(children),
children: children
};
};
8 changes: 1 addition & 7 deletions lib/parser/type/MediaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ var DISALLOW_VAR = false;
module.exports = function MediaQuery() {
this.readSC();

var start = this.scanner.tokenStart;
var end = start;
var children = new List();
var wasSpace = false;
var child = null;
Expand Down Expand Up @@ -55,13 +53,9 @@ module.exports = function MediaQuery() {
this.scanner.error('Identifier or parenthesis is expected');
}

if (this.needPositions) {
end = child.loc.end.offset;
}

return {
type: 'MediaQuery',
loc: this.getLocation(start, end),
loc: this.getLocationFromList(children),
children: children
};
};
8 changes: 1 addition & 7 deletions lib/parser/type/MediaQueryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var COMMA = require('../../tokenizer').TYPE.Comma;
module.exports = function MediaQueryList(relative) {
this.readSC();

var start = this.scanner.tokenStart;
var end = start;
var children = new List();
var mediaQuery = null;

Expand All @@ -20,13 +18,9 @@ module.exports = function MediaQueryList(relative) {
this.scanner.next();
}

if (this.needPositions) {
end = mediaQuery.children.last().loc.end.offset;
}

return {
type: 'MediaQueryList',
loc: this.getLocation(start, end),
loc: this.getLocationFromList(children),
children: children
};
};
5 changes: 2 additions & 3 deletions lib/parser/type/Nth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ module.exports = function Nth(allowOfClause) {
this.readSC();

var start = this.scanner.tokenStart;
var end;
var end = start;
var selector = null;
var query;

if (this.scanner.lookupValue(0, 'odd') || this.scanner.lookupValue(0, 'even')) {
query = this.Identifier(DISALLOW_VAR);
end = this.scanner.tokenStart;
} else {
query = this.AnPlusB();
}
Expand All @@ -24,7 +23,7 @@ module.exports = function Nth(allowOfClause) {
selector = this.SelectorList();

if (this.needPositions) {
end = selector.children.last().children.last().loc.end.offset;
end = selector.children.last().loc.end.offset;
}
} else {
if (this.needPositions) {
Expand Down
8 changes: 1 addition & 7 deletions lib/parser/type/SelectorList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ var BALANCED = true;
module.exports = function SelectorList(relative) {
this.readSC();

var start = this.scanner.tokenStart;
var end = start;
var children = new List();
var selector = null;

while (!this.scanner.eof) {
selector = this.parseSelector ? this.Selector(relative) : this.Raw(BALANCED, COMMA, LEFTCURLYBRACKET);
children.appendData(selector);

if (this.needPositions) {
end = selector.loc.end.offset;
}

if (this.scanner.tokenType === COMMA) {
this.scanner.next();
continue;
Expand All @@ -31,7 +25,7 @@ module.exports = function SelectorList(relative) {

return {
type: 'SelectorList',
loc: this.getLocation(start, end),
loc: this.getLocationFromList(children),
children: children
};
};

0 comments on commit 074f5c6

Please sign in to comment.