Skip to content

Commit

Permalink
feat(options): add sillyLogs option
Browse files Browse the repository at this point in the history
* feat(options): add `sillyLogs` option

Provides for configuration of whether to dump the eslintConfig and prettierOptions to the console,
defaults to false.

Test is still WIP.

refs #7

* test(options): sillyLogs test now succeeds

The sillyLogs specific test is now more comprehensive, checking both for the specific console.log
and also that console.dir was generally called.

refs #7

* test(options): sillyLogs increased in specificity

The sillyLogs option specific test now has greater specifics regarding structure of assertions.

closes #7
  • Loading branch information
edm00se authored and Kent C. Dodds committed Jan 19, 2017
1 parent e2fdfbf commit 3922b02
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ one, then it will use the version that `prettier-eslint` has installed locally.

This is basically the same as `eslintPath` except for the `prettier` module.

#### sillyLogs (?Boolean)

When set to `true`, `prettier-eslint` will dump the contents of both the detected `eslintConfig` and `prettierOptions`
configuration objects to the console. This defaults to `false` as it is primarily for debugging.
"globally."

### throws

`prettier-eslint` will propagate errors when either `prettier` or `eslint` fails for one reason or another. In addition
Expand Down
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import path from 'path'
import {getPrettierOptionsFromESLintRules} from './utils'

const options = {disableLog: false}
const options = {disableLog: false, sillyLogs: false}
// CommonJS + ES6 modules... is it worth it? Probably not...
module.exports = format
module.exports.options = options
Expand All @@ -23,6 +23,7 @@ module.exports.options = options
* @param {Object} options.prettierOptions - the options to pass for
* formatting with `prettier`. If not provided, prettier-eslint will attempt
* to create the options based on the eslintConfig
* @param {Boolean} options.sillyLogs - enables silly logging (default: false)
* @return {String} - the formatted string
*/
function format({
Expand All @@ -33,9 +34,11 @@ function format({
disableLog = options.disableLog,
eslintConfig = getConfig(filePath, eslintPath),
prettierOptions = getPrettierOptionsFromESLintRules(eslintConfig),
sillyLogs = options.sillyLogs,
}) {
const originalLogValue = options.disableLog
options.disableLog = disableLog
logSilliness(sillyLogs, eslintConfig, prettierOptions)

try {
// console.log('text', text)
Expand Down Expand Up @@ -130,3 +133,13 @@ function logError(...args) {
console.error('prettier-eslint error:', ...args)
}
}

function logSilliness(sillyLogs, eslintConfig, prettierOptions) {
if (sillyLogs) {
console.log('😜 logs for eslintConfig and prettierOptions:')
console.dir({
eslintConfig,
prettierOptions,
}, null, true)
}
}
21 changes: 21 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import prettierMock from 'prettier'
import format from './' // eslint-disable-line import/default

console.error = jest.fn()
console.log = jest.fn()
console.dir = jest.fn()

const tests = [
{
Expand Down Expand Up @@ -63,6 +65,8 @@ const tests = [

beforeEach(() => {
console.error.mockClear()
console.log.mockClear()
console.dir.mockClear()
eslintMock.mock.executeOnText.mockClear()
eslintMock.mock.getConfigForFile.mockClear()
prettierMock.format.mockClear()
Expand Down Expand Up @@ -114,6 +118,23 @@ test('console.error will not be called if disableLog is set', () => {
getConfigForFile.throwError = null
})

test('console receives output of both eslintConfig and prettierOptions when sillyLogs is set', () => {
format.options.sillyLogs = true

// expect(format({text: ''})).toHaveBeenCalledWith(console.log, console.dir)
format({text: ''})
// TODO: fix this test, since it fails on the matcher toHaveBeenCalledTimes
expect(console.log).toHaveBeenCalledWith('😜 logs for eslintConfig and prettierOptions:')
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.dir).toHaveBeenCalledWith(expect.objectContaining({
eslintConfig: expect.anything(),
prettierOptions: expect.anything(),
}), null, true)
expect(console.dir).toHaveBeenCalledTimes(1)

format.options.sillyLogs = false
})

test(`when prettier throws, log to console.error but don't fail`, () => {
const {format: prettierMockFormat} = prettierMock
const error = 'something bad happened'
Expand Down

0 comments on commit 3922b02

Please sign in to comment.