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

carefully filter require arguments #138

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fabiosantoscode
Copy link

@fabiosantoscode fabiosantoscode commented Jun 8, 2021

I've tried to hydrate some code I ran through esbuild (to ease typescript compilation), and I noticed some issues that came up since it minifies code.

Most of them were related to esbuild (like other minifiers) reusing variables, as illustrated in the test mock I've changed.

I also fixed a crash which occurred when esquery returned a VariableDeclarator without an initializer.

Nice project, keep it up, and hope this helps! <3

Thank you for helping out! ✨

We really appreciate your commitment to improving Architect

To maintain a high standard of quality in our releases, before merging every pull request we ask that you've completed the following:

  • Forked the repo and created your branch from master
  • Made sure tests pass (run npm it from the repo root)
  • Expanded test coverage related to your changes:
    • Added and/or updated unit tests (if appropriate)
    • Added and/or updated integration tests (if appropriate)
  • Updated relevant documentation:
  • Summarized your changes in changelog.md
  • Linked to any related issues, PRs, etc. below that may relate to, consume, or necessitate these changes

Please also be sure to completed the CLA (if you haven't already).

Learn more about contributing to Architect here.

Thanks again!

@CLAassistant
Copy link

CLAassistant commented Jun 8, 2021

CLA assistant check
All committers have signed the CLA.

package.json Outdated Show resolved Hide resolved
// Filter invalid package calls, Architect shared + views, and Node.js builtins
let isArcShared = /^@architect(\/|\\)(shared|views)/
called.forEach(r => {
if (typeof r === 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

Would we expect to see !string values in there? When and under what circumstances?

Copy link
Author

Choose a reason for hiding this comment

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

I caught this when using esbuild to preprocess lambdas. It reuses variables, like such:

let x = 123
// (use x)
x = 'lodash'
y = require(x)

And when looking for the initialisation of "x", it sees a number.

require(requiredThing)

// There may be a function called require that's not the real thing
require(1234)
Copy link
Member

Choose a reason for hiding this comment

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

Is this a case that we really ought to support? If someone is writing Node.js code that breaks CJS, I don't see any responsibility on our part to aid that.

Copy link
Author

Choose a reason for hiding this comment

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

I agree. Though it's entirely possible that someone created a function called require, but I won't die on that hill :)

// When minifiers redefine variables, the original might be an invalid module string
let requiredThing = 'some thing '
requiredThing = 'validmodule'
require(requiredThing)
Copy link
Member

Choose a reason for hiding this comment

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

It seems like you want to support this but I'm not seeing it appear in the tests, what was the intent here? Sorry I'm not following!

Also can we change that from 'some thing ' to something else? We already have a match in there for something and I just want to be a bit clearer. Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

It's another case of minifiers reusing variables. Yes I can change to "invalid module string "

// Minifiers may redefine variables to save space
let missingInit
missingInit = 'ignoredmodule'
require(missingInit)
Copy link
Member

Choose a reason for hiding this comment

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

In general I hope we'll do our best to warn folks; in this change I don't think we've successfully warned the user about these missing requires, have we?

Copy link
Author

Choose a reason for hiding this comment

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

I should verify that!

@ryanblock ryanblock marked this pull request as draft June 8, 2021 22:07
@fabiosantoscode
Copy link
Author

After writing this PR I noticed that I didn't need hydration after all (esbuild was doing all the work).

So I completely understand if you don't want to support my use case, and I can remove some of the functionality.

@ryanblock
Copy link
Member

Up to you! Seemed like some potentially useful stuff in there, but I can't say it solves problems I've had.

I've tried to hydrate some code I ran through esbuild (to ease typescript compilation), and I noticed some issues that came up since it minifies code.

Most of them were related to esbuild (like other minifiers) reusing variables, as illustrated in the test mock I've changed.

I also fixed a crash which occurred when esquery returned a VariableDeclarator without an initializer.

Nice project, keep it up, and hope this helps! <3
@fabiosantoscode fabiosantoscode force-pushed the carefully-filter-require-arguments branch from f48ceb5 to f26f209 Compare July 17, 2021 13:14
@fabiosantoscode
Copy link
Author

Sorry for the delay @ryanblock!

I've added some warnings, and tests for those warnings. You said "some potentially useful stuff" but I'm not sure which checks you'd like for me to remove so I kept them all in, please advise.

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

Successfully merging this pull request may close these issues.

3 participants