-
Notifications
You must be signed in to change notification settings - Fork 84
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
Conversation
🦋 Changeset detectedLatest commit: de34d8c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Codecov Report
@@ 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
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this 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()) |
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// I wish we could use node@18's fetch... |
not really since we have to support node14 and node16
There was a problem hiding this comment.
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.
📖 What's in there?
Here is the first stone of the feature detector package: detecting exported function with an edge signature.
This PR includes:
feature-detector
packagehasEdgeSignature(filePath: string): boolean
function, fully tested.🧪 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:
pnpm --filter feature-detector build
test.js
(or .ts)node
REPL, and try it: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
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.