Skip to content

Commit

Permalink
fix bug #430
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed Jun 27, 2017
1 parent ab850d3 commit 7eaac51
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 93 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v4.12.0

1. `bail` 默认为 `true`
2. 修复 filter 不支持 javascript 表达式的问题 [#430](https://github.com/aui/art-template/issues/430)

## v4.11.0

Expand Down
82 changes: 43 additions & 39 deletions lib/compile/adapter/rule.art.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ var artRule = {

case 'set':

code = 'var ' + values.join('');
code = 'var ' + values.join('').trim();
break;

case 'if':

code = 'if(' + values.join('') + '){';
code = 'if(' + values.join('').trim() + '){';

break;

case 'else':

var indexIf = values.indexOf('if');

if (indexIf > -1) {
if (~indexIf) {
values.splice(0, indexIf + 1);
code = '}else if(' + values.join('') + '){';
code = '}else if(' + values.join('').trim() + '){';
} else {
code = '}else{';
}
Expand Down Expand Up @@ -86,7 +86,7 @@ var artRule = {

case 'block':

code = 'block(' + values.join('') + ',function(){';
code = 'block(' + values.join('').trim() + ',function(){';
break;

case '/block':
Expand All @@ -111,41 +111,38 @@ var artRule = {

default:

if (values.indexOf('|') !== -1) {
if (~values.indexOf('|')) {

// 解析过滤器

var target = key;
var _group = [];
var v3split = ':'; // ... v3 compat ...

// TODO: typeof value | filterName
var list = values.filter(function (value) {
return !/^\s+$/.test(value);
});
// 将过滤器解析成二维数组
var _group = esTokens.reduce(function (group, token) {
var value = token.value;

// 找到要过滤的目标表达式
while (list[0] !== '|') {
target += list.shift();
} // 将过滤器解析成二维数组
list.filter(function (v) {
return v !== v3split;
}).forEach(function (value) {
if (value === '|') {
_group.push([]);
} else {
_group[_group.length - 1].push(value);
group.push([]);
} else if (/^\S+$/.test(value)) {
if (!group.length) {
group.push([]);
}
if (value === v3split && group[group.length - 1].length === 1) {
warn('value | filter: argv', 'value | filter argv');
} else {
group[group.length - 1].push(token);
}
}
return group;
}, []).map(function (g) {
return artRule._split(g);
});

// 将过滤器管道化
_group.reduce(function (accumulator, filter) {
code = _group.reduce(function (accumulator, filter) {
var name = filter.shift();
filter.unshift(accumulator);
return code = '$imports.' + name + '(' + filter.join(',') + ')';
}, target);
} else {
code = '' + key + values.join('');
return '$imports.' + name + '(' + filter.join(',') + ')';
}, _group.shift().join(' ').trim());
}

output = output || 'escape';
Expand All @@ -159,27 +156,34 @@ var artRule = {
return result;
},

// 将多个 javascript 表达式按空格分组
// 将多个 javascript 表达式拆分成组
// 支持基本运算、三元表达式、取值、运行函数
// 只支持 string、number、boolean、null、undefined 这几种类型声明,不支持 function、object、array
_split: function _split(esTokens) {

esTokens = esTokens.filter(function (_ref) {
var type = _ref.type;

return type !== 'whitespace' && type !== 'comment';
});

var current = 0;
var lastToken = esTokens.shift();
var punctuator = 'punctuator';
var close = /\]|\)/;
var group = [[lastToken]];

while (current < esTokens.length) {
var esToken = esTokens[current];
var esTokenType = esToken.type;

if (esTokenType !== 'whitespace' && esTokenType !== 'comment') {

if (lastToken.type === 'punctuator' && lastToken.value !== ']' || esTokenType === 'punctuator') {
group[group.length - 1].push(esToken);
} else {
group.push([esToken]);
}

lastToken = esToken;
if (esToken.type === punctuator || lastToken.type === punctuator && !close.test(lastToken.value)) {
group[group.length - 1].push(esToken);
} else {
group.push([esToken]);
}

lastToken = esToken;

current++;
}

Expand Down
Loading

0 comments on commit 7eaac51

Please sign in to comment.