Skip to content

Commit

Permalink
feat: update for the prettier-eslint beta (#99)
Browse files Browse the repository at this point in the history
This package now supports prettier's configuration capabilities
  • Loading branch information
Kent C. Dodds committed Aug 30, 2017
1 parent bb185e2 commit baf80f4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 39 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"object-curly-spacing": [
"error",
"never"
]
],
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}]
}
}
3 changes: 2 additions & 1 deletion __mocks__/prettier-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// http://facebook.github.io/jest/docs/manual-mocks.html
// so we just return some spies here and assert
// that we're calling prettier-eslint APIs correctly
const format = jest.fn(({text, filePath = ''}) => {
// eslint-disable-next-line require-await
const format = jest.fn(async ({text, filePath = ''}) => {
if (text === 'MOCK_SYNTAX_ERROR' || filePath.includes('syntax-error')) {
throw new Error('Mock error for a syntax error')
} else if (filePath.includes('eslint-config-error')) {
Expand Down
12 changes: 6 additions & 6 deletions cli-test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const BABEL_BIN_PATH = require.resolve('babel-cli/bin/babel-node')

testOutput('--version')

test('help outputs usage information and flags', async() => {
test('help outputs usage information and flags', async () => {
// can't just do the testOutput function here because
// the output is variable (based on the width of your
// terminal I think)...
Expand All @@ -39,7 +39,7 @@ test('help outputs usage information and flags', async() => {
}
})

test('formats files and outputs to stdout', async() => {
test('formats files and outputs to stdout', async () => {
// can't just do the testOutput function here because
// the output is in an undeterministic order
const stdout = await runPrettierESLintCLI(
Expand Down Expand Up @@ -71,7 +71,7 @@ test('formats files and outputs to stdout', async() => {
)
})

test('list different files with the --list-different option', async() => {
test('list different files with the --list-different option', async () => {
// can't just do the testOutput function here because
// the output is in an undeterministic order
const stdout = await runPrettierESLintCLI(
Expand All @@ -81,15 +81,15 @@ test('list different files with the --list-different option', async() => {
expect(stdout).toContain('cli-test/fixtures/stdout2.js')
})

test('accepts stdin of code', async() => {
test('accepts stdin of code', async () => {
const stdin = 'echo "console.log( window.baz , typeof [] ); "'
const stdout = await runPrettierESLintCLI('--stdin', stdin)
expect(stdout).toEqual('console.log(window.baz, typeof [])\n')
})

const writeCommand = 'cli-test/fixtures/example*.js --write --no-eslint-ignore'

test(`prettier-eslint ${writeCommand}`, async() => {
test(`prettier-eslint ${writeCommand}`, async () => {
// because we're using --write,
// we have to recreate and delete the files every time
const example1Path = path.resolve(__dirname, '../fixtures/example1.js')
Expand Down Expand Up @@ -120,7 +120,7 @@ test(`prettier-eslint ${writeCommand}`, async() => {
})

function testOutput(command) {
test(`prettier-eslint ${command}`, async() => {
test(`prettier-eslint ${command}`, async () => {
try {
const stdout = await runPrettierESLintCLI(command)
expect(stdout).toMatchSnapshot(`stdout: ${command}`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"lodash.memoize": "^4.1.2",
"loglevel-colored-level-prefix": "^1.0.0",
"messageformat": "^1.0.2",
"prettier-eslint": "^6.4.3",
"prettier-eslint": "^7.0.0",
"rxjs": "^5.3.0",
"yargs": "8.0.2"
},
Expand Down
19 changes: 13 additions & 6 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function formatFilesFromArgv({
async function formatStdin(prettierESLintOptions) {
const stdinValue = (await getStdin()).trim()
try {
const formatted = format({text: stdinValue, ...prettierESLintOptions})
const formatted = await format({text: stdinValue, ...prettierESLintOptions})
process.stdout.write(formatted)
return Promise.resolve(formatted)
} catch (error) {
Expand Down Expand Up @@ -197,11 +197,18 @@ function getFilesFromGlob(ignoreGlobs, applyEslintIgnore, fileGlob) {

function formatFile(filePath, prettierESLintOptions, cliOptions) {
const fileInfo = {filePath}
let format$ = rxReadFile(filePath, 'utf8').map(text => {
fileInfo.text = text
fileInfo.formatted = format({text, filePath, ...prettierESLintOptions})
fileInfo.unchanged = fileInfo.text === fileInfo.formatted
return fileInfo
let format$ = rxReadFile(filePath, 'utf8').mergeMap(text => {
const promise = format({
text,
filePath,
...prettierESLintOptions,
}).then(formatted => {
fileInfo.text = text
fileInfo.formatted = formatted
fileInfo.unchanged = fileInfo.text === fileInfo.formatted
return fileInfo
})
return Rx.Observable.fromPromise(promise)
})

if (cliOptions.write) {
Expand Down
48 changes: 24 additions & 24 deletions src/format-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ afterEach(() => {
process.exitCode = 0
})

test('sanity test', async() => {
test('sanity test', async () => {
const globs = ['src/**/1*.js', 'src/**/2*.js']
await formatFiles({_: globs})
expect(globMock).toHaveBeenCalledTimes(globs.length)
Expand All @@ -37,7 +37,7 @@ test('sanity test', async() => {
expect(console.error).toHaveBeenCalledWith(successOutput)
})

test('glob call inclues an ignore of node_modules', async() => {
test('glob call inclues an ignore of node_modules', async () => {
const fileGlob = 'src/**/1*.js'
await formatFiles({_: [fileGlob]})
const globOptions = expect.objectContaining({
Expand All @@ -47,7 +47,7 @@ test('glob call inclues an ignore of node_modules', async() => {
expect(globMock).toHaveBeenCalledWith(fileGlob, globOptions, callback)
})

test('glob call excludes an ignore of node_modules', async() => {
test('glob call excludes an ignore of node_modules', async () => {
const fileGlob = 'foo/node_modules/stuff*.js'
await formatFiles({_: [fileGlob]})
expect(globMock).not.toHaveBeenCalledWith(
Expand All @@ -60,7 +60,7 @@ test('glob call excludes an ignore of node_modules', async() => {
)
})

test('should accept stdin', async() => {
test('should accept stdin', async () => {
mockGetStdin.stdin = ' var [ foo, { bar } ] = window.APP ;'
await formatFiles({stdin: true})
expect(formatMock).toHaveBeenCalledTimes(1)
Expand All @@ -71,19 +71,19 @@ test('should accept stdin', async() => {
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin')
})

test('will write to files if that is specified', async() => {
test('will write to files if that is specified', async () => {
const fileGlob = 'src/**/1*.js'
await formatFiles({_: [fileGlob], write: true})
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
})

test('handles stdin errors gracefully', async() => {
test('handles stdin errors gracefully', async () => {
mockGetStdin.stdin = 'MOCK_SYNTAX_ERROR'
await formatFiles({stdin: true})
expect(console.error).toHaveBeenCalledTimes(1)
})

test('handles file errors gracefully', async() => {
test('handles file errors gracefully', async () => {
const globs = ['files-with-syntax-errors/*.js', 'src/**/1*.js']
await formatFiles({_: globs, write: true})
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
Expand All @@ -94,13 +94,13 @@ test('handles file errors gracefully', async() => {
expect(console.error).toHaveBeenCalledWith(failureOutput)
})

test('does not print success if there were no successful files', async() => {
test('does not print success if there were no successful files', async () => {
await formatFiles({_: ['no-match/*.js']})
const successOutput = expect.stringMatching(/unhandled error/)
expect(process.stdout.write).not.toHaveBeenCalledWith(successOutput)
})

test('fails gracefully if something odd happens', async() => {
test('fails gracefully if something odd happens', async () => {
await formatFiles({_: ['throw-error/*.js']})
expect(console.error).toHaveBeenCalledTimes(1)
const label = expect.stringMatching(/prettier-eslint-cli/)
Expand All @@ -109,7 +109,7 @@ test('fails gracefully if something odd happens', async() => {
expect(console.error).toHaveBeenCalledWith(label, notice, errorStack)
})

test('logs errors to the console if something goes wrong', async() => {
test('logs errors to the console if something goes wrong', async () => {
const globs = ['eslint-config-error/*.js', 'src/**/2*.js']
await formatFiles({_: globs, write: true})
expect(fsMock.writeFile).toHaveBeenCalledTimes(4)
Expand All @@ -124,7 +124,7 @@ test('logs errors to the console if something goes wrong', async() => {
expect(console.error).toHaveBeenCalledWith(errorPrefix, cliError, errorOutput)
})

test('does not log anything to the console if logLevel is silent', async() => {
test('does not log anything to the console if logLevel is silent', async () => {
const log = getLogger()
const globs = ['eslint-config-error/*.js', 'src/**/2*.js']
await formatFiles({
Expand All @@ -136,20 +136,20 @@ test('does not log anything to the console if logLevel is silent', async() => {
expect(console.error).not.toHaveBeenCalled()
})

test('forwards logLevel onto prettier-eslint', async() => {
test('forwards logLevel onto prettier-eslint', async () => {
await formatFiles({_: ['src/**/1*.js'], logLevel: 'debug'})
const options = expect.objectContaining({logLevel: 'debug'})
expect(formatMock).toHaveBeenCalledWith(options)
})

test('forwards prettierLast onto prettier-eslint', async() => {
test('forwards prettierLast onto prettier-eslint', async () => {
await formatFiles({_: ['src/**/1*.js'], prettierLast: true})
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({prettierLast: true}),
)
})

test('forwards prettierOptions onto prettier-eslint', async() => {
test('forwards prettierOptions onto prettier-eslint', async () => {
await formatFiles({
_: ['src/**/1*.js'],
trailingComma: 'es5',
Expand All @@ -159,7 +159,7 @@ test('forwards prettierOptions onto prettier-eslint', async() => {
)
})

test('wont save file if contents did not change', async() => {
test('wont save file if contents did not change', async () => {
const fileGlob = 'no-change/*.js'
await formatFiles({_: [fileGlob], write: true})
expect(fsMock.readFile).toHaveBeenCalledTimes(3)
Expand All @@ -168,7 +168,7 @@ test('wont save file if contents did not change', async() => {
expect(console.error).toHaveBeenCalledWith(unchangedOutput)
})

test('will report unchanged files even if not written', async() => {
test('will report unchanged files even if not written', async () => {
const fileGlob = 'no-change/*.js'
await formatFiles({_: [fileGlob], write: false})
expect(fsMock.readFile).toHaveBeenCalledTimes(3)
Expand All @@ -177,7 +177,7 @@ test('will report unchanged files even if not written', async() => {
expect(console.error).toHaveBeenCalledWith(unchangedOutput)
})

test('allows you to specify an ignore glob', async() => {
test('allows you to specify an ignore glob', async () => {
const ignore = ['src/ignore/thing', 'src/ignore/otherthing']
const fileGlob = 'src/**/1*.js'
await formatFiles({_: [fileGlob], ignore})
Expand All @@ -189,7 +189,7 @@ test('allows you to specify an ignore glob', async() => {
expect(globMock).toHaveBeenCalledWith(fileGlob, globOptions, callback)
})

test('wont modify a file if it is eslint ignored', async() => {
test('wont modify a file if it is eslint ignored', async () => {
await formatFiles({_: ['src/**/ignored*.js'], write: true})
expect(fsMock.readFile).toHaveBeenCalledTimes(1)
expect(fsMock.writeFile).toHaveBeenCalledTimes(1)
Expand All @@ -207,7 +207,7 @@ test('wont modify a file if it is eslint ignored', async() => {
expect(console.error).toHaveBeenCalledWith(ignoredOutput)
})

test('will modify a file if it is eslint ignored with noIgnore', async() => {
test('will modify a file if it is eslint ignored with noIgnore', async () => {
await formatFiles({
_: ['src/**/ignored*.js'],
write: true,
Expand All @@ -219,7 +219,7 @@ test('will modify a file if it is eslint ignored with noIgnore', async() => {
expect(console.error).toHaveBeenCalledWith(ignoredOutput)
})

test('will not blow up if an .eslintignore cannot be found', async() => {
test('will not blow up if an .eslintignore cannot be found', async () => {
const originalSync = findUpMock.sync
findUpMock.sync = () => null
try {
Expand All @@ -235,7 +235,7 @@ test('will not blow up if an .eslintignore cannot be found', async() => {
})

describe('listDifferent', () => {
test('will list different files', async() => {
test('will list different files', async () => {
await formatFiles({
_: ['src/**/1*.js', 'src/**/no-change*.js'],
listDifferent: true,
Expand All @@ -257,7 +257,7 @@ describe('listDifferent', () => {
expect(console.log).toHaveBeenCalledWith(`${path}start.js`)
})

test('will error out when contents did change', async() => {
test('will error out when contents did change', async () => {
const fileGlob = 'src/**/1*.js'
await formatFiles({
_: [fileGlob],
Expand All @@ -266,7 +266,7 @@ describe('listDifferent', () => {
expect(process.exitCode).toBe(1)
})

test('wont error out when contents did not change', async() => {
test('wont error out when contents did not change', async () => {
const fileGlob = 'no-change/*.js'
await formatFiles({
_: [fileGlob],
Expand All @@ -277,7 +277,7 @@ describe('listDifferent', () => {
})

describe('eslintConfigPath', () => {
test('will use eslintrc', async() => {
test('will use eslintrc', async () => {
await formatFiles({
_: ['src/**/1*.js'],
eslintConfigPath: '.eslintrc',
Expand Down

0 comments on commit baf80f4

Please sign in to comment.