Split syntax and transform into two plugins
This commit is contained in:
parent
2a496890ff
commit
30ee87159d
3
packages/babel-plugin-syntax-optional-chaining/.gitignore
vendored
Normal file
3
packages/babel-plugin-syntax-optional-chaining/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
node_modules
|
||||||
|
*.log
|
||||||
|
src
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
src
|
||||||
|
test
|
||||||
|
*.log
|
||||||
35
packages/babel-plugin-syntax-optional-chaining/README.md
Normal file
35
packages/babel-plugin-syntax-optional-chaining/README.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# babel-plugin-syntax-optional-chaining
|
||||||
|
|
||||||
|
Allow parsing of optional properties.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install --save-dev babel-plugin-syntax-optional-chaining
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Via `.babelrc` (Recommended)
|
||||||
|
|
||||||
|
**.babelrc**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": ["syntax-optional-chaining"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Via CLI
|
||||||
|
|
||||||
|
```sh
|
||||||
|
babel --plugins syntax-optional-chaining script.js
|
||||||
|
```
|
||||||
|
|
||||||
|
### Via Node API
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
require("babel-core").transform("code", {
|
||||||
|
plugins: ["syntax-optional-chaining"]
|
||||||
|
});
|
||||||
|
```
|
||||||
7
packages/babel-plugin-syntax-optional-chaining/index.js
Normal file
7
packages/babel-plugin-syntax-optional-chaining/index.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export default function () {
|
||||||
|
return {
|
||||||
|
manipulateOptions(opts, parserOpts) {
|
||||||
|
parserOpts.plugins.push("optionalChaining");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
13
packages/babel-plugin-syntax-optional-chaining/package.json
Normal file
13
packages/babel-plugin-syntax-optional-chaining/package.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "babel-plugin-syntax-optional-chaining",
|
||||||
|
"version": "7.0.0-alpha.12",
|
||||||
|
"description": "Allow parsing of optional properties",
|
||||||
|
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-optional-chaining",
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"keywords": [
|
||||||
|
"babel-plugin"
|
||||||
|
],
|
||||||
|
"dependencies": {},
|
||||||
|
"devDependencies": {}
|
||||||
|
}
|
||||||
@ -6,10 +6,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-traverse": "7.0.0-alpha.7",
|
"babel-plugin-syntax-optional-chaining": "7.0.0-alpha.12"
|
||||||
"babel-types": "7.0.0-alpha.7",
|
|
||||||
"babel-template": "7.0.0-alpha.7",
|
|
||||||
"lodash": "^4.2.0"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"babel-plugin"
|
"babel-plugin"
|
||||||
|
|||||||
@ -1,105 +1,70 @@
|
|||||||
|
import syntaxOptionalChaining from "babel-plugin-syntax-optional-chaining";
|
||||||
|
|
||||||
export default function ({ types: t }) {
|
export default function ({ types: t }) {
|
||||||
// DO NOT SUBMIT. This is until the parser is complete
|
return {
|
||||||
const fixer = {
|
inherits: syntaxOptionalChaining,
|
||||||
NewExpression: {
|
|
||||||
exit(path) {
|
|
||||||
const { callee } = path.node;
|
|
||||||
if (t.isCallExpression(callee)) {
|
|
||||||
const replacement = t.newExpression(callee.callee, callee.arguments);
|
|
||||||
replacement.optional = true;
|
|
||||||
path.replaceWith(replacement);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
CallExpression(path) {
|
visitor: {
|
||||||
const { node } = path;
|
MemberExpression(path) {
|
||||||
if (!node.optional || node.callee) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const callee = t.clone(node.arguments[0]);
|
|
||||||
if (t.isMemberExpression(callee)) {
|
|
||||||
callee.optional = node.arguments[1].value;
|
|
||||||
}
|
|
||||||
node.callee = callee;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// END DO NOT SUBMIT
|
|
||||||
|
|
||||||
const visitor = {
|
|
||||||
Program(path) {
|
|
||||||
path.traverse(fixer);
|
|
||||||
},
|
|
||||||
|
|
||||||
MemberExpression(path) {
|
|
||||||
if (!path.node.optional) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { scope, node } = path;
|
|
||||||
const { object } = node;
|
|
||||||
|
|
||||||
node.optional = false;
|
|
||||||
|
|
||||||
const ref = scope.generateUidIdentifierBasedOnNode(object);
|
|
||||||
scope.push({ id: ref });
|
|
||||||
node.object = ref;
|
|
||||||
|
|
||||||
let parent = path;
|
|
||||||
let expression = path;
|
|
||||||
while (parent.listKey === undefined) {
|
|
||||||
expression = parent;
|
|
||||||
parent = parent.parentPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
const replace = parent.isExpression() ? parent : expression;
|
|
||||||
replace.replaceWith(t.conditionalExpression(
|
|
||||||
t.binaryExpression("==", t.assignmentExpression("=", ref, object), t.nullLiteral()),
|
|
||||||
scope.buildUndefinedNode(),
|
|
||||||
replace.node
|
|
||||||
));
|
|
||||||
},
|
|
||||||
|
|
||||||
"NewExpression|CallExpression": {
|
|
||||||
exit(path) {
|
|
||||||
if (!path.node.optional) {
|
if (!path.node.optional) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { scope, node } = path;
|
const { scope, node } = path;
|
||||||
const { callee } = node;
|
const { object } = node;
|
||||||
|
|
||||||
node.optional = false;
|
node.optional = false;
|
||||||
|
|
||||||
const ref = scope.generateUidIdentifierBasedOnNode(callee);
|
const ref = scope.generateUidIdentifierBasedOnNode(object);
|
||||||
scope.push({ id: ref });
|
scope.push({ id: ref });
|
||||||
node.callee = ref;
|
node.object = ref;
|
||||||
|
|
||||||
if (t.isMemberExpression(callee) && !t.isNewExpression(node)) {
|
let parent = path;
|
||||||
const context = scope.generateUidIdentifierBasedOnNode(callee.object);
|
let expression = path;
|
||||||
scope.push({ id: context });
|
while (parent.listKey === undefined) {
|
||||||
callee.object = t.assignmentExpression("=", context, callee.object);
|
expression = parent;
|
||||||
|
parent = parent.parentPath;
|
||||||
node.arguments.unshift(context);
|
|
||||||
node.callee = t.memberExpression(node.callee, t.identifier("call"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
path.replaceWith(t.conditionalExpression(
|
const replace = parent.isExpression() ? parent : expression;
|
||||||
t.binaryExpression("==", t.assignmentExpression("=", ref, callee), t.nullLiteral()),
|
replace.replaceWith(t.conditionalExpression(
|
||||||
|
t.binaryExpression("==", t.assignmentExpression("=", ref, object), t.nullLiteral()),
|
||||||
scope.buildUndefinedNode(),
|
scope.buildUndefinedNode(),
|
||||||
node
|
replace.node
|
||||||
));
|
));
|
||||||
},
|
},
|
||||||
},
|
|
||||||
|
|
||||||
};
|
"NewExpression|CallExpression": {
|
||||||
|
exit(path) {
|
||||||
|
if (!path.node.optional) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
const { scope, node } = path;
|
||||||
visitor,
|
const { callee } = node;
|
||||||
|
|
||||||
manipulateOptions(opts, parserOpts) {
|
node.optional = false;
|
||||||
parserOpts.plugins.push("optionalChaining");
|
|
||||||
|
const ref = scope.generateUidIdentifierBasedOnNode(callee);
|
||||||
|
scope.push({ id: ref });
|
||||||
|
node.callee = ref;
|
||||||
|
|
||||||
|
if (t.isMemberExpression(callee) && !t.isNewExpression(node)) {
|
||||||
|
const context = scope.generateUidIdentifierBasedOnNode(callee.object);
|
||||||
|
scope.push({ id: context });
|
||||||
|
callee.object = t.assignmentExpression("=", context, callee.object);
|
||||||
|
|
||||||
|
node.arguments.unshift(context);
|
||||||
|
node.callee = t.memberExpression(node.callee, t.identifier("call"));
|
||||||
|
}
|
||||||
|
|
||||||
|
path.replaceWith(t.conditionalExpression(
|
||||||
|
t.binaryExpression("==", t.assignmentExpression("=", ref, callee), t.nullLiteral()),
|
||||||
|
scope.buildUndefinedNode(),
|
||||||
|
node
|
||||||
|
));
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user