Skip to content

Commit

Permalink
fix parse interpolation error
Browse files Browse the repository at this point in the history
  • Loading branch information
chenfuqiang committed May 12, 2021
1 parent 20d1e57 commit 5e1fa13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/nodes/interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

module.exports = {
interpolation(token) {
let first = token;
const tokens = [token];
const validTypes = ['word', '{', '}'];

token = this.tokenizer.nextToken();
const tokens = [token, this.tokenizer.nextToken()];
const validTypes = ['word', '}'];

// look for @{ but not @[word]{
if (first[1].length > 1 || token[0] !== '{') {
this.tokenizer.back(token);
if (tokens[0][1].length > 1 || tokens[1][0] !== '{') {
this.tokenizer.back(tokens[1]);
return false;
}

token = this.tokenizer.nextToken();
while (token && validTypes.includes(token[0])) {
tokens.push(token);
token = this.tokenizer.nextToken();
}

const words = tokens.map((tokn) => tokn[1]);
[first] = tokens;
const [first] = tokens;
const last = tokens.pop();
const start = [first[2], first[3]];
const end = [last[4] || last[2], last[5] || last[3]];
Expand Down
8 changes: 8 additions & 0 deletions test/parser/interpolation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test('parses interpolation', (t) => {
t.is(root.first.first.value, '@{color}');
});

test('parses interpolation when there is not space between selector with open bracket', (t) => {
const root = parse('@{selector}-title{ @{prop}-size: @{color} }');

t.is(root.first.selector, '@{selector}-title');
t.is(root.first.first.prop, '@{prop}-size');
t.is(root.first.first.value, '@{color}');
});

test('parses mixin interpolation', (t) => {
const less = '.browser-prefix(@prop, @args) {\n @{prop}: @args;\n}';
const root = parse(less);
Expand Down

0 comments on commit 5e1fa13

Please sign in to comment.