-
Notifications
You must be signed in to change notification settings - Fork 17
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
Issue with jest --coverage #7
Comments
Yeah, I'm thinking we should add a test that can reproduce this issue. If you could come up with a simple isolated example and add a test here I can work on addressing it. This issue of babel plugins not working with the coverage babel plugin has been a real pain for me for a long time. preval has the same problem. |
At the moment I have no idea what exactly causing it. I guess |
Yes, that's what's causing it. Check this out. I'm fairly certain that we just need to handle a babel-plugin-istanbul converts this: var x = foo() to something like this: var x = (c++, foo()) So we need to figure out a good way to handle that SequenceExpression. I'm pretty sure our test case could be something like: import importAll from '../macro'
const a = ('whatever', importAll.sync('./fixtures/*.js')) With that we should be able to figure out what's wrong and fix this bug. |
the smallest reproducible demo I can come with const filename = "test.js";
const src = `import importAll from "import-all.macro";
const all = importAll("./components/*.js");`;
const options = {
presets: [
[
require("@babel/preset-env").default,
{
targets: { node: "6.12" }
}
]
],
plugins: [
require("babel-plugin-macros"),
[
require("babel-plugin-istanbul").default,
{
cwd: __dirname,
exclude: []
}
]
]
};
require("babel-core").transform(src, { filename, ...options }); https://github.com/stereobooster/babel-macro-issue/tree/minimal-example |
Hey-o, I did some research on my own (I ❤️ problems like this), and I think we could do something even easier. Here's some stuff that I printed out while debugging:
It seems to me that while the paths are up-to-date with the transforms, the relationship between the nodes is preserved. I'm positive that if Of course then Are there any strong arguments for using |
Btw. handling |
Thanks for that. I'll dig in! |
Further findings:
I'll continue looking into that in parallel, maybe we'll come up with a solution quicker. |
Awesome! Thank you for your help @fatfisz! |
It looks like babel is not doing a great job at updating paths after
is called in istanbul-lib-instrument (from babel-plugin-istanbul). It seems that if a plugin is doing Based on this I experimented with traversing the whole program again (the idea was to look for the identifiers manually instead of using the scope), and the most magical thing has happened. When the following piece of code is put inside
everything starts to work! It doesn't matter if you put it before the
What's even more interesting is that just calling
doesn't fix the problem - The thing I don't like about this finding is that it is a hack and the relationship between the line of code that fixes all and the actual underlying cause is not clear. The good thing about it though is that it should handle all the cases, not only when the path is replaced with itself inside a |
Also that finding makes things clear: babel is at fault, because calling |
@fatfisz, would you like to open a PR on babel-plugin-macros with your changes and a comment explaining what it's there for. Then we can at least get this fixed for people using macros for now. Then we can maybe open an issue on Babel to see why this works around the problem and how to actually fix it in babel. Thank you so much for all the digging! |
Ok, I'll try to come up with a minimal reproduction test and prepare the PR. It's quite late where I live though, so I'm not sure if I'll get it done today or tomorrow. |
I'll come back to this tomorrow. For now here's a minimal reproduction of the same path behavior: 'use strict';
const { transform } = require('babel-core');
const { default: generator } = require('babel-generator');
const { default: traverse } = require('babel-traverse');
const types = require('babel-types');
const visitedPaths = new Set();
const badVisitor = {
VariableDeclarator: {
enter(path, state) {
const initPath = path.get('init');
initPath.replaceWith(
types.sequenceExpression([
types.stringLiteral('foobar'),
initPath.node,
]),
);
},
},
};
transform(
`
import foo from 'foo';
const bar = foo();
`,
{
plugins: [
() => ({
visitor: {
Program: {
enter(path) {
path.traverse(badVisitor);
},
exit(path, state) {
console.log(generator(state.file.scope.path.node, {}, null).code);
},
},
ImportDeclaration(path) {
const varPath = path.scope.getBinding('foo').referencePaths[0];
console.log(
generator(varPath.node, {}, null).code,
);
console.log(
generator(varPath.parentPath.node, {}, null).code,
);
console.log(
generator(varPath.parent, {}, null).code,
);
// The last two should print the same thing
},
},
})
],
},
); |
This is great work! Well done. Thank you @fatfisz and @stereobooster! |
Just FYI @hzoo and @loganfsmyth, this will likely turn into a babel issue soon :) |
I hit multiple issues trying to run the tests on Windows - switching to Ubuntu VM... |
Thanks for fixing that issue! It's been merged and will be released in a minute. Then @stereobooster, if could test this out with the latest version of |
Verified fix with small example. Opened PR in create-react-app. @fatfisz is a hero |
babel/babel#7596 This is the original issue we found before. |
Reproducible demo: https://github.com/stereobooster/babel-macro-issue/tree/import-all
But
Related pveyes/raw.macro#5
The text was updated successfully, but these errors were encountered: