-
Notifications
You must be signed in to change notification settings - Fork 414
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
Use separate module system to properly handle errors #111
Conversation
Codecov Report
@@ Coverage Diff @@
## master #111 +/- ##
==========================================
+ Coverage 96.87% 97.65% +0.77%
==========================================
Files 15 18 +3
Lines 256 341 +85
Branches 49 59 +10
==========================================
+ Hits 248 333 +85
Misses 8 8
Continue to review full report at Codecov.
|
> 4 | width: \${document.width}; | ||
| ^ | ||
5 | \`; | ||
at File.buildCodeFrameError (<<REPLACED>>/node_modules/babel-core/lib/transformation/file/index.js:427:15) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stacktrace seems confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stacktrace is negligible here. Could not find a reference to
error is thrown even before evaluating, also this error is from previous PR.
pluginOptions: any = { cache: false, extract: false }, | ||
babelOptions: any = { filename: 'test.js' } | ||
) { | ||
const PATH_TO_TRANSPILE_BIN = path.join(__dirname, './transpile.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: don't need the ./
const filenames = []; | ||
|
||
fs.writeFileSync = (filename, data, opts) => { | ||
if (/\.css$/.test(filename)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment will be nice to tell what's going on here
3d8b4e6
to
057984a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some small comments, but generally looks good. Now it makes sense why we need our own module mock for prevaled stuff, stellar work 👍
expect.addSnapshotSerializer({ | ||
test: val => val && val.toString && val.toString().includes(process.cwd()), | ||
serialize, | ||
print: serialize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should use either serialize
or print
, both are not necessary.
font-size: 3em; | ||
\`; | ||
`); | ||
expect(code.includes('font-size: 3em;')).toBeTruthy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use toMatch
, also snapshot is usually not needed when you're matching strings (you're already testing the transpiled output in other places).
expect(code).toMatch('font-size: 3em;')
`); | ||
} catch (error) { | ||
expect(stripAnsi(error.toString())).toMatch(/> 10 \| {5}throw new Error/); | ||
expect(error).toMatchSnapshot(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either snapshot or toMatch
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's just redundant
codeWithSlugFromFilename | ||
); | ||
|
||
expect(classnameWithSlugFromContent).not.toBeNull(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RegExp.test
is faster than exec
and returns a boolean, also global flag is not needed.
expect(/header = '(header__[a-z0-9]+)'/.test(codeWithSlugFromContent)).toBe(true)
But I see you're already using exec
everywhere, so I can live with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to use exec
, test
is insufficient for the test.
expect(css).toMatchSnapshot(); | ||
}); | ||
|
||
it('should preval const and let without transpilation to var', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you actually testing that here?
expect(data).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally in this whole file, when you have toMatch
next to toMatchSnapshot
– pick one, in most cases I'd pick toMatch
because it's simple and clear enough and doesn't force me to jump between files
"Error: Test message | ||
[0m [90m 1 | [39m[90m// comment[39m | ||
[31m[1m>[22m[39m[90m 2 | [39m[36mthrow[39m [36mnew[39m [33mError[39m([32m\\"test\\"[39m)[33m;[39m | ||
[90m | [39m [31m[1m^[22m[39m[0m |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we strip these colors? I can live without them being here and it's pretty hard to read in this form.
src/babel/lib/moduleSystem.js
Outdated
const filename = resolveMock(moduleId); | ||
|
||
// Native Node modules | ||
if (filename === moduleId && !moduleId.startsWith('/')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest the solution
expect(ModuleMock('file.js')).toEqual(new Module('file.js')); | ||
const cache = ModuleMock._cache; | ||
expect(ModuleMock._debug()).toBeUndefined(); | ||
expect(ModuleMock._preloadModules()).toBeUndefined(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things like this should be handled by Flow, just sayin'
src/babel/lib/moduleSystem.js
Outdated
* ============================================== | ||
* To avoid leakage from evaled code to module cache in current context, | ||
* for example with `babel-register` we provide our custom module system. | ||
* It designed to mimic the native node one, with the exception being |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: It's
Rebase to master and I think we're good to go. @satya164 ? |
const { code } = transpile(dedent` | ||
/* linaria-preval */ | ||
const header = css\` | ||
font-size: {2 + 1}em; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you mean ${2 + 1}
? this syntax seems wrong?
} | ||
|
||
export function clearModulesCache() { | ||
Object.keys(modulesCache).forEach(moduleId => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
won't it be better just to do modulesCache = {}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will break references. Besides it's used only in tests.
Is it ready? There seem to be some small comments which need to be addressed ARE YOU READY? |
Few notes
Related to #81
Things done in this PR
babel-register
babel-register
leakage to main module system__integration-tests__
Things to do
module
module in module system 😛preval-extract
modulesbabel/lib
modules