Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional chaining and nullish coalescing support #11198

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/ConstPlugin.js
Expand Up @@ -314,6 +314,45 @@ class ConstPlugin {
dep.loc = expression.loc;
parser.state.module.addPresentationalDependency(dep);
}
return keepRight;
}
} else if (expression.operator === "??") {
const param = parser.evaluateExpression(expression.left);
const keepRight = param && param.asNullish();
if (typeof keepRight === "boolean") {
// ------------------------------------------
//
// Given the following code:
//
// (falsy || truthy) ?? someExpression();
//
// the generated code is:
//
// (falsy || truthy) ?? false;
//
// ------------------------------------------
//
// Given the following code:
//
// nullable ?? someExpression();
//
// the generated code is:
//
// null ?? someExpression();
//
if (keepRight) {
const dep = new ConstDependency("null", param.range);
dep.loc = expression.loc;
parser.state.module.addPresentationalDependency(dep);
} else {
const dep = new ConstDependency(
"false",
expression.right.range
);
dep.loc = expression.loc;
parser.state.module.addPresentationalDependency(dep);
}

return keepRight;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/CommonJsPlugin.js
Expand Up @@ -199,7 +199,7 @@ class CommonJsPlugin {

parser.hooks.evaluateIdentifier.for("module.hot").tap(
"CommonJsPlugin",
evaluateToIdentifier("module.hot", "module", () => ["hot"], false)
evaluateToIdentifier("module.hot", "module", () => ["hot"], null)
);

new CommonJsImportsParserPlugin(options).apply(parser);
Expand Down
63 changes: 62 additions & 1 deletion lib/javascript/BasicEvaluatedExpression.js
Expand Up @@ -26,6 +26,7 @@ class BasicEvaluatedExpression {
this.range = undefined;
this.falsy = false;
this.truthy = false;
this.nullish = undefined;
this.bool = undefined;
this.number = undefined;
this.bigint = undefined;
Expand Down Expand Up @@ -97,6 +98,37 @@ class BasicEvaluatedExpression {
return this.type === TypeTemplateString;
}

/**
* check for "simple" types (inlined) only
* @returns {boolean} is simple type
*/
isSimpleType() {
switch (this.type) {
case TypeIdentifier:
case TypeConditional:
case TypeWrapped:
case TypeUnknown:
return false;
default:
return true;
}
}

/**
* @param {BasicEvaluatedExpression} basicEvaluatedExpression basicEvaluatedExpression
* @returns {boolean|undefined} is same type
*/
isSameType(basicEvaluatedExpression) {
if (
this.type === TypeUnknown ||
basicEvaluatedExpression.type === TypeUnknown
) {
return undefined;
}

return this.type === basicEvaluatedExpression.type;
}

isTruthy() {
return this.truthy;
}
Expand All @@ -105,9 +137,13 @@ class BasicEvaluatedExpression {
return this.falsy;
}

isNullish() {
return this.nullish;
}

asBool() {
if (this.truthy) return true;
if (this.falsy) return false;
if (this.falsy || this.nullish) return false;
if (this.isBoolean()) return this.bool;
if (this.isNull()) return false;
if (this.isUndefined()) return false;
Expand All @@ -130,6 +166,26 @@ class BasicEvaluatedExpression {
return undefined;
}

asNullish() {
const nullish = this.isNullish();

if (nullish === true || this.isNull() || this.isUndefined()) return true;

if (nullish === false) return false;
if (this.isTruthy()) return false;
if (this.isBoolean()) return false;
if (this.isString()) return false;
if (this.isNumber()) return false;
if (this.isBigInt()) return false;
if (this.isRegExp()) return false;
if (this.isArray()) return false;
if (this.isConstArray()) return false;
if (this.isTemplateString()) return false;
if (this.isRegExp()) return false;

return undefined;
}

asString() {
if (this.isBoolean()) return `${this.bool}`;
if (this.isNull()) return "null";
Expand Down Expand Up @@ -265,6 +321,11 @@ class BasicEvaluatedExpression {
return this;
}

setNullish(value) {
this.nullish = value;
return this;
}

setRange(range) {
this.range = range;
return this;
Expand Down