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

export types.BaseNode for generic utility use #12600

Closed
wants to merge 1 commit into from

Conversation

kentcdodds
Copy link
Member

@kentcdodds kentcdodds commented Jan 9, 2021

Q                       A
Fixed Issues? N/A
Patch: Bug Fix? N/A
Major: Breaking Change? N/A
Minor: New Feature? 👍
Tests Added + Pass? N/A
Documentation PR Link N/A
Any Dependency Changes? No
License MIT

I'm trying to write a utility that accepts a NodePath of any BaseNode type and does stuff with it (replaces etc.). So I need to type that utility with BaseNode, but that's not exported yet so this simply exports the BaseNode.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 9, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 55e1a6b:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jan 9, 2021

Does NodePath<Node> work for you, as opposed to NodePath<BaseNode>?

@kentcdodds
Copy link
Member Author

It didn't when I tried. I think it's because Node isn't in the inheritance chain or something 🤔

@nicolo-ribaudo
Copy link
Member

Is that code public? I have nothing against exposing BaseNode if it's needed, but I am very surprised that Node does not work since it's the union of all the possible nodes.

@kentcdodds
Copy link
Member Author

Yeah, mauve I did something wrong, but using Node here didn't work: https://github.com/kentcdodds/babel-plugin-codegen/blob/pr/migrate-to-ts/src/helpers.ts#L85

@nicolo-ribaudo
Copy link
Member

Ok I see the error:

src/replace.ts:55:9 - error TS2322: Type 'NodePath<ImportDeclaration>' is not assignable to type 'NodePath<Node>'.
  Type 'Node' is not assignable to type 'ImportDeclaration'.

I tried to export BaseNode in node_modules/@babel/types and I still get

src/replace.ts:55:9 - error TS2322: Type 'NodePath<ImportDeclaration>' is not assignable to type 'NodePath<BaseNode>'.
  Type 'BaseNode' is missing the following properties from type 'ImportDeclaration': specifiers, source

I think it has to do with covariance/contravariance of type parameters (I don't know exactly what it means), but I fixed the type errors like this:

diff --git a/src/helpers.ts b/src/helpers.ts
index c813b8f..0701b03 100644
--- a/src/helpers.ts
+++ b/src/helpers.ts
@@ -63,9 +63,9 @@ function getReplacement(
   })()
 }
 
-function applyReplacementToPath(
+function applyReplacementToPath<SpecificNode extends babelCore.types.Node>(
   replacement: babelCore.Node | Array<babelCore.Node> | null | undefined,
-  path: babelCore.NodePath,
+  path: babelCore.NodePath<SpecificNode>,
 ) {
   if (replacement) {
     // If it's not an array, wrap into an array
@@ -79,16 +79,16 @@ function applyReplacementToPath(
   }
 }
 
-type ReplaceOptions = {
+type ReplaceOptions<SpecificNode extends babelCore.types.Node> = {
   // https://github.com/babel/babel/pull/12600
   // path: babelCore.NodePath<babelCore.types.BaseNode>
-  path: babelCore.NodePath<any>
+  path: babelCore.NodePath<SpecificNode>
   code: string | Buffer
   fileOpts: babelCore.TransformOptions
   args?: Array<any>
 }
-function replace(
-  {path, code, fileOpts, args}: ReplaceOptions,
+function replace<SpecificNode extends babelCore.types.Node>(
+  {path, code, fileOpts, args}: ReplaceOptions<SpecificNode>,
   babel: typeof babelCore,
 ) {
   const replacement = getReplacement({code, args, fileOpts}, babel)

@kentcdodds
Copy link
Member Author

That worked. Thank you for taking the time @nicolo-ribaudo! Cheers :)

@kentcdodds kentcdodds closed this Jan 12, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the pr/export-base-node branch January 12, 2021 01:29
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 13, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: types
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants