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

Added stub for require.cache #4807

Closed
wants to merge 3 commits into from
Closed

Added stub for require.cache #4807

wants to merge 3 commits into from

Conversation

navaru
Copy link
Contributor

@navaru navaru commented Jun 26, 2020

↪️ Pull Request

Fixes: #4616

Following the previous #4621 discussion on the same issue, I've made the changes.

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

@height
Copy link

height bot commented Jun 26, 2020

Link Height tasks by mentioning a task ID in the pull request title or description, commit messages, or comments.

💡Tip: You can also use "Close T-X" to automatically close a task when the pull request is merged.

@DeMoorJasper
Copy link
Member

Looks good to, @mischnic could you have a look to validate the changes to scope-hoisting are done properly?

Comment on lines +609 to +615
if (t.matchesPattern(path.node, 'module.require') && !isCommonJS) {
path.replaceWith(t.identifier('null'));
}

if (t.matchesPattern(path.node, 'require.cache') && !isCommonJS) {
path.replaceWith(t.objectExpression([]));
}
Copy link
Member

Choose a reason for hiding this comment

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

{
	let module = {
		require() {
			return 1;
		},
	};
	const x = module.require("fs");

	console.log(module, x);
}

Is transformed to ...; var x = null("fs"); ..., you need && !path.scope.hasBinding("module") (and same for require).

Then this should work:

{
	function require() {
		return 1;
	}
	require.cache = [1, 2, 3];
	let module = {
		require,
	};
	const x = module.require("fs");

	console.log(module, module.require("fs"), require.cache[0]);
}
  1. The following would throw a cryptic error (Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "ObjectExpression") even with the hasBinding check.
{
	require.cache = [1, 2, 3];
	let module = {
		require() {
			return 1;
		},
	};
	const x = module.require("fs");

	console.log(module, module.require("fs"), require.cache[0]);
}

(And these should probably be integration tests as well)

@navaru
Copy link
Contributor Author

navaru commented Jul 15, 2020

Currently I'm overwhelmed by stuff at work, closing for now, will be back in a couple of weeks.

@navaru navaru closed this Jul 15, 2020
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

Successfully merging this pull request may close these issues.

Require.cache causes errors
3 participants