From ab0abcf8ff3a4f0c916397a3878aab35aba90153 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 7 Oct 2024 11:38:01 +0200 Subject: [PATCH] Pretty print `!important` in declarations (#14611) This PR is a very small improvement. We started pretty printing the generated CSS (proper indentation) a while ago, so that we can use the output as-is for intellisense (on hover). The other day I noticed that when you use `!important` that we attach it directly to the declaration. Not the end of the world, but this PR injects a little space to make sure that the `!important` is separated from the value which makes it a little easier to read and looks more like what you would write by hand. Before: ```css .flex\! { display: flex!important; } ``` After: ```css .flex\! { display: flex !important; } ``` --- CHANGELOG.md | 1 + packages/tailwindcss/src/ast.ts | 2 +- packages/tailwindcss/src/compat/config.test.ts | 6 +++--- packages/tailwindcss/src/important.test.ts | 8 ++++---- packages/tailwindcss/src/intellisense.test.ts | 4 ++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27613adc0..8de5d08e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for `tailwindcss/colors.js`, `tailwindcss/defaultTheme.js`, and `tailwindcss/plugin.js` exports ([#14595](https://github.com/tailwindlabs/tailwindcss/pull/14595)) - Support `keyframes` in JS config file themes ([14594](https://github.com/tailwindlabs/tailwindcss/pull/14594)) +- Pretty print `!important` in declarations ([#14611](https://github.com/tailwindlabs/tailwindcss/pull/14611)) ### Fixed diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index afc1888c2..c34a3d5af 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -206,7 +206,7 @@ export function toCss(ast: AstNode[]) { // Declaration else if (node.property !== '--tw-sort' && node.value !== undefined && node.value !== null) { - css += `${indent}${node.property}: ${node.value}${node.important ? '!important' : ''};\n` + css += `${indent}${node.property}: ${node.value}${node.important ? ' !important' : ''};\n` } return css diff --git a/packages/tailwindcss/src/compat/config.test.ts b/packages/tailwindcss/src/compat/config.test.ts index 8660808c1..b174895d6 100644 --- a/packages/tailwindcss/src/compat/config.test.ts +++ b/packages/tailwindcss/src/compat/config.test.ts @@ -1428,15 +1428,15 @@ test('important: true', async () => { expect(compiler.build(['underline', 'hover:line-through', 'custom'])).toMatchInlineSnapshot(` ".custom { - color: red!important; + color: red !important; } .underline { - text-decoration-line: underline!important; + text-decoration-line: underline !important; } .hover\\:line-through { &:hover { @media (hover: hover) { - text-decoration-line: line-through!important; + text-decoration-line: line-through !important; } } } diff --git a/packages/tailwindcss/src/important.test.ts b/packages/tailwindcss/src/important.test.ts index fe7debf20..8867062e8 100644 --- a/packages/tailwindcss/src/important.test.ts +++ b/packages/tailwindcss/src/important.test.ts @@ -45,12 +45,12 @@ test('Utilities can be marked with important', async () => { expect(compiler.build(['underline', 'hover:line-through'])).toMatchInlineSnapshot(` ".underline { - text-decoration-line: underline!important; + text-decoration-line: underline !important; } .hover\\:line-through { &:hover { @media (hover: hover) { - text-decoration-line: line-through!important; + text-decoration-line: line-through !important; } } } @@ -74,12 +74,12 @@ test('Utilities can be wrapped with a selector and marked as important', async ( expect(compiler.build(['underline', 'hover:line-through'])).toMatchInlineSnapshot(` "#app { .underline { - text-decoration-line: underline!important; + text-decoration-line: underline !important; } .hover\\:line-through { &:hover { @media (hover: hover) { - text-decoration-line: line-through!important; + text-decoration-line: line-through !important; } } } diff --git a/packages/tailwindcss/src/intellisense.test.ts b/packages/tailwindcss/src/intellisense.test.ts index c61d22951..9a76cceba 100644 --- a/packages/tailwindcss/src/intellisense.test.ts +++ b/packages/tailwindcss/src/intellisense.test.ts @@ -160,13 +160,13 @@ test('Utilities, when marked as important, show as important in intellisense', a expect(design.candidatesToCss(['underline', 'hover:line-through'])).toMatchInlineSnapshot(` [ ".underline { - text-decoration-line: underline!important; + text-decoration-line: underline !important; } ", ".hover\\:line-through { &:hover { @media (hover: hover) { - text-decoration-line: line-through!important; + text-decoration-line: line-through !important; } } }