2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-04-05 21:00:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const ContextDependencyHelpers = exports;
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2016-11-17 20:43:14 +08:00
|
|
|
/**
|
|
|
|
* Escapes regular expression metacharacters
|
2016-11-21 22:38:56 +08:00
|
|
|
* @param {string} str String to quote
|
2018-05-08 20:31:51 +08:00
|
|
|
* @returns {string} Escaped string
|
2016-11-17 20:43:14 +08:00
|
|
|
*/
|
2017-11-08 18:32:05 +08:00
|
|
|
const quotemeta = str => {
|
2017-01-11 17:51:58 +08:00
|
|
|
return str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2016-11-17 20:43:14 +08:00
|
|
|
|
2018-08-03 15:20:23 +08:00
|
|
|
const splitContextFromPrefix = prefix => {
|
|
|
|
const idx = prefix.lastIndexOf("/");
|
|
|
|
let context = ".";
|
|
|
|
if (idx >= 0) {
|
|
|
|
context = prefix.substr(0, idx);
|
|
|
|
prefix = `.${prefix.substr(idx)}`;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
prefix
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const splitQueryFromPostfix = postfix => {
|
|
|
|
const idx = postfix.indexOf("?");
|
|
|
|
let query = "";
|
|
|
|
if (idx >= 0) {
|
|
|
|
query = postfix.substr(idx);
|
|
|
|
postfix = postfix.substr(0, idx);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
postfix,
|
|
|
|
query
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
ContextDependencyHelpers.create = (
|
|
|
|
Dep,
|
|
|
|
range,
|
|
|
|
param,
|
|
|
|
expr,
|
|
|
|
options,
|
2018-10-30 19:39:43 +08:00
|
|
|
contextOptions,
|
|
|
|
// when parser is not passed in, expressions won't be walked
|
|
|
|
parser = null
|
2018-02-25 09:00:20 +08:00
|
|
|
) => {
|
|
|
|
if (param.isTemplateString()) {
|
2018-08-03 15:20:23 +08:00
|
|
|
let prefixRaw = param.quasis[0].string;
|
|
|
|
let postfixRaw =
|
2018-02-25 09:00:20 +08:00
|
|
|
param.quasis.length > 1
|
|
|
|
? param.quasis[param.quasis.length - 1].string
|
|
|
|
: "";
|
2018-08-03 15:20:23 +08:00
|
|
|
const prefixRange = [param.quasis[0].range[0], param.quasis[0].range[1]];
|
|
|
|
const postfixRange =
|
|
|
|
param.quasis.length > 1
|
|
|
|
? param.quasis[param.quasis.length - 1].range
|
|
|
|
: "";
|
|
|
|
const valueRange = param.range;
|
|
|
|
const { context, prefix } = splitContextFromPrefix(prefixRaw);
|
|
|
|
const { postfix, query } = splitQueryFromPostfix(postfixRaw);
|
2016-11-15 21:03:53 +08:00
|
|
|
// If there are more than two quasis, maybe the generated RegExp can be more precise?
|
2018-08-03 15:20:23 +08:00
|
|
|
const regExp = new RegExp(
|
2018-02-25 09:00:20 +08:00
|
|
|
`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(
|
|
|
|
postfix
|
|
|
|
)}$`
|
|
|
|
);
|
2018-08-03 15:20:23 +08:00
|
|
|
const dep = new Dep(
|
2018-02-25 09:00:20 +08:00
|
|
|
Object.assign(
|
|
|
|
{
|
2018-08-03 15:20:23 +08:00
|
|
|
request: context + query,
|
2018-02-25 09:00:20 +08:00
|
|
|
recursive: options.wrappedContextRecursive,
|
|
|
|
regExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
valueRange
|
|
|
|
);
|
2016-11-15 21:03:53 +08:00
|
|
|
dep.loc = expr.loc;
|
2018-08-03 15:20:23 +08:00
|
|
|
const replaces = [];
|
2018-10-30 19:39:43 +08:00
|
|
|
|
|
|
|
if (parser) {
|
|
|
|
let paramExpr = param.expression;
|
|
|
|
while (paramExpr.type !== "TemplateLiteral") {
|
|
|
|
if (paramExpr.type === "ConditionalExpression") {
|
|
|
|
const test = parser.evaluateExpression(paramExpr.test);
|
|
|
|
let replace;
|
|
|
|
if (test.asBool()) {
|
|
|
|
replace = paramExpr.alternate;
|
|
|
|
paramExpr = paramExpr.consequent;
|
|
|
|
} else {
|
|
|
|
replace = paramExpr.consequent;
|
|
|
|
paramExpr = paramExpr.alternate;
|
|
|
|
}
|
|
|
|
replaces.push({
|
|
|
|
range: replace.range,
|
|
|
|
value: JSON.stringify("")
|
|
|
|
});
|
|
|
|
} else if (paramExpr.type === "TaggedTemplateExpression") {
|
|
|
|
paramExpr = paramExpr.quasi;
|
|
|
|
} else {
|
|
|
|
break; // there are other cases, just ignore for now
|
|
|
|
}
|
2018-10-30 00:33:58 +08:00
|
|
|
}
|
2018-10-30 19:39:43 +08:00
|
|
|
param.parts.forEach(part => {
|
|
|
|
if (
|
|
|
|
part.range[0] > prefixRange[1] &&
|
|
|
|
(!postfixRange || part.range[1] < postfixRange[0])
|
|
|
|
) {
|
|
|
|
if (part.isString()) {
|
|
|
|
replaces.push({
|
|
|
|
range: part.range,
|
|
|
|
value: part.string
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
parser.walkExpression(part.expression);
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:20:23 +08:00
|
|
|
});
|
|
|
|
}
|
2018-10-30 19:39:43 +08:00
|
|
|
|
|
|
|
// replace even prefix === prefixRaw as it's evaluated
|
|
|
|
replaces.push({
|
|
|
|
range: prefixRange,
|
|
|
|
value: prefix
|
|
|
|
});
|
|
|
|
// replace even postfix === postfixRaw as it's evaluated
|
|
|
|
if (postfixRange) {
|
2018-08-03 15:20:23 +08:00
|
|
|
replaces.push({
|
|
|
|
range: postfixRange,
|
|
|
|
value: postfix
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dep.replaces = replaces;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.critical =
|
|
|
|
options.wrappedContextCritical &&
|
|
|
|
"a part of the request of a dependency is an expression";
|
2016-11-15 21:03:53 +08:00
|
|
|
return dep;
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (
|
|
|
|
param.isWrapped() &&
|
|
|
|
((param.prefix && param.prefix.isString()) ||
|
|
|
|
(param.postfix && param.postfix.isString()))
|
|
|
|
) {
|
2018-08-03 15:20:23 +08:00
|
|
|
let prefixRaw =
|
|
|
|
param.prefix && param.prefix.isString() ? param.prefix.string : "";
|
|
|
|
let postfixRaw =
|
2018-02-25 09:00:20 +08:00
|
|
|
param.postfix && param.postfix.isString() ? param.postfix.string : "";
|
2018-08-03 15:20:23 +08:00
|
|
|
const prefixRange =
|
2018-02-25 09:00:20 +08:00
|
|
|
param.prefix && param.prefix.isString() ? param.prefix.range : null;
|
2018-08-03 15:20:23 +08:00
|
|
|
const postfixRange =
|
|
|
|
param.postfix && param.postfix.isString() ? param.postfix.range : null;
|
|
|
|
const valueRange = param.range;
|
|
|
|
const { context, prefix } = splitContextFromPrefix(prefixRaw);
|
|
|
|
const { postfix, query } = splitQueryFromPostfix(postfixRaw);
|
|
|
|
const regExp = new RegExp(
|
2018-02-25 09:00:20 +08:00
|
|
|
`^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta(
|
|
|
|
postfix
|
|
|
|
)}$`
|
|
|
|
);
|
2018-08-03 15:20:23 +08:00
|
|
|
const dep = new Dep(
|
2018-02-25 09:00:20 +08:00
|
|
|
Object.assign(
|
|
|
|
{
|
2018-08-03 15:20:23 +08:00
|
|
|
request: context + query,
|
2018-02-25 09:00:20 +08:00
|
|
|
recursive: options.wrappedContextRecursive,
|
|
|
|
regExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
valueRange
|
|
|
|
);
|
2013-02-13 21:42:34 +08:00
|
|
|
dep.loc = expr.loc;
|
2018-08-03 15:20:23 +08:00
|
|
|
const replaces = [];
|
|
|
|
if (prefixRange && prefix !== prefixRaw) {
|
|
|
|
replaces.push({
|
|
|
|
range: prefixRange,
|
|
|
|
value: JSON.stringify(prefix)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (postfixRange && postfix !== postfixRaw) {
|
|
|
|
replaces.push({
|
|
|
|
range: postfixRange,
|
|
|
|
value: JSON.stringify(postfix)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dep.replaces = replaces;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.critical =
|
|
|
|
options.wrappedContextCritical &&
|
|
|
|
"a part of the request of a dependency is an expression";
|
2013-01-31 01:49:25 +08:00
|
|
|
return dep;
|
|
|
|
} else {
|
2018-08-03 15:20:23 +08:00
|
|
|
const dep = new Dep(
|
2018-02-25 09:00:20 +08:00
|
|
|
Object.assign(
|
|
|
|
{
|
|
|
|
request: options.exprContextRequest,
|
|
|
|
recursive: options.exprContextRecursive,
|
|
|
|
regExp: options.exprContextRegExp,
|
|
|
|
mode: "sync"
|
|
|
|
},
|
|
|
|
contextOptions
|
|
|
|
),
|
|
|
|
range,
|
|
|
|
param.range
|
|
|
|
);
|
2013-02-13 21:42:34 +08:00
|
|
|
dep.loc = expr.loc;
|
2018-02-25 09:00:20 +08:00
|
|
|
dep.critical =
|
|
|
|
options.exprContextCritical &&
|
|
|
|
"the request of a dependency is an expression";
|
2018-10-30 19:39:43 +08:00
|
|
|
|
|
|
|
if (parser) {
|
|
|
|
parser.walkExpression(param.expression);
|
|
|
|
}
|
|
|
|
|
2013-02-13 21:42:34 +08:00
|
|
|
return dep;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
};
|