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

Error: Cannot find symbol for node: replace while using react-router-dom #229

Closed
jinzhubaofu opened this issue Nov 18, 2022 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@jinzhubaofu
Copy link

Bug report

Input code

input file

import type { Params } from 'react-router-dom';

export type Route = {
  pattern: string;
  path: string;
  component: string;
  params?: Params;
};

export const a = 1;

Expected output

import { Params } from 'react-router-dom';

export declare type Route = {
	pattern: string;
	path: string;
	component: string;
	params?: Params;
};
export declare const a = 1;

export {};

Actual output

error thrown with message:

Error: Cannot find symbol for node: replace
      at TypesUsageEvaluator.getSymbol (src/types-usage-evaluator.ts:128:10)
      at TypesUsageEvaluator.computeUsagesRecursively (src/types-usage-evaluator.ts:107:52)
      at TypesUsageEvaluator.computeUsageForNode (src/types-usage-evaluator.ts:72:9)
      at visitNodes (node_modules/typescript/lib/typescript.js:31214:30)
      at forEachChildInSourceFile (node_modules/typescript/lib/typescript.js:31573:20)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:31988:47)
      at TypesUsageEvaluator.computeUsages (src/types-usage-evaluator.ts:60:7)
      at new TypesUsageEvaluator (src/types-usage-evaluator.ts:18:8)
      at generateDtsBundle (src/bundle-generator.ts:149:30)
      at Context.<anonymous> (tests/e2e/all-test-cases.ts:68:39)
      at processImmediate (node:internal/timers:464:21)

error thrown from here:

private getSymbol(node: ts.Node): ts.Symbol {
    const nodeSymbol = this.typeChecker.getSymbolAtLocation(node); 
   if (nodeSymbol === undefined) {
	throw new Error(`Cannot find symbol for node: ${node.getText()}`);  // <---- this line
    }

   return this.getActualSymbol(nodeSymbol);
}

I have no idea why getSymblAtLocation return undefined under these conditions.

to re-produce the error, the following code is key-point: in the react-router-dom,

export declare function useLinkClickHandler<E extends Element = HTMLAnchorElement>(to: To, { target, replace: replaceProp, state, }?: {
    target?: React.HTMLAttributeAnchorTarget;
    replace?: boolean;
    state?: any;
}): (event: React.MouseEvent<E, MouseEvent>) => void;
  1. the second parameter must have the question mark ?
  2. replace rename to replaceProp

to fix this, maybe try to avoid put BindingElement's propertyName into the queue, for example:

private computeUsagesRecursively(parent: ts.Node, parentSymbol: ts.Symbol): void {
  // ...
  if (ts.isIdentifier(child)) {
	// identifiers in labelled tuples don't have symbols for their labels
	// so let's just skip them from collecting
	if (ts.isNamedTupleMember(child.parent) && child.parent.name === child) {
	    continue;
	}
        // ADD THIS: do not put BindingElement( {a: b} )'s propertyName into queue.
	if (ts.isBindingElement(child.parent) && child === child.parent.propertyName) {
		continue;
	}
  }
  // ...
}

Please take a look and expecting replies.
@jinzhubaofu
Copy link
Author

You can re-produce the error this this demo project: https://github.com/jinzhubaofu/dts-bundle-generator-demo

@timocov timocov added the Bug label Nov 21, 2022
@timocov timocov added this to the 7.1 milestone Nov 21, 2022
@timocov timocov self-assigned this Nov 21, 2022
@timocov
Copy link
Owner

timocov commented Nov 21, 2022

Many thanks @jinzhubaofu for reporting this and for provided fix. I mentioned you as co-author in 2e1b1af. The fix is released in v7.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants