Skip to content

Commit

Permalink
feat!: support prettier 3.0 (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabeler-lee-6rs authored Jan 15, 2024
1 parent 56e166d commit c6d8543
Show file tree
Hide file tree
Showing 11 changed files with 980 additions and 542 deletions.
22 changes: 9 additions & 13 deletions __mocks__/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ const path = require('path');

const prettierMock = {
format: jest.fn().mockImplementation((input) => 'formatted:' + input),
resolveConfig: {
sync: jest.fn().mockImplementation((file) => ({ file })),
},
getFileInfo: {
sync: jest.fn().mockImplementation((file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' };
} else {
return { ignored: false, inferredParser: null };
}
}),
},
resolveConfig: jest.fn().mockImplementation(async (file) => ({ file })),
getFileInfo: jest.fn().mockImplementation(async (file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' };
} else {
return { ignored: false, inferredParser: null };
}
}),
};

module.exports = prettierMock;
108 changes: 56 additions & 52 deletions bin/pretty-quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,67 @@ const args = mri(process.argv.slice(2), {
},
});

const prettyQuickResult = prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm,
)} revision ${chalk.bold(revision)}.`,
);
},
async function main() {
const prettyQuickResult = await prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm,
)} revision ${chalk.bold(revision)}.`,
);
},

onFoundChangedFiles: (changedFiles) => {
console.log(
`🎯 Found ${chalk.bold(changedFiles.length)} changed ${
changedFiles.length === 1 ? 'file' : 'files'
}.`,
);
},
onFoundChangedFiles: (changedFiles) => {
console.log(
`🎯 Found ${chalk.bold(changedFiles.length)} changed ${
changedFiles.length === 1 ? 'file' : 'files'
}.`,
);
},

onPartiallyStagedFile: (file) => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
},
onPartiallyStagedFile: (file) => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
},

onWriteFile: (file) => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},
onWriteFile: (file) => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},

onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
}
},
onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
}
},

onExamineFile: (file) => {
console.log(`🔍 Examining ${chalk.bold(file)}.`);
},
}),
);
onExamineFile: (file) => {
console.log(`🔍 Examining ${chalk.bold(file)}.`);
},
}),
);

if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!');
} else {
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
);
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
);
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
);
if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!');
} else {
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
);
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
);
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
);
}
process.exit(1); // ensure git hooks abort
}
process.exit(1); // ensure git hooks abort
}

main();
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@
},
"prettier": "@azz/prettier-config",
"peerDependencies": {
"prettier": ">=2.0.0"
"prettier": "^3.0.0"
},
"devDependencies": {
"@azz/prettier-config": "^1.0.0",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"eslint": "^6.8.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-jest": "^27.2.2",
"eslint-plugin-prettier": "^5.0.0-alpha.1",
"husky": "^4.2.3",
"jest": "^25.2.3",
"mock-fs": "^4.11.0",
"prettier": "2.0.2"
"prettier": "^3.0.0"
}
}
18 changes: 9 additions & 9 deletions src/__tests__/isSupportedExtension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import prettier from 'prettier';

afterEach(() => jest.clearAllMocks());

test('return true when file with supported extension passed in', () => {
expect(isSupportedExtension(true)('banana.js')).toEqual(true);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.js', {
test('return true when file with supported extension passed in', async () => {
expect(await isSupportedExtension(true)('banana.js')).toEqual(true);
expect(prettier.getFileInfo).toHaveBeenCalledWith('banana.js', {
file: 'banana.js',
resolveConfig: true,
});
});

test('return false when file with not supported extension passed in', () => {
expect(isSupportedExtension(true)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
test('return false when file with not supported extension passed in', async () => {
expect(await isSupportedExtension(true)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo).toHaveBeenCalledWith('banana.txt', {
file: 'banana.txt',
resolveConfig: true,
});
});

test('do not resolve config when false passed', () => {
expect(isSupportedExtension(false)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo.sync).toHaveBeenCalledWith('banana.txt', {
test('do not resolve config when false passed', async () => {
expect(await isSupportedExtension(false)('banana.txt')).toEqual(false);
expect(prettier.getFileInfo).toHaveBeenCalledWith('banana.txt', {
file: 'banana.txt',
resolveConfig: false,
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/pretty-quick.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jest.mock('execa');

afterEach(() => mock.restore());

test('throws an error when no vcs is found', () => {
test('throws an error when no vcs is found', async () => {
mock({
'root/README.md': '',
});

expect(() => prettyQuick('root')).toThrow(
await expect(prettyQuick('root')).rejects.toThrow(
'Unable to detect a source control manager.',
);
});
Loading

0 comments on commit c6d8543

Please sign in to comment.