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

Upgrade to Prettier 3 in devDependencies #75

Merged
merged 13 commits into from
Jul 14, 2023
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [14.x, 16.x, 18.x]
node: [16.x, 18.x]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"prepare": "yarn run compile",
"compile": "tsc",
"preexample": "yarn run compile",
"test": "vitest --run",
"test": "NODE_OPTIONS=--experimental-vm-modules vitest --run",
"test:watch": "vitest",
"format": "prettier . --write",
"format:check": "prettier . --check",
Expand Down Expand Up @@ -67,7 +67,7 @@
"@types/prettier": "^2.7.2",
"@types/semver": "^7.3.13",
"@vue/compiler-sfc": "3.2.47",
"prettier": "2.8.7",
"prettier": "^3.0.0",
"typescript": "5.0.4",
"vitest": "^0.30.1"
},
Expand Down
8 changes: 4 additions & 4 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { testingOnly } from '../normalize-plugin-options';

const emptyImportOrder = testingOnly.normalizeImportOrderOption([]);

test('sorts imports correctly', () => {
test('sorts imports correctly', async () => {
const code = `import z from 'z';
import c from 'c';
import g from 'g';
Expand All @@ -26,7 +26,7 @@ import a from 'a';
originalCode: code,
directives: [],
});
expect(format(formatted, { parser: 'babel' })).toEqual(
expect(await format(formatted, { parser: 'babel' })).toEqual(
`import a from "a";
import c from "c";
import g from "g";
Expand All @@ -37,7 +37,7 @@ import z from "z";
);
});

test('merges duplicate imports correctly', () => {
test('merges duplicate imports correctly', async () => {
const code = `import z from 'z';
import c from 'c';
import g from 'g';
Expand All @@ -59,7 +59,7 @@ import type {See} from 'c';
originalCode: code,
directives: [],
});
expect(format(formatted, { parser: 'babel' })).toEqual(
expect(await format(formatted, { parser: 'typescript' })).toEqual(
`import a, { b, type Bee } from "a";
import c, { type C, type See } from "c";
import g from "g";
Expand Down
40 changes: 20 additions & 20 deletions src/utils/__tests__/merge-nodes-with-matching-flavors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const defaultOptions = examineAndNormalizePluginOptions({
filepath: __filename,
});

test('should merge duplicate imports within a given chunk', () => {
test('should merge duplicate imports within a given chunk', async () => {
const code = `
import type { A } from 'a';
import { Junk } from 'junk-group-1'
Expand Down Expand Up @@ -61,7 +61,7 @@ test('should merge duplicate imports within a given chunk', () => {
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import type { A, B } from "a";
import { Junk } from "junk-group-1";

Expand Down Expand Up @@ -89,7 +89,7 @@ import { Junk2 } from "junk-group-2";
`);
});

test('should merge type imports into regular imports', () => {
test('should merge type imports into regular imports', async () => {
const code = `
// Preserves 'import type'
import type { A1 } from 'a';
Expand Down Expand Up @@ -119,7 +119,7 @@ test('should merge type imports into regular imports', () => {
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`// Preserves 'import type'

import type { A1, A2 } from "a";
Expand All @@ -132,7 +132,7 @@ import { D1, type D2 } from "d";
`);
});

test('should combine type import and default import', () => {
test('should combine type import and default import', async () => {
const code = `
import type {MyType} from './source';
import defaultValue from './source';
Expand All @@ -152,12 +152,12 @@ import defaultValue from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import defaultValue, { type MyType } from "./source";
`);
});

test('should not combine type import and namespace import', () => {
test('should not combine type import and namespace import', async () => {
const code = `
import type {MyType} from './source';
import * as Namespace from './source';
Expand All @@ -177,13 +177,13 @@ import * as Namespace from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import type { MyType } from "./source";
import * as Namespace from "./source";
`);
});

test('should support aliased named imports', () => {
test('should support aliased named imports', async () => {
const code = `
import type {MyType} from './source';
import {value as alias} from './source';
Expand All @@ -203,12 +203,12 @@ import {value as alias} from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import { value as alias, type MyType } from "./source";
`);
});

test('should combine multiple imports from the same source', () => {
test('should combine multiple imports from the same source', async () => {
const code = `
import type {MyType, SecondType} from './source';
import {value, SecondValue} from './source';
Expand All @@ -228,12 +228,12 @@ import {value, SecondValue} from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import { SecondValue, value, type MyType, type SecondType } from "./source";
`);
});

test('should combine multiple groups of imports', () => {
test('should combine multiple groups of imports', async () => {
const code = `
import type {MyType} from './source';
import type {OtherType} from './other';
Expand All @@ -255,13 +255,13 @@ import {otherValue} from './other';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import { otherValue, type OtherType } from "./other";
import { value, type MyType } from "./source";
`);
});

test('should combine multiple imports statements from the same source', () => {
test('should combine multiple imports statements from the same source', async () => {
const code = `
import type {MyType} from './source';
import type {SecondType} from './source';
Expand All @@ -283,12 +283,12 @@ import {SecondValue} from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import { SecondValue, value, type MyType, type SecondType } from "./source";
`);
});

test('should not impact imports from different sources', () => {
test('should not impact imports from different sources', async () => {
const code = `
import type {MyType} from './source';
import type {OtherType} from './other';
Expand All @@ -310,14 +310,14 @@ import {value} from './source';
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import type { OtherType } from "./other";
import { value, type MyType } from "./source";
import { thirdValue } from "./third";
`);
});

test('should not combine default type imports', () => {
test('should not combine default type imports', async () => {
const code = `
import { ComponentProps, useEffect } from "react";
import type React from "react";
Expand All @@ -337,7 +337,7 @@ test('should not combine default type imports', () => {
directives: [],
});

expect(format(formatted, { parser: 'babel' }))
expect(await format(formatted, { parser: 'typescript' }))
.toEqual(`import { ComponentProps, useEffect } from "react";
import type React from "react";
`);
Expand Down
6 changes: 4 additions & 2 deletions src/utils/__tests__/remove-nodes-from-original-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import k from 'k';
import a from 'a';
`;

test('it should remove nodes from the original code', () => {
test('it should remove nodes from the original code', async () => {
const ast = babelParser(code, { sourceType: 'module' });
const importNodes = getImportNodes(code);
const sortedNodes = getSortedNodes(importNodes, {
Expand All @@ -38,6 +38,8 @@ test('it should remove nodes from the original code', () => {
code,
commentAndImportsToRemoveFromCode,
);
const result = format(codeWithoutImportDeclarations, { parser: 'babel' });
const result = await format(codeWithoutImportDeclarations, {
parser: 'babel',
});
expect(result).toEqual('');
});
51 changes: 27 additions & 24 deletions src/utils/get-all-comments-from-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export const getAllCommentsFromNodes = (
| InterpreterDirective
)[],
) =>
nodes.reduce((acc, node) => {
if (
Array.isArray(node.leadingComments) &&
node.leadingComments.length > 0
) {
acc = [...acc, ...node.leadingComments];
}
if (
Array.isArray(node.innerComments) &&
node.innerComments.length > 0
) {
acc = [...acc, ...node.innerComments];
}
if (
Array.isArray(node.trailingComments) &&
node.trailingComments.length > 0
) {
acc = [...acc, ...node.trailingComments];
}
if (node.type === 'ImportDeclaration') {
acc = [...acc, ...getAllCommentsFromNodes(node.specifiers)];
}
return acc;
}, [] as (CommentBlock | CommentLine)[]);
nodes.reduce(
(acc, node) => {
if (
Array.isArray(node.leadingComments) &&
node.leadingComments.length > 0
) {
acc = [...acc, ...node.leadingComments];
}
if (
Array.isArray(node.innerComments) &&
node.innerComments.length > 0
) {
acc = [...acc, ...node.innerComments];
}
if (
Array.isArray(node.trailingComments) &&
node.trailingComments.length > 0
) {
acc = [...acc, ...node.trailingComments];
}
if (node.type === 'ImportDeclaration') {
acc = [...acc, ...getAllCommentsFromNodes(node.specifiers)];
}
return acc;
},
[] as (CommentBlock | CommentLine)[],
);
31 changes: 18 additions & 13 deletions test-setup/run_spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

import fs from 'fs';
import { extname, join, resolve } from 'path';
import { extname } from 'path';

import prettier from 'prettier';
import { format } from 'prettier';
import { expect, test } from 'vitest';

import * as plugin from '../src';

export function run_spec(dirname, parsers, options) {
export async function run_spec(dirname, parsers, options) {
options = Object.assign(
{
plugins: [plugin],
Expand All @@ -22,7 +22,7 @@ export function run_spec(dirname, parsers, options) {
throw new Error(`No parsers were specified for ${dirname}`);
}

fs.readdirSync(dirname).forEach((filename) => {
for (const filename of fs.readdirSync(dirname)) {
const path = dirname + '/' + filename;
if (
extname(filename) !== '.snap' &&
Expand All @@ -35,32 +35,37 @@ export function run_spec(dirname, parsers, options) {
const mergedOptions = Object.assign({}, options, {
parser: parsers[0],
});
const output = prettyprint(source, path, mergedOptions);
test(`${filename} - ${mergedOptions.parser}-verify`, () => {
test(`${filename} - ${mergedOptions.parser}-verify`, async () => {
const output = await prettyprint(source, path, mergedOptions);
expect(
raw(source + '~'.repeat(80) + '\n' + output),
).toMatchSnapshot(filename);
});

parsers.slice(1).forEach((parserName) => {
test(`${filename} - ${parserName}-verify`, () => {
for (const parserName of parsers.slice(1)) {
test(`${filename} - ${parserName}-verify`, async () => {
const output = await prettyprint(
source,
path,
mergedOptions,
);
const verifyOptions = Object.assign(mergedOptions, {
parser: parserName,
});
const verifyOutput = prettyprint(
const verifyOutput = await prettyprint(
source,
path,
verifyOptions,
);
expect(output).toEqual(verifyOutput);
});
});
}
}
});
}
}

function prettyprint(src, filename, options) {
return prettier.format(
async function prettyprint(src, filename, options) {
return await format(
src,
Object.assign(
{
Expand Down
Loading