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

[bug] Circular references break .d.ts building #782

Open
PapiOphidian opened this issue Nov 20, 2022 · 0 comments
Open

[bug] Circular references break .d.ts building #782

PapiOphidian opened this issue Nov 20, 2022 · 0 comments

Comments

@PapiOphidian
Copy link

PapiOphidian commented Nov 20, 2022

In 2 files, I have each other referencing each other directly through the import statement.

The files look a little something like this:

LocalBucket.ts

/**
 * Bucket used for saving ratelimits
 * @protected
 */
class LocalBucket {
	/**
	 * ratelimiter used for ratelimiting requests
	 */
	public ratelimiter: import("./Ratelimiter");
	/**
	 * Key used internally to routify requests
	 */
	public routeKey: string;

	/**
	 * Create a new bucket
	 * @param ratelimiter ratelimiter used for ratelimiting requests
	 * @param routeKey Key used internally to routify requests. Assigned by ratelimiter
	 */
	public constructor(ratelimiter: import("./Ratelimiter"), routeKey: string) {
		this.ratelimiter = ratelimiter;
		this.routeKey = routeKey;
	}
}

export = LocalBucket;

Ratelimiter.ts

import LocalBucket = require("./LocalBucket");

const routeRegex = /\/([a-z-]+)\/(?:\d+)/g;
const reactionsRegex = /\/reactions\/[^/]+/g;
const reactionsUserRegex = /\/reactions\/:id\/[^/]+/g;
const webhooksRegex = /^\/webhooks\/(\d+)\/[A-Za-z0-9-_]+/;
const isMessageEndpointRegex = /\/messages\/:id$/;

/**
 * Ratelimiter used for handling the ratelimits imposed by the rest api
 * @protected
 */
class Ratelimiter {
	/**
	 * An object of Buckets that store rate limit info
	 */
	public buckets: { [routeKey: string]: LocalBucket; } = {};
	
	/**
	 * Queue a rest call to be executed
	 * @param fn function to call once the ratelimit is ready
	 * @param url Endpoint of the request
	 * @param method Http method used by the request
	 */
	public queue(fn: (bucket: import("./LocalBucket")) => any, url: string, method: string) {
		const routeKey = this.routify(url, method);
		if (!this.buckets[routeKey]) this.buckets[routeKey] = new LocalBucket(this, routeKey);
		this.buckets[routeKey].queue(fn);
	}
}

Whenever tsup gets to building the .d.ts, it tries to generate this:

var __Ratelimiter = /*#__PURE__*/Object.freeze({
   __proto__: null,
  get default () { return Ratelimiter; }
});

Although this causes an "Expected a property assignment" error which comes from the rollup dist file.
Here's the full stack:

Error: Expected a property assignment

   6 | var __Ratelimiter = /*#__PURE__*/Object.freeze({
   7 |  __proto__: null,
>  8 |  get default () { return Ratelimiter; }
     |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   9 | });
  10 |
  11 | declare const Constants: {
    at NamespaceFixer.findNamespaces (A:\Windows\Documents\GitHub\SnowTransfer\node_modules\tsup\dist\rollup.js:6121:21)
    at NamespaceFixer.fix (A:\Windows\Documents\GitHub\SnowTransfer\node_modules\tsup\dist\rollup.js:6142:48)
    at Object.renderChunk (A:\Windows\Documents\GitHub\SnowTransfer\node_modules\tsup\dist\rollup.js:7199:25)
    at A:\Windows\Documents\GitHub\SnowTransfer\node_modules\rollup\dist\shared\rollup.js:23807:40
Error: error occured in dts build
    at Worker.<anonymous> (A:\Windows\Documents\GitHub\SnowTransfer\node_modules\tsup\dist\index.js:2226:26)
    at Worker.emit (node:events:513:28)
    at MessagePort.<anonymous> (node:internal/worker:233:53)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:645:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28)
DTS Build error

After making a type in LocalBucket which represented the RateLimiter partially as I needed it, the error went away. Although, I'm a little confused as generating a getter has no benefit and the plain names can be referenced anywhere in the file regardless of where they appear.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
ndelangen added a commit to storybookjs/storybook that referenced this issue Apr 26, 2023
ndelangen added a commit to storybookjs/storybook that referenced this issue May 1, 2023
shilman pushed a commit to storybookjs/storybook that referenced this issue May 3, 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

No branches or pull requests

1 participant