Skip to content

Commit

Permalink
feat: 📦 Support ESLint 8 (#431)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Requires ESLint 8. ESLint 8 API changes caused the format function to now be asynchronous.
Requires minimum Node 12 as required by ESLint 8
  • Loading branch information
idahogurl committed May 11, 2022
1 parent afc22f5 commit 4e4077d
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 57 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@
"code",
"maintenance"
]
},
{
"login": "idahogurl",
"name": "Rebecca Vest",
"avatar_url": "https://avatars.githubusercontent.com/u/10620169?v=4",
"profile": "https://campcode.dev/",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
18 changes: 10 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const config = {
extends: ["kentcdodds", "kentcdodds/jest"],
extends: ['kentcdodds', 'kentcdodds/jest'],
rules: {
"max-len": "off",
"import/max-dependencies": "off",
"space-before-function-paren": [
"error",
quotes: ['error', 'single', { avoidEscape: true }],
'arrow-parens': ['error', 'as-needed'],
'max-len': 'off',
'import/max-dependencies': 'off',
'space-before-function-paren': [
'error',
{
anonymous: "never",
named: "never",
asyncArrow: "always"
anonymous: 'never',
named: 'never',
asyncArrow: 'always'
}
]
}
Expand Down
49 changes: 25 additions & 24 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion __mocks__/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = jest.fn(function mockGlob(globString, options, callback) {
} else {
throw new Error(
`Your test globString: "${globString}"` +
` doesn't have associated mock data.`
" doesn't have associated mock data."
);
}
});
Expand Down
4 changes: 2 additions & 2 deletions __mocks__/prettier-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const format = jest.fn(({ text, filePath = '' }) => {
} else if (filePath.includes('eslint-config-error')) {
throw new Error('Some weird eslint config error');
} else if (filePath.includes('no-change')) {
return text;
return Promise.resolve(text);
}
return `MOCK_OUTPUT for ${filePath || 'stdin'}`;
return Promise.resolve(`MOCK_OUTPUT for ${filePath || 'stdin'}`);
});

export default format;
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = {
'@babel/preset-env',
{
targets: {
node: '8'
node: '12'
},
useBuiltIns: 'usage',
corejs: 3
Expand Down
5 changes: 5 additions & 0 deletions cli-test/override-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
quotes: ['error', 'double']
}
};
25 changes: 23 additions & 2 deletions cli-test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ test('formats files and outputs to stdout', async () => {
);
});

test('handles --eslint-config-path', async () => {
// can't just do the testOutput function here because
// the output is in an undeterministic order
const stdout = await runPrettierESLintCLI(
`cli-test/fixtures/stdout1.js --no-eslint-ignore --no-prettier-ignore --eslint-config-path ${__dirname}/../override-config.js`
);
expect(stdout).toContain(
stripIndent(
`
import baz, { stuff } from "fdjakfdlfw-baz";
export { bazzy };
function bazzy(something) {
return baz(stuff(something));
}
`
).trim()
);
});

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
Expand All @@ -96,8 +117,8 @@ test(`prettier-eslint ${writeCommand}`, async () => {
const example1Path = path.resolve(__dirname, '../fixtures/example1.js');
const example2Path = path.resolve(__dirname, '../fixtures/example2.js');
try {
const example1 = `const { example1 } = baz.bar`;
const example2 = `function example2(thing){return thing;};;;;;;;;;`;
const example1 = 'const { example1 } = baz.bar';
const example2 = 'function example2(thing){return thing;};;;;;;;;;';
await Promise.all([
pWriteFile(example1Path, example1),
pWriteFile(example2Path, example2)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "CLI for prettier-eslint",
"main": "dist/no-main.js",
"engines": {
"node": ">=8"
"node": ">=12.22"
},
"bin": {
"prettier-eslint": "dist/index.js"
Expand All @@ -30,7 +30,7 @@
"chalk": "^2.4.2",
"common-tags": "^1.8.0",
"core-js": "^3.1.4",
"eslint": "^5.0.0",
"eslint": "^8.14.0",
"find-up": "^4.1.0",
"get-stdin": "^7.0.0",
"glob": "^7.1.4",
Expand All @@ -39,7 +39,7 @@
"lodash.memoize": "^4.1.2",
"loglevel-colored-level-prefix": "^1.0.0",
"messageformat": "^2.2.1",
"prettier-eslint": "^9.0.0",
"prettier-eslint": "^14.0.3",
"rxjs": "^6.5.2",
"yargs": "^13.2.4"
},
Expand All @@ -53,7 +53,7 @@
"ajv": "^6.10.0",
"all-contributors-cli": "^6.7.0",
"babel-jest": "^24.8.0",
"eslint-config-kentcdodds": "14.3.3",
"eslint-config-kentcdodds": "^20.2.0",
"husky": "^2.4.1",
"jest": "^24.8.0",
"jest-cli": "^24.8.0",
Expand Down
24 changes: 13 additions & 11 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import findUp from 'find-up';
import memoize from 'lodash.memoize';
import indentString from 'indent-string';
import getLogger from 'loglevel-colored-level-prefix';
import ConfigFile from 'eslint/lib/config/config-file';
import Linter from 'eslint/lib/linter';
import Config from 'eslint/lib/config';
import * as messages from './messages';

const LINE_SEPERATOR_REGEX = /(\r|\n|\r\n)/;
Expand Down Expand Up @@ -69,11 +66,9 @@ function formatFilesFromArgv({
};

if (eslintConfigPath) {
const configContext = new Config({}, new Linter());
prettierESLintOptions.eslintConfig = ConfigFile.load(
eslintConfigPath,
configContext
);
prettierESLintOptions.eslintConfig = {
overrideConfigFile: eslintConfigPath
};
}

const cliOptions = { write, listDifferent };
Expand All @@ -94,7 +89,10 @@ 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 @@ -238,9 +236,13 @@ function getFilesFromGlob(
function formatFile(filePath, prettierESLintOptions, cliOptions) {
const fileInfo = { filePath };
let format$ = rxReadFile(filePath, 'utf8').pipe(
map(text => {
mergeMap(async text => {
fileInfo.text = text;
fileInfo.formatted = format({ text, filePath, ...prettierESLintOptions });
fileInfo.formatted = await format({
text,
filePath,
...prettierESLintOptions
});
fileInfo.unchanged = fileInfo.text === fileInfo.formatted;
return fileInfo;
})
Expand Down
6 changes: 3 additions & 3 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ const mf = new MessageFormat('en');
export { success, failure, unchanged };

function success(data) {
const files = `{count, plural, one{file} other{files}}`;
const files = '{count, plural, one{file} other{files}}';
return mf.compile(
`{success} formatting {countString} ${files} with prettier-eslint`
)(data);
}

function failure(data) {
const files = `{count, plural, one{file} other{files}}`;
const files = '{count, plural, one{file} other{files}}';
return mf.compile(
`{failure} formatting {countString} ${files} with prettier-eslint`
)(data);
}

function unchanged(data) {
const files = `{count, plural, one{file was} other{files were}}`;
const files = '{count, plural, one{file was} other{files were}}';
return mf.compile(`{countString} ${files} {unchanged}`)(data);
}
1 change: 0 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ const parser = yargs
},
'jsx-bracket-same-line': {
type: 'boolean',
default: undefined,
describe: oneLine`
Put the > of a multi-line JSX element at
the end of the last line instead of
Expand Down

0 comments on commit 4e4077d

Please sign in to comment.