flowgen
Version:
Generate flowtype definition files from TypeScript
136 lines (104 loc) • 5.19 kB
JavaScript
exports.__esModule = true;
exports.getLeftMostEntityName = getLeftMostEntityName;
exports.renames = renames;
var ts = _interopRequireWildcard(require("typescript"));
var _checker = require("../checker");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const setImportedName = (name, type, symbol, decl) => {
var _symbol$parent, _decl$parent, _decl$parent$parent, _decl$parent$parent$p, _decl$parent$parent$p2;
const specifiers = ["react"];
const namespaces = ["React"];
const paths = name => {
if (name === "react" || name === "React") {
return {
ReactNode: "Node",
ReactElement: "Element"
};
}
return {};
}; // @ts-expect-error todo(flow->ts)
if (namespaces.includes((_symbol$parent = symbol.parent) == null ? void 0 : _symbol$parent.escapedName)) {
var _symbol$parent2;
// @ts-expect-error todo(flow->ts)
type.escapedText = paths((_symbol$parent2 = symbol.parent) == null ? void 0 : _symbol$parent2.escapedName)[name] || name;
return true;
} else if ( // @ts-expect-error todo(flow->ts)
specifiers.includes((_decl$parent = decl.parent) == null ? void 0 : (_decl$parent$parent = _decl$parent.parent) == null ? void 0 : (_decl$parent$parent$p = _decl$parent$parent.parent) == null ? void 0 : (_decl$parent$parent$p2 = _decl$parent$parent$p.moduleSpecifier) == null ? void 0 : _decl$parent$parent$p2.text)) {
type.escapedText = // @ts-expect-error todo(flow->ts)
paths(decl.parent.parent.parent.moduleSpecifier.text)[name] || name;
return true;
}
return false;
};
const setGlobalName = (type, _symbol) => {
const globals = [{
from: ts.createQualifiedName(ts.createIdentifier("JSX"), "Element"),
to: ts.createIdentifier("React$Node")
}];
if (_checker.checker.current) {
const bools = [];
for (const {
from,
to
} of globals) {
if (ts.isQualifiedName(type.typeName) && compareQualifiedName(type.typeName, from)) {
// @ts-expect-error readonly property, but we write to it
type.typeName = to;
bools.push(true);
}
}
return bools.length > 0;
}
return false;
};
function renames(symbol, type) {
if (!symbol) return false;
if (!symbol.declarations) return false;
const decl = symbol.declarations[0];
if (ts.isImportSpecifier(type)) {
// @ts-expect-error todo(flow->ts)
setImportedName(decl.name.escapedText, decl.name, symbol, decl);
} else if (type.kind === ts.SyntaxKind.TypeReference) {
const leftMost = getLeftMostEntityName(type.typeName);
if (leftMost && _checker.checker.current) {
var _leftMostSymbol$paren;
const leftMostSymbol = _checker.checker.current.getSymbolAtLocation(leftMost);
const isGlobal = (leftMostSymbol == null ? void 0 : (_leftMostSymbol$paren = leftMostSymbol.parent) == null ? void 0 : _leftMostSymbol$paren.escapedName) === "__global";
if (isGlobal) {
return setGlobalName(type, symbol);
}
}
if (ts.isQualifiedName(type.typeName)) {
return setImportedName(symbol.escapedName, type.typeName.right, symbol, decl);
} else {
return setImportedName(symbol.escapedName, type.typeName, symbol, decl);
}
}
return false;
}
function getLeftMostEntityName(type) {
if (type.kind === ts.SyntaxKind.QualifiedName) {
return type.left.kind === ts.SyntaxKind.Identifier ? type.left : getLeftMostEntityName(type.left);
} else if (type.kind === ts.SyntaxKind.Identifier) {
return type;
}
}
function compareIdentifier(a, b) {
if (a.kind !== b.kind) return false;
if (a.escapedText === b.escapedText && a.text === b.text) return true;
return false;
}
function compareEntityName(a, b) {
if (a.kind === ts.SyntaxKind.Identifier && b.kind === ts.SyntaxKind.Identifier) {
return compareIdentifier(a, b);
}
if (a.kind === ts.SyntaxKind.QualifiedName && b.kind === ts.SyntaxKind.QualifiedName) {
return compareQualifiedName(a, b);
}
return false;
}
function compareQualifiedName(a, b) {
return compareEntityName(a.left, b.left) && compareIdentifier(a.right, b.right);
}
;