@realm/babel-plugin
Version:
Babel plugin making it easier to declare your Realm schema
64 lines (63 loc) • 2.88 kB
JavaScript
"use strict";
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPropertyImportedFromRealm = exports.isImportedFromRealm = void 0;
const core_1 = require("@babel/core");
// TODO: Merge the functions below into the functions above
function isImportedFromRealm(path) {
if (path.isMemberExpression()) {
return isImportedFromRealm(path.get("object"));
}
else if (path.isTSQualifiedName()) {
return isImportedFromRealm(path.get("left"));
}
else if (path.isDecorator() && core_1.types.isMemberExpression(path.get("expression"))) {
// Handle decorators with Realm namespace like `@Realm.index`
return isImportedFromRealm(path.get("expression"));
}
else if (path.isDecorator() &&
core_1.types.isCallExpression(path.get("expression")) &&
core_1.types.isMemberExpression(path.get("expression").get("callee"))) {
// Handle called decorators with Realm namespace like `@Realm.mapTo('xxx')`
return isImportedFromRealm(path.get("expression").get("callee"));
}
else if (path.isIdentifier() || path.isDecorator()) {
const node = path.isDecorator()
? core_1.types.isCallExpression(path.node.expression)
? path.node.expression.callee
: path.node.expression
: path.node;
if (!core_1.types.isIdentifier(node))
return false;
const binding = path.scope.getBinding(node.name);
if (binding && binding.path.parentPath && binding.path.parentPath.isImportDeclaration()) {
return (binding.path.parentPath.get("source").isStringLiteral({ value: "realm" }) ||
binding.path.parentPath.get("source").isStringLiteral({ value: "@realm/react" }));
}
}
return false;
}
exports.isImportedFromRealm = isImportedFromRealm;
function isPropertyImportedFromRealm(path, name) {
if (path.isMemberExpression()) {
return isImportedFromRealm(path.get("object")) && path.get("property").isIdentifier({ name });
}
return false;
}
exports.isPropertyImportedFromRealm = isPropertyImportedFromRealm;