Use getPhysicalFilename() instead of getFilename() if available (ESLint 7.28+)#2160
Conversation
| it('falls back to getFilename() when getPhysicalFileName() is not available (ESLint < 7.28)', function () { | ||
| const testContext = utils.testContext({ 'import/resolver': './foo-bar-resolver-v1' }); | ||
|
|
||
| expect(resolve( '../files/foo' | ||
| , Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js'); } }), | ||
| )).to.equal(utils.testFilePath('./bar.jsx')); | ||
|
|
||
| expect(resolve( '../files/foo' | ||
| , Object.assign({}, testContext, { getFilename: function () { throw new Error('Should call getPhysicalFilename() instead of getFilename()'); }, getPhysicalFilename: function () { return utils.getFilename('foo.js'); } }), | ||
| )).to.equal(utils.testFilePath('./bar.jsx')); | ||
|
|
||
|
|
||
| }); |
There was a problem hiding this comment.
Added this test to make sure the fallback behavior is working. Otherwise the tests have been updated to use getPhysicalFilename() instead of getFilename().
ljharb
left a comment
There was a problem hiding this comment.
Thanks!
please swap all the a && a() || b() for a ? a() : b(); plus the tests comment
|
|
||
| export function getContextPackagePath(context) { | ||
| return getFilePackagePath(context.getFilename()); | ||
| return getFilePackagePath((context.getPhysicalFilename && context.getPhysicalFilename()) || context.getFilename()); |
There was a problem hiding this comment.
| return getFilePackagePath((context.getPhysicalFilename && context.getPhysicalFilename()) || context.getFilename()); | |
| return getFilePackagePath(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()); |
| if (deepLookup.path.length > 1) { | ||
| const deepPath = deepLookup.path | ||
| .map(i => path.relative(path.dirname(context.getFilename()), i.path)) | ||
| .map(i => path.relative(path.dirname((context.getPhysicalFilename && context.getPhysicalFilename()) || context.getFilename()), i.path)) |
There was a problem hiding this comment.
| .map(i => path.relative(path.dirname((context.getPhysicalFilename && context.getPhysicalFilename()) || context.getFilename()), i.path)) | |
| .map(i => path.relative(path.dirname(context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename()), i.path)) |
There was a problem hiding this comment.
Yes, that's better. Let me know how you want to proceed on the tests and I'll make both changes at the same time. Thanks for the feedback!
|
|
||
| expect(resolve( '../files/foo' | ||
| , Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js'); } }), | ||
| , Object.assign({}, testContext, { getPhysicalFilename: function () { return utils.getFilename('foo.js'); } }), |
There was a problem hiding this comment.
rather than modifying the existing tests, please leave these alone, so that the new getPhysicalFilename duplicated tests are clearly additions
There was a problem hiding this comment.
Okay, which do you prefer?
- Duplicate all of the existing tests so that the only difference is
getFilename()vs.getPhysicalFilename() - Revert the changes so that we're testing all of the use cases for
getFilename()and not exercisinggetPhysicalFilename()except for the couple of tests demonstrating thatgetPhysicalFilename()is the preferred API andgetFIlename()is a fallback. - Same as 1, but add
getFilename() { throw new Error(...) }to the mock context on all the new tests rather than have a separate test for the fallback.
There was a problem hiding this comment.
I was originally thinking option 1, but i really do like option 3.
8c3aafa to
2f0243f
Compare
a4c0ab4 to
bfab4cc
Compare
|
(Had to update the tests so the getPhysicalFilename stuff only applies in eslint v7.28+) |
Starting with 7.28.0, ESLint has a new feature distinguishing the physical filename from the virtual filename.
When there's a difference, this plugin needs the real, physical filename. (See
eslint/eslint#11989)
I ran into the problem when I noticed
no-unresolvedwas yielding false positives inside of code blocks in Storybook / Markdown files and helped push the necessary change through in ESLint.To maintain backwards compatibility, this change uses
context.getPhysicalFilename()if available and otherwise falls back togetFilename().