Add placeholders support to @babel/types and @babel/generator (#9542)

This commit is contained in:
Nicolò Ribaudo
2019-03-07 11:47:39 +01:00
committed by GitHub
parent 095e5c0e09
commit 15dfce33df
15 changed files with 302 additions and 20 deletions

View File

@@ -1,14 +1,37 @@
"use strict";
const definitions = require("../../lib/definitions");
const has = Function.call.bind(Object.prototype.hasOwnProperty);
function joinComparisons(leftArr, right) {
return (
leftArr.map(JSON.stringify).join(` === ${right} || `) + ` === ${right}`
);
}
function addIsHelper(type, aliasKeys, deprecated) {
const targetType = JSON.stringify(type);
let aliasSource = "";
if (aliasKeys) {
aliasSource =
" || " +
aliasKeys.map(JSON.stringify).join(" === nodeType || ") +
" === nodeType";
aliasSource = " || " + joinComparisons(aliasKeys, "nodeType");
}
let placeholderSource = "";
const placeholderTypes = [];
if (
definitions.PLACEHOLDERS.includes(type) &&
has(definitions.FLIPPED_ALIAS_KEYS, type)
) {
placeholderTypes.push(type);
}
if (has(definitions.PLACEHOLDERS_FLIPPED_ALIAS, type)) {
placeholderTypes.push(...definitions.PLACEHOLDERS_FLIPPED_ALIAS[type]);
}
if (placeholderTypes.length > 0) {
placeholderSource =
' || nodeType === "Placeholder" && (' +
joinComparisons(placeholderTypes, "node.expectedNode") +
")";
}
return `export function is${type}(node: ?Object, opts?: Object): boolean {
@@ -16,7 +39,7 @@ function addIsHelper(type, aliasKeys, deprecated) {
if (!node) return false;
const nodeType = node.type;
if (nodeType === ${targetType}${aliasSource}) {
if (nodeType === ${targetType}${aliasSource}${placeholderSource}) {
if (typeof opts === "undefined") {
return true;
} else {