Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: prettier ~3.0.0 #1202

Merged
merged 11 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/lazy-foxes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'skuba': minor
---

deps: Prettier 3.0

See the [release notes](https://prettier.io/blog/2023/07/05/3.0.0.html) for more information.
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ jobs:

- name: Test
run: yarn test:ci
env:
NODE_OPTIONS: --experimental-vm-modules

- name: Lint
run: yarn lint
Expand Down Expand Up @@ -91,3 +93,5 @@ jobs:
- if: github.head_ref != 'beta' && github.head_ref != 'changeset-release/master' && github.ref_name != 'beta' && github.ref_name != 'changeset-release/master'
name: Test template
run: yarn test:template ${{ matrix.template }}
env:
NODE_OPTIONS: --experimental-vm-modules
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ If all is well, they will merge your pull request into master.
You may find it easier to develop alongside unit tests:

```shell
export NODE_OPTIONS=--experimental-vm-modules

yarn test --watch
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"npm-run-path": "^4.0.1",
"npm-which": "^3.0.1",
"picomatch": "^2.2.2",
"prettier": "~2.8.5",
"prettier": "~3.0.0",
"read-pkg-up": "^7.0.1",
"runtypes": "^6.0.0",
"semantic-release": "^21.0.0",
Expand Down
6 changes: 4 additions & 2 deletions src/cli/adapter/prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ describe('inferParser', () => {
${'client.tsx'} | ${'typescript'}
${'server.ts'} | ${'typescript'}
${'vanilla.js'} | ${'babel'}
`('inferParser($filepath) === $parser', ({ filepath, parser }) =>
expect(inferParser(filepath)).toBe(parser),
`(
'inferParser($filepath) === $parser',
async ({ filepath, parser }) =>
await expect(inferParser(filepath)).resolves.toBe(parser),
);
});
28 changes: 16 additions & 12 deletions src/cli/adapter/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ let languages: SupportLanguage[] | undefined;
* - https://github.com/prettier/prettier/blob/2.4.1/src/main/options.js#L167
* - seek-oss/skuba#659
*/
export const inferParser = (filepath: string): string | undefined => {
export const inferParser = async (
filepath: string,
): Promise<string | undefined> => {
const filename = path.basename(filepath).toLowerCase();

languages ??= getSupportInfo().languages.filter((language) => language.since);
languages ??= (await getSupportInfo()).languages;

const firstLanguage = languages.find(
(language) =>
Expand All @@ -46,21 +48,21 @@ export const inferParser = (filepath: string): string | undefined => {
return firstLanguage?.parsers[0];
};

const isPackageJsonOk = ({
const isPackageJsonOk = async ({
data,
filepath,
}: {
data: string;
filepath: string;
}): boolean => {
}): Promise<boolean> => {
if (path.basename(filepath) !== 'package.json') {
return true;
}

try {
const packageJson = parsePackage(data);

return !packageJson || formatPackage(packageJson) === data;
return !packageJson || (await formatPackage(packageJson)) === data;
} catch {
// Be more lenient about our custom formatting and don't throw if it errors.
}
Expand All @@ -81,15 +83,17 @@ interface Result {
unparsed: string[];
}

const formatOrLintFile = (
const formatOrLintFile = async (
{ data, filepath, options }: File,
mode: 'format' | 'lint',
result: Result,
): string | undefined => {
): Promise<string | undefined> => {
if (mode === 'lint') {
let ok: boolean;
try {
ok = check(data, options) && isPackageJsonOk({ data, filepath });
ok =
(await check(data, options)) &&
(await isPackageJsonOk({ data, filepath }));
} catch (err) {
result.errored.push({ err, filepath });
return;
Expand All @@ -104,7 +108,7 @@ const formatOrLintFile = (

let formatted: string;
try {
formatted = format(data, options);
formatted = await format(data, options);
} catch (err) {
result.errored.push({ err, filepath });
return;
Expand All @@ -115,7 +119,7 @@ const formatOrLintFile = (
if (path.basename(filepath) === 'package.json') {
const packageJson = parsePackage(formatted);
if (packageJson) {
formatted = formatPackage(packageJson);
formatted = await formatPackage(packageJson);
}
}
} catch {
Expand Down Expand Up @@ -183,7 +187,7 @@ export const runPrettier = async (

for (const filepath of filepaths) {
// Infer parser upfront so we can skip unsupported files.
const parser = inferParser(filepath);
const parser = await inferParser(filepath);

logger.debug(filepath);
logger.debug(' parser:', parser ?? '-');
Expand All @@ -204,7 +208,7 @@ export const runPrettier = async (
options: { ...config, filepath },
};

const formatted = formatOrLintFile(file, mode, result);
const formatted = await formatOrLintFile(file, mode, result);

if (typeof formatted === 'string') {
await fs.promises.writeFile(filepath, formatted);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/configure/addEmptyExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const addEmptyExports = async () => {
return;
}

const data = formatPrettier([inputFile, 'export {}'].join('\n\n'), {
const data = await formatPrettier([inputFile, 'export {}'].join('\n\n'), {
parser: 'typescript',
});

Expand Down
2 changes: 1 addition & 1 deletion src/cli/configure/analyseDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const analyseDependencies = async ({
}

return async () => {
const updatedPackageJson = formatPackage({
const updatedPackageJson = await formatPackage({
...packageJson,
dependencies: output.dependencies,
devDependencies: output.devDependencies,
Expand Down
10 changes: 7 additions & 3 deletions src/cli/configure/analysis/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const loadModuleFiles = async (modules: Module[], destinationRoot: string) => {
};
};

const processTextFiles = (
const processTextFiles = async (
modules: Module[],
inputFiles: Readonly<Files>,
patternToFilepaths: Record<string, string[]>,
Expand All @@ -69,7 +69,7 @@ const processTextFiles = (
);

for (const [filepath, processText] of textProcessorEntries) {
outputFiles[filepath] = processText(
outputFiles[filepath] = await processText(
outputFiles[filepath],
outputFiles,
inputFiles,
Expand All @@ -86,7 +86,11 @@ export const diffFiles = async (opts: Options): Promise<FileDiff> => {
await loadModuleFiles(modules, opts.destinationRoot),
);

const outputFiles = processTextFiles(modules, inputFiles, patternToFilepaths);
const outputFiles = await processTextFiles(
modules,
inputFiles,
patternToFilepaths,
);

const diffEntries = Object.entries(outputFiles)
.filter(([filepath, data]) => inputFiles[filepath] !== data)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/configure/ensureTemplateCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const ensureTemplateCompletion = async ({
name: 'customAnswers',
});

const updatedPackageJson = formatPackage(manifest.packageJson);
const updatedPackageJson = await formatPackage(manifest.packageJson);
const packageJsonFilepath = path.join(destinationRoot, 'package.json');
await fs.promises.writeFile(packageJsonFilepath, updatedPackageJson);

Expand Down
6 changes: 4 additions & 2 deletions src/cli/configure/modules/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const jestModule = async (): Promise<Module> => {
return {
...deleteFiles('jest.config.js', 'jest.setup.js'),

'jest.config.ts': (tsFile, currentFiles, initialFiles) => {
'jest.config.ts': async (tsFile, currentFiles, initialFiles) => {
// Allow customised TS Jest config that extends skuba
if (tsFile?.includes('skuba')) {
return OUTDATED_ISOLATED_MODULES_CONFIG_SNIPPETS.reduce(
Expand All @@ -66,7 +66,9 @@ export const jestModule = async (): Promise<Module> => {
const inputFile = tsFile ?? jsFile;

const props =
inputFile === undefined ? undefined : readModuleExports(inputFile);
inputFile === undefined
? undefined
: await readModuleExports(inputFile);

if (props === undefined) {
return configFile;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/configure/patchRenovateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const patchJson: PatchFile = async ({ filepath, input, presetToAdd }) => {

await fs.promises.writeFile(
filepath,
formatPrettier(JSON.stringify(config), { parser: 'json' }),
await formatPrettier(JSON.stringify(config), { parser: 'json' }),
);

return;
Expand All @@ -77,7 +77,7 @@ const patchJson5: PatchFile = async ({ filepath, input, presetToAdd }) => {

await fs.promises.writeFile(
filepath,
formatPrettier(fleece.patch(input, config), { parser: 'json5' }),
await formatPrettier(fleece.patch(input, config), { parser: 'json5' }),
);

return;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/configure/patchServerListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const patchServerListener = async (dir: string) => {

await fs.promises.writeFile(
SERVER_LISTENER_FILENAME,
formatPrettier(listener, {
await formatPrettier(listener, {
parser: 'typescript',
}),
);
Expand Down
17 changes: 9 additions & 8 deletions src/cli/configure/processing/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { formatObject, parseObject } from './json';

describe('formatObject', () => {
it('sorts and formats', () =>
expect(
it('sorts and formats', async () =>
await expect(
formatObject({
b: 1,
a: null,
c: [],
}),
).toMatchInlineSnapshot(`
).resolves.toMatchInlineSnapshot(`
"{
"a": null,
"b": 1,
Expand All @@ -17,17 +17,18 @@ describe('formatObject', () => {
"
`));

