Skip to content

Commit

Permalink
chore: update prettier to version 3
Browse files Browse the repository at this point in the history
Closes #95.
  • Loading branch information
simonhaenisch committed Jul 11, 2023
1 parent d588db2 commit b15df6a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @todo change these to their new locations (`prettier/plugins/<parser>`) with
* the next major release.
*/
const { parsers: babelParsers } = require('prettier/parser-babel');
const { parsers: htmlParsers } = require('prettier/parser-html');
const { parsers: typescriptParsers } = require('prettier/parser-typescript');
Expand Down Expand Up @@ -53,7 +57,6 @@ const plugin = {
default: false,
category: 'OrganizeImports',
description: 'Skip destructive code actions like removing unused imports.',
since: '2.0.0',
},
},
parsers: {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@
},
"devDependencies": {
"@types/node": "18.11.9",
"@types/prettier": "2.7.3",
"@volar/vue-language-plugin-pug": "1.0.9",
"@volar/vue-typescript": "1.0.9",
"ava": "3.15.0",
"prettier": "2.7.1",
"prettier": "3.0.0",
"typescript": "5.1.6"
},
"prettier": {
Expand Down
7 changes: 4 additions & 3 deletions test/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const prettier = require('prettier');
* @param {string} code
* @param {prettier.Options} [options]
*/
module.exports.prettify = (code, options) => prettier.format(code, { plugins: ['.'], filepath: 'file.ts', ...options });
module.exports.prettify = async (code, options) =>
prettier.format(code, { plugins: ['./index.js'], filepath: 'file.ts', ...options });

/**
* @param {prettier.Options['parser']} parser
Expand All @@ -18,8 +19,8 @@ module.exports.getMacro = (parser) => {
* @param {prettier.Options} [options.options]
* @param {(res: string) => string} [options.transformer]
*/
function macro(t, input, expected, { options = {}, transformer = (res) => res.split('\n')[0] } = {}) {
const formattedCode = module.exports.prettify(input, { parser, ...options });
async function macro(t, input, expected, { options = {}, transformer = (res) => res.split('\n')[0] } = {}) {
const formattedCode = await module.exports.prettify(input, { parser, ...options });

t.is(transformer(formattedCode), expected);
}
Expand Down
4 changes: 2 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ test(
{ transformer: (res) => res.split('\n')[1] },
);

test('does not remove unused imports with `organizeImportsSkipDestructiveCodeActions` enabled', (t) => {
test('does not remove unused imports with `organizeImportsSkipDestructiveCodeActions` enabled', async (t) => {
const code = `import { foo } from "./bar";
`;

const formattedCode = prettify(code, { organizeImportsSkipDestructiveCodeActions: true });
const formattedCode = await prettify(code, { organizeImportsSkipDestructiveCodeActions: true });

t.is(formattedCode, code);
});
24 changes: 12 additions & 12 deletions test/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('ava').default;
const ts = require('typescript');
const { prettify } = require('./_utils');

test('works with TypeScript code inside Vue files', (t) => {
test('works with TypeScript code inside Vue files', async (t) => {
const code = `
<script lang="ts">
import {defineComponent,compile} from 'vue';
Expand All @@ -11,25 +11,25 @@ test('works with TypeScript code inside Vue files', (t) => {
</script>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode.split('\n')[1], `import { compile, defineComponent } from "vue";`);
});

test('works with Vue setup scripts', (t) => {
test('works with Vue setup scripts', async (t) => {
const code = `
<script setup lang="ts">
import {defineComponent,compile} from 'vue';
export default defineComponent({});
</script>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode.split('\n')[1], `import { defineComponent } from "vue";`);
});

test('preserves new lines and comments in Vue files', (t) => {
test('preserves new lines and comments in Vue files', async (t) => {
const code = `<script lang="ts">
import { defineComponent, ref } from "vue";
export default defineComponent({
Expand All @@ -45,12 +45,12 @@ export default defineComponent({
<style></style>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode, code);
});

test('does not remove imports when Vue components use kebab case', (t) => {
test('does not remove imports when Vue components use kebab case', async (t) => {
const code = `<template>
<div>
<n-divider />
Expand All @@ -62,12 +62,12 @@ import { NDivider } from "naive-ui";
</script>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode, code);
});

test('works with pug templates in Vue files', (t) => {
test('works with pug templates in Vue files', async (t) => {
const code = `<script setup lang="ts">
import { Foo, Bar } from "@/components";
</script>
Expand All @@ -86,12 +86,12 @@ Foo
</template>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode, expected);
});

test.serial('works with Volar language plugins when not running from the project root', (t) => {
test.serial('works with Volar language plugins when not running from the project root', async (t) => {
const originalGetCurrentDir = ts.sys.getCurrentDirectory;

ts.sys.getCurrentDirectory = () => '/';
Expand All @@ -114,7 +114,7 @@ Foo
</template>
`;

const formattedCode = prettify(code, { filepath: 'file.vue' });
const formattedCode = await prettify(code, { filepath: 'file.vue' });

t.is(formattedCode, expected);

Expand Down

0 comments on commit b15df6a

Please sign in to comment.