Skip to content

Commit 087b85a

Browse files
committed
Support evaluating .concat on strings.
1 parent 154c579 commit 087b85a

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎lib/Parser.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,36 @@ class Parser extends Tapable {
362362
return new BasicEvaluatedExpression().setTemplateString(parts).setRange(node.range);
363363
});
364364

365+
this.plugin("evaluate CallExpression .concat", function(expr, param) {
366+
if(!param.isString() && !param.isWrapped()) return;
367+
368+
let stringSuffix = null;
369+
let hasUnknownParams = false;
370+
for(let i = expr.arguments.length - 1; i >= 0; i--) {
371+
const argExpr = this.evaluateExpression(expr.arguments[i]);
372+
if(!argExpr.isString() && !argExpr.isNumber()) {
373+
hasUnknownParams = true;
374+
break;
375+
}
376+
377+
const value = argExpr.isString() ? argExpr.string : "" + argExpr.number;
378+
379+
const newString = value + (stringSuffix ? stringSuffix.string : "");
380+
const newRange = [argExpr.range[0], (stringSuffix || argExpr).range[1]];
381+
stringSuffix = new BasicEvaluatedExpression().setString(newString).setRange(newRange);
382+
}
383+
384+
if(hasUnknownParams) {
385+
const prefix = param.isString() ? param : param.prefix;
386+
return new BasicEvaluatedExpression().setWrapped(prefix, stringSuffix).setRange(expr.range);
387+
} else if(param.isWrapped()) {
388+
const postfix = stringSuffix || param.postfix;
389+
return new BasicEvaluatedExpression().setWrapped(param.prefix, postfix).setRange(expr.range);
390+
} else {
391+
const newString = param.string + (stringSuffix ? stringSuffix.string : "");
392+
return new BasicEvaluatedExpression().setString(newString).setRange(expr.range);
393+
}
394+
});
365395
this.plugin("evaluate CallExpression .split", function(expr, param) {
366396
if(!param.isString()) return;
367397
if(expr.arguments.length !== 1) return;

‎test/Parser.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,37 @@ describe("Parser", () => {
331331
"b.Number": "number=123",
332332
"b['Number']": "number=123",
333333
"b[Number]": "",
334+
"'str'.concat()": "string=str",
335+
"'str'.concat('one')": "string=strone",
336+
"'str'.concat('one').concat('two')": "string=stronetwo",
337+
"'str'.concat('one').concat('two', 'three')": "string=stronetwothree",
338+
"'str'.concat('one', 'two')": "string=stronetwo",
339+
"'str'.concat('one', 'two').concat('three')": "string=stronetwothree",
340+
"'str'.concat('one', 'two').concat('three', 'four')": "string=stronetwothreefour",
341+
"'str'.concat('one', obj)": "wrapped=['str' string=str]+[null]",
342+
"'str'.concat('one', obj).concat()": "wrapped=['str' string=str]+[null]",
343+
"'str'.concat('one', obj, 'two')": "wrapped=['str' string=str]+['two' string=two]",
344+
"'str'.concat('one', obj, 'two').concat()": "wrapped=['str' string=str]+['two' string=two]",
345+
"'str'.concat('one', obj, 'two').concat('three')": "wrapped=['str' string=str]+['three' string=three]",
346+
"'str'.concat(obj)": "wrapped=['str' string=str]+[null]",
347+
"'str'.concat(obj).concat()": "wrapped=['str' string=str]+[null]",
348+
"'str'.concat(obj).concat('one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]",
349+
"'str'.concat(obj).concat(obj, 'one')": "wrapped=['str' string=str]+['one' string=one]",
350+
"'str'.concat(obj).concat(obj, 'one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]",
351+
"'str'.concat(obj).concat('one', obj, 'one')": "wrapped=['str' string=str]+['one' string=one]",
352+
"'str'.concat(obj).concat('one', obj, 'two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]",
353+
"'str'.concat(obj, 'one')": "wrapped=['str' string=str]+['one' string=one]",
354+
"'str'.concat(obj, 'one').concat()": "wrapped=['str' string=str]+['one' string=one]",
355+
"'str'.concat(obj, 'one').concat('two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]",
356+
"'str'.concat(obj, 'one').concat(obj, 'two', 'three')": "wrapped=['str' string=str]+['two', 'three' string=twothree]",
357+
"'str'.concat(obj, 'one').concat('two', obj, 'three')": "wrapped=['str' string=str]+['three' string=three]",
358+
"'str'.concat(obj, 'one').concat('two', obj, 'three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]",
359+
"'str'.concat(obj, 'one', 'two')": "wrapped=['str' string=str]+['one', 'two' string=onetwo]",
360+
"'str'.concat(obj, 'one', 'two').concat()": "wrapped=['str' string=str]+['one', 'two' string=onetwo]",
361+
"'str'.concat(obj, 'one', 'two').concat('three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]",
362+
"'str'.concat(obj, 'one', 'two').concat(obj, 'three', 'four')": "wrapped=['str' string=str]+['three', 'four' string=threefour]",
363+
"'str'.concat(obj, 'one', 'two').concat('three', obj, 'four')": "wrapped=['str' string=str]+['four' string=four]",
364+
"'str'.concat(obj, 'one', 'two').concat('three', obj, 'four', 'five')": "wrapped=['str' string=str]+['four', 'five' string=fourfive]",
334365
"`start${obj}mid${obj2}end`": "template=[start string=start],[mid string=mid],[end string=end]", // eslint-disable-line no-template-curly-in-string
335366
"`start${'str'}mid${obj2}end`": "template=[start${'str'}mid string=startstrmid],[end string=end]", // eslint-disable-line no-template-curly-in-string
336367
"'abc'.substr(1)": "string=bc",

0 commit comments

Comments
 (0)