prettier-eslint
Advanced tools
Comparing version
@@ -49,6 +49,24 @@ "use strict"; | ||
const { | ||
output | ||
} = await analyze(options); | ||
return output; | ||
} | ||
/** | ||
* Analyzes and formats text with prettier and eslint, based on the | ||
* identical options as for the `format` function. It differs from | ||
* `format` only in that the return value is a simple object with | ||
* properties `output` giving the formatted code and `messages` giving | ||
* any error messages generated in the analysis. | ||
* @param {Object} identical to options parameter of `format` | ||
* @returns {Promise<Object>} the return value is an object `r` such that | ||
* `r.output` is the formatted string and `r.messages` is an array of | ||
* message specifications from eslint. | ||
*/ | ||
async function analyze(options) { | ||
const { | ||
logLevel = getDefaultLogLevel() | ||
} = options; | ||
logger.setLevel(logLevel); | ||
logger.trace('called format with options:', (0, _prettyFormat.default)(options)); | ||
logger.trace('called analyze with options:', (0, _prettyFormat.default)(options)); | ||
const { | ||
@@ -105,6 +123,12 @@ filePath, | ||
} | ||
return eslintFix(await prettify(text), filePath); | ||
return eslintFix((await prettify(text)).output, filePath); | ||
} | ||
function createPrettify(formatOptions, prettierPath) { | ||
return async function prettify(text) { | ||
return async function prettify(param) { | ||
let text = param; | ||
let messages = []; | ||
if (typeof param !== 'string') { | ||
text = param.output; | ||
messages = param.text; | ||
} | ||
logger.debug('calling prettier on text'); | ||
@@ -126,3 +150,6 @@ logger.trace((0, _commonTags.stripIndent)` | ||
`); | ||
return output; | ||
return { | ||
output, | ||
messages | ||
}; | ||
} catch (error) { | ||
@@ -177,3 +204,4 @@ logger.error('prettier formatting failed due to a prettier error'); | ||
const [{ | ||
output = text | ||
output = text, | ||
messages | ||
}] = await report; | ||
@@ -190,3 +218,6 @@ logger.trace('eslint --fix: output === input', output === text); | ||
`); | ||
return output; | ||
return { | ||
output, | ||
messages | ||
}; | ||
} catch (error) { | ||
@@ -271,2 +302,7 @@ logger.error('eslint fix failed due to an eslint error'); | ||
return process.env.LOG_LEVEL || 'warn'; | ||
} | ||
} | ||
// Allow named imports of either `analyze` or `format` from this module, | ||
// while leaving `format` in place as the default import: | ||
module.exports.format = format; | ||
module.exports.analyze = analyze; |
{ | ||
"name": "prettier-eslint", | ||
"version": "16.1.2", | ||
"version": "16.2.0", | ||
"description": "Formats your JavaScript using prettier followed by eslint --fix", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -149,4 +149,43 @@ # prettier-eslint | ||
**Note:** `prettier-eslint` will not show any message regarding broken rules in either `prettier` or `eslint`. | ||
**Note:** `format` will not show any message regarding broken rules in either `prettier` or `eslint`. | ||
## Capturing ESLint messages | ||
```javascript | ||
const {analyze} = require("prettier-eslint"); | ||
const text = 'var x = 0;'; | ||
const result = await analyze({ | ||
text, | ||
eslintConfig: { | ||
rules: { 'no-var': 'error' } | ||
} | ||
}) | ||
console.log(result.messages); | ||
``` | ||
produces on the console | ||
``` | ||
[{ | ||
ruleId: 'no-var', | ||
severity: 2, | ||
message: 'Unexpected var, use let or const instead.', | ||
line: 1, | ||
column: 1, | ||
nodeType: 'VariableDeclaration', | ||
messageId: 'unexpectedVar', | ||
endLine: 1, | ||
endColumn: 11 | ||
}] | ||
``` | ||
The additional export `analyze` is identical to `format` except that it | ||
returns a simple object with properties `output` giving the exact string | ||
that `format` would return, and `messages` giving the array of message | ||
descriptions (with the structure shown above) produced by the `eslint` | ||
analysis of the code. You may use `analyze` in place of `format` if you | ||
would like to perform the formatting but also capture any errors that | ||
`eslint` may notice. | ||
## Technical details | ||
@@ -312,2 +351,3 @@ | ||
<td align="center" valign="top" width="14.28%"><a href="https://jonathan.rehm.me/"><img src="https://avatars.githubusercontent.com/u/999845?v=4?s=100" width="100px;" alt="Jonathan Rehm"/><br /><sub><b>Jonathan Rehm</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint/commits?author=jkrehm" title="Code">💻</a></td> | ||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/gwhitney"><img src="https://avatars.githubusercontent.com/u/3825429?v=4?s=100" width="100px;" alt="Glen Whitney"/><br /><sub><b>Glen Whitney</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint/commits?author=gwhitney" title="Code">💻</a></td> | ||
</tr> | ||
@@ -314,0 +354,0 @@ </tbody> |
58118
4.34%712
5.01%403
11.02%