Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11221 from webpack/feature/optional-chaining
Feature/optional chaining
- Loading branch information
Showing
15 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
it("should evaluate optional members", () => { | ||
if (!module.hot) { | ||
expect( | ||
module.hot?.accept((() => {throw new Error("fail")})()) | ||
).toBe(undefined); | ||
} | ||
}); | ||
|
||
it("should evaluate optional chaining as a part of statement", () => { | ||
if (module.hot?.accept) { | ||
module.hot?.accept("./a.js"); | ||
} else { | ||
expect(module.hot).toBe(undefined); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const supportsOptionalChaining = require("../../../helpers/supportsOptionalChaining"); | ||
|
||
module.exports = function (config) { | ||
return !config.minimize && supportsOptionalChaining(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
it("should correctly render defined data #1", () => { | ||
expect(_VALUE_?._DEFINED_).toBe(1); | ||
}); | ||
|
||
it("should correctly render defined data #2", () => { | ||
const val1 = _VALUE_?._PROP_?._DEFINED_; | ||
const val2 = _VALUE_?._PROP_?._UNDEFINED_; | ||
const val3 = typeof _VALUE_?._PROP_?._DEFINED_; | ||
const val4 = typeof _VALUE_?._PROP_?._UNDEFINED_; | ||
const val5 = _VALUE_?._PROP_; | ||
const val6 = typeof _VALUE_?._PROP_; | ||
expect(val1).toBe(2); | ||
expect(val2).toBeUndefined(); | ||
expect(val3).toBe("number"); | ||
expect(val4).toBe("undefined"); | ||
expect(val5).toEqual({ _DEFINED_: 2 }); | ||
expect(val6).toBe("object"); | ||
expect((() => typeof _VALUE_?._PROP_?._DEFINED_).toString()).toContain( | ||
"number" | ||
); | ||
expect((() => typeof _VALUE_?._PROP_).toString()).toContain("object"); | ||
if (_VALUE_._PROP_._DEFINED_ !== 2) require("fail"); | ||
if (_VALUE_?._PROP_?._DEFINED_ !== 2) require("fail"); | ||
if (typeof _VALUE_._PROP_._DEFINED_ !== "number") require("fail"); | ||
if (typeof _VALUE_?._PROP_?._DEFINED_ !== "number") require("fail"); | ||
if (typeof _VALUE_._PROP_ !== "object") require("fail"); | ||
if (typeof _VALUE_?._PROP_ !== "object") require("fail"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var supportsOptionalChaining = require("../../../helpers/supportsOptionalChaining"); | ||
|
||
module.exports = function (config) { | ||
return supportsOptionalChaining(); | ||
}; |
18 changes: 18 additions & 0 deletions
18
test/configCases/parsing/optional-chaining/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { DefinePlugin } = require("../../../../"); | ||
|
||
/** @type {import("../../../../").Configuration} */ | ||
module.exports = { | ||
mode: "development", | ||
devtool: false, | ||
target: "web", | ||
plugins: [ | ||
new DefinePlugin({ | ||
_VALUE_: { | ||
_DEFINED_: 1, | ||
_PROP_: { | ||
_DEFINED_: 2 | ||
} | ||
} | ||
}) | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = function supportsOptionalChaining() { | ||
try { | ||
var f = eval("(function f() { return ({a: true}) ?.a })"); | ||
return f(); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default 1; | ||
--- | ||
export default 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import value from "./a"; | ||
|
||
it("should run module.hot.accept(…)", function (done) { | ||
expect(value).toBe(1); | ||
module?.hot?.accept("./a", function () {}); | ||
NEXT( | ||
require("../../update")(done, true, () => { | ||
expect(value).toBe(2); | ||
done(); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters