Skip to content

Commit 912767c

Browse files
committed
refactor: set the namespaced node's start inside namespace()
Instead of passing a start token through word(), splitWord() and universal(), namespace() now adjusts the first node they create.
1 parent 8afdfbc commit 912767c

1 file changed

Lines changed: 27 additions & 33 deletions

File tree

‎src/parser.js‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -843,22 +843,31 @@ export default class Parser {
843843
prev[TOKEN.TYPE] === tokens.ampersand)
844844
? this.content(prev)
845845
: true;
846-
// The node starts at its prefix, or at the `|` when there is none.
847-
const start = before === true ? this.currToken : prev;
848846
if (!this.nextToken) {
849847
// A trailing `|` with nothing after it. `unexpectedPipe` reports against
850848
// `currToken`, which is the pipe itself and always present here.
851849
return this.unexpectedPipe();
852850
}
853-
if (this.nextToken[TOKEN.TYPE] === tokens.word) {
854-
this.position++;
855-
return this.word(before, start);
856-
} else if (this.nextToken[TOKEN.TYPE] === tokens.asterisk) {
857-
this.position++;
858-
return this.universal(before, start);
851+
const type = this.nextToken[TOKEN.TYPE];
852+
if (type !== tokens.word && type !== tokens.asterisk) {
853+
return this.unexpectedPipe();
854+
}
855+
// The node starts at its prefix, or at the `|` when there is none, but
856+
// `word()` and `universal()` only see the name after the `|`.
857+
const start = before === true ? this.currToken : prev;
858+
const nodes = this.current.nodes;
859+
const index = nodes.length;
860+
this.position++;
861+
if (type === tokens.word) {
862+
this.word(before);
863+
} else {
864+
this.universal(before);
865+
}
866+
const node = nodes[index];
867+
if (node) {
868+
node.sourceIndex = start[TOKEN.START_POS];
869+
node.source.start = { line: start[TOKEN.START_LINE], column: start[TOKEN.START_COL] };
859870
}
860-
861-
this.unexpectedPipe();
862871
}
863872

864873
nesting() {
@@ -1071,12 +1080,7 @@ export default class Parser {
10711080
this.position++;
10721081
}
10731082

1074-
/**
1075-
* @param {string | true} [namespace]
1076-
* @param {Array} [start] the token the node starts at when it has a
1077-
* namespace, which is its prefix or the `|`
1078-
*/
1079-
universal(namespace, start) {
1083+
universal(namespace) {
10801084
const nextToken = this.nextToken;
10811085
if (nextToken && this.content(nextToken) === "|") {
10821086
this.position++;
@@ -1086,21 +1090,15 @@ export default class Parser {
10861090
this.newNode(
10871091
new Universal({
10881092
value: this.content(),
1089-
source: getTokenSourceSpan(start || current, current),
1090-
sourceIndex: (start || current)[TOKEN.START_POS],
1093+
source: getTokenSource(current),
1094+
sourceIndex: current[TOKEN.START_POS],
10911095
}),
10921096
namespace,
10931097
);
10941098
this.position++;
10951099
}
10961100

1097-
/**
1098-
* @param {string | true | false | null} [namespace]
1099-
* @param {Function} [firstCallback]
1100-
* @param {Array} [start] the token the first node starts at when it has a
1101-
* namespace, which is its prefix or the `|`
1102-
*/
1103-
splitWord(namespace, firstCallback, start) {
1101+
splitWord(namespace, firstCallback) {
11041102
let nextToken = this.nextToken;
11051103
let word = this.content();
11061104
while (
@@ -1148,12 +1146,8 @@ export default class Parser {
11481146
}
11491147
let node;
11501148
const current = this.currToken;
1151-
let sourceIndex = current[TOKEN.START_POS] + indices[i];
1152-
let source = getSource(current[1], current[2] + ind, current[3], current[2] + (index - 1));
1153-
if (i === 0 && start) {
1154-
sourceIndex = start[TOKEN.START_POS];
1155-
source.start = { line: start[TOKEN.START_LINE], column: start[TOKEN.START_COL] };
1156-
}
1149+
const sourceIndex = current[TOKEN.START_POS] + indices[i];
1150+
const source = getSource(current[1], current[2] + ind, current[3], current[2] + (index - 1));
11571151
if (classIndexes.has(ind)) {
11581152
let classNameOpts = {
11591153
value: value.slice(1),
@@ -1189,13 +1183,13 @@ export default class Parser {
11891183
this.position++;
11901184
}
11911185

1192-
word(namespace, start) {
1186+
word(namespace) {
11931187
const nextToken = this.nextToken;
11941188
if (nextToken && this.content(nextToken) === "|") {
11951189
this.position++;
11961190
return this.namespace();
11971191
}
1198-
return this.splitWord(namespace, undefined, start);
1192+
return this.splitWord(namespace);
11991193
}
12001194

12011195
loop() {

0 commit comments

Comments
 (0)