-
-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for loading custom formatter from package (#6228)
* Add support for loading custom formatter from package when using PnP (fix #6100) * Update lib/__tests__/resolveCustomFormatter.test.js * Apply suggestions from code review * Update lib/__tests__/resolveCustomFormatter.test.js * Update lib/__tests__/resolveCustomFormatter.test.js * Update lib/__tests__/resolveCustomFormatter.test.js * Apply suggestions from code review Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> * Added cli test and updated doc * Updated doc and added test with module Co-authored-by: JounQin <admin@1stg.me> Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
- Loading branch information
1 parent
3a277b1
commit e271601
Showing
9 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
.eslintcache | ||
yarn.lock | ||
.vscode/settings.json | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../fixtures/formatters/customFormatter'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = function customFormatter(messages) { | ||
return `Custom formatter reports ${messages.length} warning(s).`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
jest.mock('fs'); | ||
jest.mock('path'); | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const resolveCustomFormatter = require('../resolveCustomFormatter'); | ||
|
||
describe('resolveCustomFormatter', () => { | ||
it('should return absolute path when provided path is a file path', () => { | ||
const aRelativePath = 'a/relative/path'; | ||
const expected = `/cwd/${aRelativePath}`; | ||
|
||
path.resolve.mockReturnValue(expected); | ||
path.isAbsolute.mockReturnValue(false); | ||
fs.existsSync.mockReturnValue(true); | ||
|
||
const result = resolveCustomFormatter(aRelativePath); | ||
|
||
expect(result).toEqual(expected); | ||
}); | ||
|
||
it('should return provided path when path is neither absolute nor relative', () => { | ||
const aModulePath = '@stylelint/prettier-config/index.js'; | ||
|
||
fs.existsSync.mockReturnValue(false); | ||
|
||
const result = resolveCustomFormatter(aModulePath); | ||
|
||
const realPathModule = jest.requireActual('path'); | ||
const expectedPath = realPathModule.join('node_modules', aModulePath); | ||
|
||
expect(result.endsWith(expectedPath)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
/** | ||
* @param {string} formatterPath | ||
* @returns {string} | ||
*/ | ||
module.exports = function resolveCustomFormatter(formatterPath) { | ||
const resolvedPath = path.resolve(formatterPath); | ||
|
||
if (fs.existsSync(resolvedPath)) { | ||
return resolvedPath; | ||
} | ||
|
||
return require.resolve(formatterPath); | ||
}; |