Skip to content

fix(pluginutils): attach scope object to for-loop#616

Merged
shellscape merged 3 commits intorollup:masterfrom
eight04:dev-for
Oct 22, 2020
Merged

fix(pluginutils): attach scope object to for-loop#616
shellscape merged 3 commits intorollup:masterfrom
eight04:dev-for

Conversation

@eight04
Copy link
Contributor

@eight04 eight04 commented Oct 22, 2020

Rollup Plugin Name: pluginutils

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers: Fixes #547, fixes #548, fixes #549

Description

This PR adds a new behavior to attachScopes. It creates a new scope object for ForStatement, ForInStatement, and ForOfStatement. IDK if this should be considered as a breaking change.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Looks good, just some minor type-related comments

}
`,
{ ecmaVersion: 2020, sourceType: 'module' }
) as unknown) as import('estree').Program;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be nicer to just

import {Program, ForStatement,...} from 'estree'

once at the top?

Copy link
Member

@lukastaegert lukastaegert Oct 22, 2020

Choose a reason for hiding this comment

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

Also, shouldn't this make "estree" and explicit devDependency in the package.json file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with typescript so these were copied from:

const node = n as import('estree').Node;

Should we also move them to the top?

Copy link
Member

@lukastaegert lukastaegert Oct 22, 2020

Choose a reason for hiding this comment

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

Ah I see, I did not notice that one. So Node is special in so far as it is also a builtin type so one should be careful to override it, this can cause confusion. Maybe in that case just use a star import instead (works for types just like regular things):

import * as estree from 'estree';

and then below you use e.g. estree.Node or estree.ForStatement. Also do not forget to install estree es devDependency, which you should do via pnpm install -D estree from the package directory so that the lockfile is updated.

t.falsy(scope.contains('a'));
t.falsy(scope.contains('b'));

const forLoop = ast.body[0] as import('estree').ForStatement & Record<string, any>;
Copy link
Member

Choose a reason for hiding this comment

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

How about as ForStatement & {scope: AttachedScope}, using the type that attachScopes itself uses?

"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-typescript": "^5.0.2",
"@types/estree": "0.0.45",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up adding @types/estree instead of estree

Comment on lines +1 to 4
// eslint-disable-next-line import/no-unresolved
import * as estree from 'estree';

import { walk } from 'estree-walker';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copied from

// eslint-disable-next-line import/no-unresolved
import { BaseNode } from 'estree';

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Looks good to go from my side!

@shellscape shellscape merged commit ce0652d into rollup:master Oct 22, 2020
@shellscape
Copy link
Collaborator

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants