Skip to content

Commit

Permalink
Merge pull request #17 from taiga-family/compatibility
Browse files Browse the repository at this point in the history
fix(backticks): add compatibility with @trivago/prettier-plugin-sort-imports
  • Loading branch information
MillerSvt authored Apr 4, 2024
2 parents 6f194d1 + be4ed7e commit 685dc15
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 48 deletions.
4 changes: 4 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ npx prettier --write .
# или
yarn prettier --write .
```

## Совместимые плагины

* `@trivago/prettier-plugin-sort-imports`
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ npx prettier --write .
# or
yarn prettier --write .
```

## Compatible plugins

* `@trivago/prettier-plugin-sort-imports`
264 changes: 220 additions & 44 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.3",
"description": "",
"scripts": {
"test": "tsc --noEmit && node --import tsx --test test/fixtures.spec.ts",
"test": "tsc --noEmit && node --import tsx --test test/*.spec.ts",
"lint": "eslint .",
"build": "tsc",
"prepare": "husky",
Expand All @@ -14,6 +14,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@taiga-ui/eslint-plugin-experience": "^0.67.10",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/node": "^20.11.29",
"eslint": "^8.57.0",
"husky": "^9.0.11",
Expand Down
1 change: 1 addition & 0 deletions src/constants/plugin-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default Symbol('PrettierPluginBackticks');
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@ import type { Plugin } from 'prettier';
import babelParsers from 'prettier/parser-babel';
import typescriptParsers from 'prettier/parser-typescript';

import pluginKey from './constants/plugin-key.js';
import printers from './printers.js';
import getParentParser from './utils/get-parent-parser.js';

export default {
parsers: {
babel: babelParsers.parsers.babel,
typescript: typescriptParsers.parsers.typescript,
babel: {
...babelParsers.parsers.babel,
preprocess: (text, options) => {
const parentParser = getParentParser(options);

return parentParser?.preprocess(text, options) ?? text;
},
},
typescript: {
...typescriptParsers.parsers.typescript,
preprocess: (text, options) => {
const parentParser = getParentParser(options);

return parentParser?.preprocess?.(text, options) ?? text;
},
},
},
printers,
[pluginKey]: true,
} satisfies Plugin;
9 changes: 9 additions & 0 deletions src/utils/assert-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ParserOptions, Plugin } from 'prettier';

export function assertPlugins(
plugins: ParserOptions['plugins'],
): asserts plugins is Plugin[] {
if (plugins.some((plugin) => typeof plugin === 'string')) {
throw new Error('Assertion error: plugin must not be a string');
}
}
15 changes: 15 additions & 0 deletions src/utils/get-current-plugin-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Plugin } from 'prettier';

import isCurrentPlugin from './is-current-plugin.js';

export default function (plugins: Plugin[]): number {
const currentPlugins = plugins.filter(isCurrentPlugin);

if (currentPlugins.length > 1) {
throw new Error(
'prettier-plugin-backticks initialized multiple times.',
);
}

return plugins.indexOf(currentPlugins[0]);
}
18 changes: 18 additions & 0 deletions src/utils/get-parent-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Parser, ParserOptions } from 'prettier';

import { assertPlugins } from './assert-plugins.js';
import getCurrentPluginIndex from './get-current-plugin-index.js';

export default function ({ parser, plugins }: ParserOptions): Parser {
assertPlugins(plugins);

const currentPluginIndex = getCurrentPluginIndex(plugins);

for (let i = currentPluginIndex - 1; i > 0; i--) {
const parentParser = plugins[i].parsers?.[parser as string];

if (parentParser) {
return parentParser;
}
}
}
7 changes: 7 additions & 0 deletions src/utils/is-current-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Plugin } from 'prettier';

import pluginKey from '../constants/plugin-key.js';

export default function (plugin: Plugin): boolean {
return pluginKey in plugin;
}
36 changes: 36 additions & 0 deletions test/compatibility.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { equal } from 'node:assert';
import { it } from 'node:test';

import { format } from 'prettier';

import Plugin from '../src/index.js';

await it('compatibility with @trivago/prettier-plugin-sort-imports', async () => {
const originCode = `import { B, A } from './b';
import { D, C } from './a';
const a = 'test';
`;
const expectedCode = `import { D, C } from "./a";
import { B, A } from "./b";
const a = \`test\`;
`;

equal(
await format(originCode, {
filepath: 'test.ts',
plugins: ['@trivago/prettier-plugin-sort-imports', Plugin],
}),
expectedCode,
);

equal(
await format(originCode, {
filepath: 'test.ts',
plugins: [Plugin, '@trivago/prettier-plugin-sort-imports'],
}),
expectedCode,
);
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"rootDir": ".",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"declaration": true
"declaration": true,
"lib": ["ES2023"]
},
"files": [
"src/index.ts"
Expand Down

0 comments on commit 685dc15

Please sign in to comment.