Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(feature-detector): introduces edge signature detection #226

Merged
merged 7 commits into from
Jan 9, 2023

Conversation

feugy
Copy link
Member

@feugy feugy commented Jan 6, 2023

📖 What's in there?

Here is the first stone of the feature detector package: detecting exported function with an edge signature.
This PR includes:

  • the new feature-detector package
  • the hasEdgeSignature(filePath: string): boolean function, fully tested.
  • documentation page (giant WIP with a useful example).
  • README, redirecting to the documentation page.

🧪 How to test?

The package is fully covered with unit tests: pnpm --filter feature-detector test -- --coverage

You can also try it out on a real file:

  1. pnpm --filter feature-detector build
  2. Create a file named test.js (or .ts)
  3. start node REPL, and try it:
    > const { hasEdgeSignature } = require('./packages/feature-detector/dist')
    undefined
    > hasEdgeSignature('./test.js')
    false
  4. change the file content and check its compliance with hasEdgeSignature() again.

Documentation can be seen at http://localhost:3000/packages/feature-detector after starting the doc site with pnpm --filter docs dev
You can also review it here

:this: Notes to reviewers

Because Response.json() static method is not supported in Typescript default types (lib.dom.d.ts), I had to overload default types with an augmented definition.

This implies redefining Response, but also discarding all other default types with /// <reference no-default-lib="true"/>.
As a consequence, we're loosing Promise, fetch(), and all other build-in types...

Hence why I had to download the entire es2022 + DOM type definitions, crunch them in a file (with no comments to make it lighter), and load it into the temporary ts-morph project used to analyze user code.

@feugy feugy requested a review from a team January 6, 2023 10:26
@changeset-bot
Copy link

changeset-bot bot commented Jan 6, 2023

🦋 Changeset detected

Latest commit: de34d8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@edge-runtime/feature-detector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jan 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
edge-runtime ✅ Ready (Inspect) Visit Preview Jan 9, 2023 at 1:37PM (UTC)

@codecov-commenter
Copy link

codecov-commenter commented Jan 6, 2023

Codecov Report

Merging #226 (3fbfdc4) into main (eef6b34) will increase coverage by 13.46%.
The diff coverage is 95.37%.

@@             Coverage Diff             @@
##             main     #226       +/-   ##
===========================================
+ Coverage   81.98%   95.45%   +13.46%     
===========================================
  Files          34        7       -27     
  Lines        2326      110     -2216     
  Branches      191        7      -184     
===========================================
- Hits         1907      105     -1802     
+ Misses        400        4      -396     
+ Partials       19        1       -18     
Impacted Files Coverage Δ
packages/feature-detector/src/utils/functions.ts 92.00% <92.00%> (ø)
packages/feature-detector/src/utils/types.ts 92.30% <92.30%> (ø)
packages/feature-detector/src/utils/project.ts 94.11% <94.11%> (ø)
...ackages/feature-detector/src/has-edge-signature.ts 100.00% <100.00%> (ø)
packages/feature-detector/src/index.ts 100.00% <100.00%> (ø)
packages/feature-detector/src/utils/exports.ts 100.00% <100.00%> (ø)
packages/runtime/src/cli/eval.ts
packages/node-utils/src/node-to-edge/index.ts
packages/node-utils/src/edge-to-node/handler.ts
packages/vm/src/edge-vm.ts
... and 29 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link

@nuta nuta left a comment

Choose a reason for hiding this comment

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

Great work! 👍 Left a suggestion on a tricky test case.

export function getReturnType(node?: Node): Type | undefined {
switch (node?.getKind()) {
case ExportAssignment:
return getReturnType(node.asKind(ExportAssignment)?.getExpression())
Copy link

@nuta nuta Jan 6, 2023

Choose a reason for hiding this comment

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

question: May I assume that getReturnType won't be in an infinite loop? Is the following code properly rejected?

type A = A;                 // invalid: circular type definition

const exported = () => {    // valid: cyclic references
  const x = {}
  x.next = x
  return x.next;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I added two test cases. For invalid cyclic types, getReturnType() would return any.

For valid cyclic reference, it depends on the language: any for TS, and { next: any } for JS (interesting!).

}

async function fetchTypeDefinition(file: string): Promise<string[]> {
// I wish we could use node@18's fetch...
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// I wish we could use node@18's fetch...

not really since we have to support node14 and node16 ☹️

Copy link
Member Author

Choose a reason for hiding this comment

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

Runtime, yes. But this is a build-time script. And we don't strictly need to build using node16 & node14. We strictly need to test the code and be sure it's running on node16 & node14.

However, to simplify CI, our test job installs deps, builds and runs tests on nodeX version, while we could always install & build on node 18 (and share this artifact between tests runs...), and use nodeX version for testing only.

I don't think it worth the hassle, so I rented and used builtin http utilities instead of undici.

@feugy feugy merged commit 51d071d into main Jan 9, 2023
@feugy feugy deleted the damien/ec-544-build-a-minimal-ts-morph-feature-1 branch January 9, 2023 13:39
@github-actions github-actions bot mentioned this pull request Jan 9, 2023
jridgewell pushed a commit to jridgewell/edge-runtime that referenced this pull request Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants