Skip to content

Commit

Permalink
fix(modules): use prettier-eslint versions of modules if needed
Browse files Browse the repository at this point in the history
This way you can install prettier-eslint and not have
prettier or eslint installed yourself and it should
still work.
  • Loading branch information
Kent C. Dodds committed Jan 20, 2017
1 parent b4ebe38 commit dc49660
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
},
"jest": {
"testEnvironment": "node",
"testEnvironment": "jest-environment-node",
"coverageThreshold": {
"global": {
"branches": 100,
Expand Down
9 changes: 7 additions & 2 deletions src/__mocks__/eslint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// this mock file is so eslint doesn't attempt to actually
// search around the filesystem for stuff
// search around the file system for stuff

const eslint = require.requireActual('eslint')
const {CLIEngine} = eslint
Expand All @@ -17,9 +17,14 @@ module.exports = Object.assign(eslint, {
})

function MockCLIEngine(...args) {
global.__PRETTIER_ESLINT_TEST_STATE__.eslintPath = __filename
CLIEngine.apply(this, args)
// not sure why, but in some cases, this.executeOnText is undefined...
// so we create a fakeCLIEngine to get a copy of that function
// and call it with apply :)
const fakeCLIEngine = new CLIEngine(...args)
this.getConfigForFile = mockGetConfigForFileSpy
this._originalExecuteOnText = this.executeOnText
this._originalExecuteOnText = fakeCLIEngine.executeOnText
this.executeOnText = mockExecuteOnTextSpy
}

Expand Down
1 change: 1 addition & 0 deletions src/__mocks__/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Object.assign(prettier, {
})

function mockFormat(...args) {
global.__PRETTIER_ESLINT_TEST_STATE__.prettierPath = __filename
if (mockFormatSpy.throwError) {
throw mockFormatSpy.throwError
}
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ function getConfig(filePath, eslintPath) {
}

function getModulePath(filePath = __filename, moduleName) {
return requireRelative.resolve(moduleName, filePath)
try {
return requireRelative.resolve(moduleName, filePath)
} catch (error) {
logError(`There was a problem finding the ${moduleName} module. Using prettier-eslint's version`, error.stack)
return require.resolve(moduleName)
}
}

function getESLintCLIEngine(eslintPath, eslintOptions) {
Expand Down
12 changes: 12 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ test('resolves to the eslint module relative to the given filePath', () => {
})
})

test('resolves to the local eslint module if none is found via the filePath', () => {
const filePath = '/blah-blah/default-config'
format({text: '', filePath})
expect(global.__PRETTIER_ESLINT_TEST_STATE__).toMatchObject({
// without Jest's mocking, these would actually resolve to the
// project modules :) The fact that jest's mocking is being
// applied is good enough for this test.
eslintPath: require.resolve('./__mocks__/eslint'),
prettierPath: require.resolve('./__mocks__/prettier'),
})
})

function getESLintConfigWithDefaultRules(overrides) {
return {
parserOptions: {
Expand Down
13 changes: 11 additions & 2 deletions tests/fixtures/paths/node_modules/eslint/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions tests/fixtures/paths/node_modules/prettier/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc49660

Please sign in to comment.