plugins icon indicating copy to clipboard operation
plugins copied to clipboard

fix(inject): detect references inside object destructuring

Open eight04 opened this issue 4 years ago • 13 comments

Rollup Plugin Name: inject

This PR contains:

  • [x] bugfix
  • [ ] feature
  • [ ] refactor
  • [ ] documentation
  • [ ] other

Are tests included?

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

Breaking Changes?

  • [ ] yes (breaking changes will not be merged unless absolutely necessary)
  • [x] 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:

#1235

Description

Currently the plugin doesn't detect references inside object destructuring:

// `process` wasn't detected
function foo({cwd = process.cwd()}) { }

This PR fixes it.

Another issue: I ran git commit and hit this bug: https://github.com/pnpm/pnpm/issues/4790 I ended up running git commit --no-verify so something might be wrong.

Also it seems that test snapshots are built differently on different platforms. You may want to rebuild it on Unix.

eight04 avatar Aug 12 '22 16:08 eight04

What platform are you developing on? We get quite a few Windows users submitting PRs and a good chunk of the maintainers use MacOS and Linux.

shellscape avatar Aug 12 '22 18:08 shellscape

Windows 10 / Node 17.4.0.

eight04 avatar Aug 13 '22 02:08 eight04

Maybe it's your Node version? Even Node doesn't recommend running odd number versions for anything remotely production.

shellscape avatar Aug 13 '22 02:08 shellscape

I upgraded Node.js to 18.7.0.

  • Still can't git commit:
    C:\Users\eight04\dev\plugins\packages\inject>git commit -m test
     ERR_PNPM_FETCH_404  GET https://registry.npmjs.org/--no-install: Not Found - 404
    
    --no-install is not in the npm registry, or you have no permission to fetch it.
    
    An authorization header was used: Bearer ****[hidden]
    
  • Test snapshots still doesn't include escapes backslashes section.

eight04 avatar Aug 13 '22 02:08 eight04

There's something awry on your system I'm afraid. Just cloned a fresh copy, applied your changes, and had no issues with committing. WSL might be an option for you, but generally when we see odd errors like that, it points back to bad system config.

shellscape avatar Aug 13 '22 02:08 shellscape

Out of curiosity, which version of pnpm are you using?

shellscape avatar Aug 13 '22 02:08 shellscape

pnpm 7.9.0.

Someone in the thread (https://github.com/pnpm/pnpm/issues/4790) did suggest that downgrading pnpm solves the issue. Should I try that?

eight04 avatar Aug 13 '22 02:08 eight04

Yes please try the last v6.x version. fwiw our lockfile here is still v6 format.

shellscape avatar Aug 13 '22 02:08 shellscape

Yes downgrading pnpm to 6.x works.

C:\Users\eight04\dev\plugins\packages\inject>pnpm --version
6.34.0

C:\Users\eight04\dev\plugins\packages\inject>git commit -m "test"
husky > pre-commit (node v18.7.0)
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.{ts,js}
[STARTED] Running tasks for **/(package|tsconfig(.*)?).json
[STARTED] Running tasks for (pnpm-workspace|.github/**/*).{yml,yaml}
[STARTED] Running tasks for ((.github/**/*)|(README|CHANGELOG)|(**/(README|CHANGELOG))).md
[SKIPPED] No staged files match **/(package|tsconfig(.*)?).json
[SKIPPED] No staged files match (pnpm-workspace|.github/**/*).{yml,yaml}
[SKIPPED] No staged files match ((.github/**/*)|(README|CHANGELOG)|(**/(README|CHANGELOG))).md
[STARTED] eslint --cache --fix
[SUCCESS] eslint --cache --fix
[SUCCESS] Running tasks for *.{ts,js}
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SUCCESS] Applying modifications...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
[dev-destruct 9e8fa3a] test
 1 file changed, 1 insertion(+), 1 deletion(-)

eight04 avatar Aug 13 '22 02:08 eight04

sweet. something still squirrelly with v7

shellscape avatar Aug 13 '22 02:08 shellscape

I'm going to be bumping the pnpm lockfile version in master on Monday (I think). we can resolve the CI issues with this PR then.

shellscape avatar Sep 10 '22 20:09 shellscape

The error is that when testing on Windows, one test snapshot is missing.

I think we need someone who dont use Windows to rebuild snapshots in this pr.

eight04 avatar Sep 11 '22 01:09 eight04

Or fix the test so it can generate snapshots correctly on Windows.

eight04 avatar Sep 11 '22 01:09 eight04

I tried to build this on master but then got a new error:

> @rollup/[email protected] build C:\Users\eight04\dev\rollup-plugins\packages\inject
> rollup -c

[!] Error: Debug Failure. Expected C:/Users/eight04/dev/rollup-plugins/packages/inject/tsconfig.json === C:\Users\eight04\dev\rollup-plugins\packages\inject\tsconfig.json.
Error: Debug Failure. Expected C:/Users/eight04/dev/rollup-plugins/packages/inject/tsconfig.json === C:\Users\eight04\dev\rollup-plugins\packages\inject\tsconfig.json.
    at attachFileToDiagnostic (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:20253:18)
    at Object.attachFileToDiagnostics (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:20285:42)
    at Object.parseJsonText (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:32087:46)
    at Object.parseJsonText (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:31813:23)
    at parseConfigFileTextToJson (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:41443:33)
    at Object.readConfigFile (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\[email protected]\node_modules\typescript\lib\typescript.js:41434:48)
    at readTsConfigFile (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\@[email protected][email protected]\node_modules\@rollup\plugin-typescript\dist\index.js:303:34)
    at parseTypescriptConfig (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\@[email protected][email protected]\node_modules\@rollup\plugin-typescript\dist\index.js:356:41)
    at typescript (C:\Users\eight04\dev\rollup-plugins\node_modules\.pnpm\@[email protected][email protected]\node_modules\@rollup\plugin-typescript\dist\index.js:758:27)
    at createConfig (file:///C:/Users/eight04/dev/rollup-plugins/shared/rollup.config.mjs:38:15)

Seems like a cross platform issue related to path separator.

eight04 avatar Oct 14 '22 06:10 eight04

I think it should be guarded by if (scope.contains(...))

Fair enough

I guess we can add more test cases

Also

const {$ = fallback} = foo;

which is the example I should have written.

lukastaegert avatar Oct 14 '22 07:10 lukastaegert

Done

eight04 avatar Oct 14 '22 12:10 eight04