Skip to content

Commit

Permalink
Handle decorators imported from Realm
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Duncalf committed Oct 11, 2022
1 parent 358466c commit 77286a3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/babel-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ describe("Babel plugin", () => {
expect((parsedSchema?.properties.name as ObjectSchemaProperty).indexed).toEqual(true);
});

it("handles `@index` decorators from the Realm import", () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
export class Person extends Realm.Object { @Realm.index name: Realm.Types.String; }`,
});
const parsedSchema = extractSchema(transformCode);

expect((parsedSchema?.properties.name as ObjectSchemaProperty).indexed).toEqual(true);
});

it("ignores `@index` decorators not imported from `realm`", () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
Expand All @@ -386,6 +396,16 @@ describe("Babel plugin", () => {
expect((parsedSchema?.properties.name as ObjectSchemaProperty).mapTo).toEqual("rename");
});

it("handles `@mapTo` decorators from the Realm import", () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
export class Person extends Realm.Object { @Realm.mapTo('rename') name: Realm.Types.String; }`,
});
const parsedSchema = extractSchema(transformCode);

expect((parsedSchema?.properties.name as ObjectSchemaProperty).mapTo).toEqual("rename");
});

it("ignores `@mapTo` decorators not imported from `realm`", () => {
const transformCode = transform({
source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm";
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-plugin/src/plugin/import-checking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export function isImportedFromRealm(path: NodePath<types.Node>): boolean {
return isImportedFromRealm(path.get("object"));
} else if (path.isTSQualifiedName()) {
return isImportedFromRealm(path.get("left"));
} else if (path.isDecorator() && types.isMemberExpression(path.get("expression"))) {
// Handle decorators with Realm namespace like `@Realm.index`
return isImportedFromRealm(path.get("expression"));
} else if (
path.isDecorator() &&
types.isCallExpression(path.get("expression")) &&
types.isMemberExpression(path.get("expression").get("callee"))
) {
// Handle called decorators with Realm namespace like `@Realm.mapTo('xxx')`
return isImportedFromRealm((path.get("expression") as NodePath<types.CallExpression>).get("callee"));
} else if (path.isIdentifier() || path.isDecorator()) {
const node = path.isDecorator()
? types.isCallExpression(path.node.expression)
Expand Down
27 changes: 24 additions & 3 deletions packages/babel-plugin/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,32 @@ function getRealmTypeForClassProperty(path: NodePath<types.ClassProperty>): Real
}
}

function isRealmDecoratorWithName(path: any, name: string): boolean {
return (
(types.isIdentifier(path) && path.name === name) ||
(types.isMemberExpression(path) &&
types.isIdentifier(path.object) &&
path.object.name === "Realm" &&
types.isIdentifier(path.property) &&
path.property.name === name)
);
}

function findDecoratorIdentifier(
decoratorsPath: NodePath<types.Decorator>[],
name: string,
): NodePath<types.Decorator> | undefined {
return decoratorsPath.find(
(d) => d.node && types.isIdentifier(d.node.expression) && d.node.expression.name === name && isImportedFromRealm(d),
(d) =>
d.node &&
isRealmDecoratorWithName(d.node.expression, name) &&
// ((types.isIdentifier(d.node.expression) && d.node.expression.name === name) ||
// (types.isMemberExpression(d.node.expression) &&
// types.isIdentifier(d.node.expression.object) &&
// d.node.expression.object.name === "Realm" &&
// types.isIdentifier(d.node.expression.property) &&
// d.node.expression.property.name === name)) &&
isImportedFromRealm(d),
);
}

Expand All @@ -242,8 +262,9 @@ function findDecoratorCall(
(d) =>
d.node &&
types.isCallExpression(d.node.expression) &&
types.isIdentifier(d.node.expression.callee) &&
d.node.expression.callee.name === name &&
isRealmDecoratorWithName(d.node.expression.callee, name) &&
// types.isIdentifier(d.node.expression.callee) &&
// d.node.expression.callee.name === name &&
isImportedFromRealm(d),
);

Expand Down

0 comments on commit 77286a3

Please sign in to comment.