Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -651,18 +651,69 @@ function isMemoCallback(path: NodePath<t.Expression>): boolean {
);
}

function isValidPropsAnnotation(
annot: t.TypeAnnotation | t.TSTypeAnnotation | t.Noop | null | undefined
): boolean {
if (annot == null) {
return true;
} else if (annot.type === "TSTypeAnnotation") {
switch (annot.typeAnnotation.type) {
case "TSArrayType":
case "TSBigIntKeyword":
case "TSBooleanKeyword":
case "TSConstructorType":
case "TSFunctionType":
case "TSLiteralType":
case "TSNeverKeyword":
case "TSNumberKeyword":
case "TSStringKeyword":
case "TSSymbolKeyword":
case "TSTupleType":
return false;
}
return true;
} else if (annot.type === "TypeAnnotation") {
switch (annot.typeAnnotation.type) {
case "ArrayTypeAnnotation":
case "BooleanLiteralTypeAnnotation":
case "BooleanTypeAnnotation":
case "EmptyTypeAnnotation":
case "FunctionTypeAnnotation":
case "NumberLiteralTypeAnnotation":
case "NumberTypeAnnotation":
case "StringLiteralTypeAnnotation":
case "StringTypeAnnotation":
case "SymbolTypeAnnotation":
case "ThisTypeAnnotation":
case "TupleTypeAnnotation":
return false;
}
return true;
} else if (annot.type === "Noop") {
return true;
} else {
assertExhaustive(annot, `Unexpected annotation node \`${annot}\``);
}
}

function isValidComponentParams(
params: Array<NodePath<t.Identifier | t.Pattern | t.RestElement>>
): boolean {
if (params.length === 0) {
return true;
} else if (params.length === 1) {
return !params[0].isRestElement();
return (
isValidPropsAnnotation(params[0].node.typeAnnotation) &&
!params[0].isRestElement()
);
} else if (params.length === 2) {
// check if second param might be a ref
if (params[1].isIdentifier()) {
const { name } = params[1].node;
return name.includes("ref") || name.includes("Ref");
return (
isValidPropsAnnotation(params[0].node.typeAnnotation) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can imagine wanting something different here over time, but given the current logic of the method it makes sense to share. maybe rename to clarify? isValidPropsOrRefAnnotation()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annotation check is still applying just to the props in both cases, actually--I've refactored the logic here to make that clearer.

(name.includes("ref") || name.includes("Ref"))
);
}
/**
* Otherwise, avoid helper functions that take more than one argument.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

## Input

```javascript
// @compilationMode(infer)
import { useIdentity, identity } from "shared-runtime";

function Component(fakeProps: number) {
const x = useIdentity(fakeProps);
return identity(x);
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [42],
};

```

## Code

```javascript
// @compilationMode(infer)
import { useIdentity, identity } from "shared-runtime";

function Component(fakeProps: number) {
const x = useIdentity(fakeProps);
return identity(x);
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [42],
};

```

### Eval output
(kind: ok) 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @compilationMode(infer)
import { useIdentity, identity } from "shared-runtime";

function Component(fakeProps: number) {
const x = useIdentity(fakeProps);
return identity(x);
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [42],
};