it('handles ordinary JSON array formatting', () =>
expect(formatObject({ files: ['1', '2', '3'] })).toMatchInlineSnapshot(`
it('handles ordinary JSON array formatting', async () =>
await expect(formatObject({ files: ['1', '2', '3'] })).resolves
.toMatchInlineSnapshot(`
"{
"files": ["1", "2", "3"]
}
"
`));

it('handles special-cased package.json array formatting', () =>
expect(formatObject({ files: ['1', '2', '3'] }, 'package.json'))
.toMatchInlineSnapshot(`
it('handles special-cased package.json array formatting', async () =>
await expect(formatObject({ files: ['1', '2', '3'] }, 'package.json'))
.resolves.toMatchInlineSnapshot(`
"{
"files": [
"1",
Expand Down
24 changes: 12 additions & 12 deletions src/cli/configure/processing/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ describe('createDependencyFilter', () => {
});

describe('withPackage', () => {
it('applies function', () =>
expect(
it('applies function', async () =>
await expect(
withPackage((data) => {
data.$name = 'unit-test';

return data;
})('{}'),
).toMatchInlineSnapshot(`
).resolves.toMatchInlineSnapshot(`
"{
"$name": "unit-test"
}
"
`));

it('preserves legitimate fields', () =>
expect(
it('preserves legitimate fields', async () =>
await expect(
withPackage((data) => {
data.$name = 'unit-test';

Expand All @@ -94,7 +94,7 @@ describe('withPackage', () => {
name: 'my-package',
}),
),
).toMatchInlineSnapshot(`
).resolves.toMatchInlineSnapshot(`
"{
"name": "my-package",
"version": "0.1.0",
Expand All @@ -105,8 +105,8 @@ describe('withPackage', () => {
"
`));

it('sorts fields', () =>
expect(
it('sorts fields', async () =>
await expect(
withPackage((data) => data)(
JSON.stringify({
devDependencies: {
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('withPackage', () => {
files: ['b', 'a'],
}),
),
).toMatchInlineSnapshot(`
).resolves.toMatchInlineSnapshot(`
"{
"files": [
"b",
Expand Down Expand Up @@ -170,14 +170,14 @@ describe('withPackage', () => {
"
`));

it('handles bad JSON gracefully', () =>
expect(
it('handles bad JSON gracefully', async () =>
await expect(
withPackage((data) => {
data.$name = 'unit-test';

return data;
})('}'),
).toMatchInlineSnapshot(`
).resolves.toMatchInlineSnapshot(`
"{
"$name": "unit-test"
}
Expand Down
Loading