feat: optional chaing
This commit is contained in:
parent
5cc1cbf3bc
commit
00f58b9bfa
@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
@ -0,0 +1 @@
|
||||
# babel-plugin-transform-optional-chaining
|
||||
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-optional-chaining",
|
||||
"version": "7.0.0-alpha.7",
|
||||
"description": "",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-optional-chaining",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"babel-traverse": "7.0.0-alpha.7",
|
||||
"babel-types": "7.0.0-alpha.7",
|
||||
"babel-template": "7.0.0-alpha.7",
|
||||
"lodash": "^4.2.0"
|
||||
},
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"devDependencies": {
|
||||
"babel-helper-plugin-test-runner": "7.0.0-alpha.7"
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
import createTemplate from "babel-template";
|
||||
import traverse from "babel-traverse";
|
||||
|
||||
const nullOrUndefinedCheck = createTemplate(`
|
||||
typeof CHECK !== "undefined" && CHECK !== null
|
||||
? NEXT
|
||||
: null
|
||||
`);
|
||||
|
||||
function isOptional(path) {
|
||||
return path.node.optional === true;
|
||||
}
|
||||
|
||||
const nullOrUndefinedCheckVisitor = {
|
||||
noScope: true,
|
||||
|
||||
Identifier(path, replacements) {
|
||||
if (path.node.name in replacements) {
|
||||
path.replaceInline(replacements[path.node.name]);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function createCheck(node, object) {
|
||||
|
||||
const template = nullOrUndefinedCheck();
|
||||
|
||||
traverse(template, nullOrUndefinedCheckVisitor, null, {
|
||||
CHECK: object,
|
||||
NEXT: node,
|
||||
});
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
export default function ({ types: t }) {
|
||||
|
||||
return {
|
||||
visitor: {
|
||||
MemberExpression(path) {
|
||||
|
||||
if (isOptional(path)) {
|
||||
let { object } = path.node;
|
||||
|
||||
while (
|
||||
t.isMemberExpression(object)
|
||||
&& isOptional({ node: object })
|
||||
) {
|
||||
object = createCheck(
|
||||
object,
|
||||
object.object
|
||||
);
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
createCheck(path.node, object)
|
||||
);
|
||||
|
||||
path.stop();
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
foo?.bar
|
||||
@ -0,0 +1 @@
|
||||
typeof foo !== "undefined" && foo !== null ? foo.bar : null;
|
||||
@ -0,0 +1 @@
|
||||
foo?.bar?.vroum
|
||||
@ -0,0 +1 @@
|
||||
typeof (typeof foo !== "undefined" && foo !== null ? foo.bar : null) !== "undefined" && (typeof foo !== "undefined" && foo !== null ? foo.bar : null) !== null ? foo.bar.vroum : null;
|
||||
3
packages/babel-plugin-transform-optional-chaining/test/fixtures/general/options.json
vendored
Normal file
3
packages/babel-plugin-transform-optional-chaining/test/fixtures/general/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-optional-chaining"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
import runner from "babel-helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
Loading…
x
Reference in New Issue
Block a user