-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from taiga-family/compatibility
fix(backticks): add compatibility with @trivago/prettier-plugin-sort-imports
- Loading branch information
Showing
12 changed files
with
337 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default Symbol('PrettierPluginBackticks'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters