Skip to content

Commit 53af959

Browse files
Merge branch 'main' into add-support-for-callable-template-literals
2 parents 3405b6e + 4794bce commit 53af959

10 files changed

Lines changed: 56 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- Don't augment global Prettier `ParserOptions` and `RequiredOptions` types ([#354](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/354))
11+
- Drop support for `prettier-plugin-import-sort` ([#385](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/385))
12+
- Format quotes in `@source`, `@plugin`, and `@config` ([#387](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/387))
1013
- Support sorting in callable template literals ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
1114
- Support sorting in function calls mixed with property accesses ([#367](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/367))
1215

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ Then add the plugin to your [Prettier configuration](https://prettier.io/docs/en
1919
}
2020
```
2121

22+
When using a JavaScript config, you can import the types for IntelliSense:
23+
24+
```js
25+
// prettier.config.js
26+
27+
/** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
28+
export default {
29+
plugins: ["prettier-plugin-tailwindcss"],
30+
}
31+
```
32+
2233
## Upgrading to v0.5.x
2334

2435
As of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means it cannot be loaded via `require()`. For more information see our [upgrade guide](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/issues/207#issuecomment-1698071122).
@@ -215,7 +226,6 @@ This plugin uses Prettier APIs that can only be used by one plugin at a time, ma
215226
- `@trivago/prettier-plugin-sort-imports`
216227
- `prettier-plugin-astro`
217228
- `prettier-plugin-css-order`
218-
- `prettier-plugin-import-sort`
219229
- `prettier-plugin-jsdoc`
220230
- `prettier-plugin-multiline-arrays`
221231
- `prettier-plugin-organize-attributes`

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"prettier": "^3.6.2",
6262
"prettier-plugin-astro": "^0.14.1",
6363
"prettier-plugin-css-order": "^2.1.2",
64-
"prettier-plugin-import-sort": "^0.0.7",
6564
"prettier-plugin-jsdoc": "^1.3.3",
6665
"prettier-plugin-marko": "^3.3.0",
6766
"prettier-plugin-multiline-arrays": "^4.0.3",
@@ -89,7 +88,6 @@
8988
"prettier": "^3.0",
9089
"prettier-plugin-astro": "*",
9190
"prettier-plugin-css-order": "*",
92-
"prettier-plugin-import-sort": "*",
9391
"prettier-plugin-jsdoc": "*",
9492
"prettier-plugin-marko": "*",
9593
"prettier-plugin-multiline-arrays": "*",
@@ -127,9 +125,6 @@
127125
"prettier-plugin-css-order": {
128126
"optional": true
129127
},
130-
"prettier-plugin-import-sort": {
131-
"optional": true
132-
},
133128
"prettier-plugin-jsdoc": {
134129
"optional": true
135130
},

src/index.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,38 @@ function transformJavaScript(
773773
}
774774

775775
function transformCss(ast: any, { env }: TransformerContext) {
776+
// `parseValue` inside Prettier's CSS parser is private API so we have to
777+
// produce the same result by parsing an import statement with the same params
778+
function tryParseAtRuleParams(name: string, params: any) {
779+
// It might already be an object or array. Could happen in the future if
780+
// Prettier decides to start parsing these.
781+
if (typeof params !== 'string') return params
782+
783+
// Otherwise we let prettier re-parse the params into its custom value AST
784+
// based on postcss-value parser.
785+
try {
786+
let parser = base.parsers.css
787+
let root = parser.parse(`@import ${params};`, env.options)
788+
789+
return root.nodes[0].params
790+
} catch (err) {
791+
console.warn(`[prettier-plugin-tailwindcss] Unable to parse at rule`)
792+
console.warn({ name, params })
793+
console.warn(err)
794+
}
795+
796+
return params
797+
}
798+
776799
ast.walk((node: any) => {
800+
if (
801+
node.name === 'plugin' ||
802+
node.name === 'config' ||
803+
node.name === 'source'
804+
) {
805+
node.params = tryParseAtRuleParams(node.name, node.params)
806+
}
807+
777808
if (node.type === 'css-atrule' && node.name === 'apply') {
778809
let isImportant = /\s+(?:!important|#{(['"]*)!important\1})\s*$/.test(
779810
node.params,
@@ -1275,8 +1306,3 @@ export interface PluginOptions {
12751306
*/
12761307
tailwindPreserveDuplicates?: boolean
12771308
}
1278-
1279-
declare module 'prettier' {
1280-
interface RequiredOptions extends PluginOptions {}
1281-
interface ParserOptions extends PluginOptions {}
1282-
}

src/internal.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export interface InternalOptions {
1+
import type { PluginOptions } from '.'
2+
3+
export interface InternalOptions extends PluginOptions {
24
printer: Printer<any>
35

46
/**

src/options.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { RequiredOptions, SupportOption } from 'prettier'
22
import type { Customizations } from './types'
3-
import './index'
43

54
export const options: Record<string, SupportOption> = {
65
tailwindConfig: {

src/plugins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ async function loadCompatiblePlugins() {
181181
'prettier-plugin-multiline-arrays',
182182
'@ianvs/prettier-plugin-sort-imports',
183183
'@trivago/prettier-plugin-sort-imports',
184-
'prettier-plugin-import-sort',
185184
'prettier-plugin-organize-imports',
186185
'prettier-plugin-sort-imports',
187186

tests/plugins.test.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,6 @@ let tests: PluginTest[] = [
212212
],
213213
},
214214
},
215-
{
216-
// NOTE: This plugin doesn't officially support Prettier v3 but it seems to work fine
217-
plugins: ['prettier-plugin-import-sort'],
218-
tests: {
219-
babel: [
220-
[
221-
`
222-
import './three'
223-
import '@one/file'
224-
import '@two/file'
225-
export default function Foo() { return <div className="sm:p-0 p-4"></div> }
226-
`,
227-
`import '@one/file'\nimport '@two/file'\n\nimport './three'\n\nexport default function Foo() {\n return <div className="p-4 sm:p-0"></div>\n}`,
228-
],
229-
],
230-
},
231-
},
232215
{
233216
plugins: ['prettier-plugin-jsdoc'],
234217
tests: {

tests/tests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ let css: TestEntry[] = [
4343
'@apply p-0\n sm:p-0;',
4444
{ tailwindPreserveWhitespace: true },
4545
],
46+
47+
// Quote conversion for custom at-rules
48+
[`@import "./file.css";`, `@import './file.css';`],
49+
[`@plugin "./file.js";`, `@plugin './file.js';`],
50+
[`@config "./file.js";`, `@config './file.js';`],
51+
[`@source "./file.js";`, `@source './file.js';`],
52+
[`@source not "./file.js";`, `@source not './file.js';`],
53+
[`@source inline("./file.js");`, `@source inline('./file.js');`],
4654
]
4755

4856
export let javascript: TestEntry[] = [

0 commit comments

Comments
 (0)