Skip to content

Commit 2d33dbb

Browse files
rcooprAdrianGonz97
andauthored
Tailwindcss default export should use satisfies instead of as (#231)
* build: update ast-types required to include `AstTypes.TSSatisfiesExpression` * feat: add `typeAnnotateSatisfiesExpression` creates an expression of the form `{ } satsfies XXX` * fix: use satisfies in tailwind config for type hints * fix: restore type import * revert package.json spacing changes * fix and tweak * not needed * annoying * tweak name * changeset --------- Co-authored-by: Ross Cooper <[email protected]> Co-authored-by: AdrianGonz97 <[email protected]>
1 parent aa23f44 commit 2d33dbb

File tree

9 files changed

+31
-20
lines changed

9 files changed

+31
-20
lines changed

.changeset/young-mice-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: use `satisfies` instead of `as` in `tailwindcss` config

packages/adders/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function addEslintConfigPrettier({ content }: FileEditor<Record<string, Q
1717
let svelteImportName: string;
1818
for (const specifier of sveltePluginImport?.specifiers ?? []) {
1919
if (specifier.type === 'ImportDefaultSpecifier' && specifier.local?.name) {
20-
svelteImportName = specifier.local.name;
20+
svelteImportName = specifier.local.name as string;
2121
}
2222
}
2323

packages/adders/tailwindcss/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineAdder, defineAdderOptions, type PackageDefinition } from '@sveltejs/cli-core';
22
import { addImports } from '@sveltejs/cli-core/css';
3-
import { array, common, exports, imports, object, type AstTypes } from '@sveltejs/cli-core/js';
3+
import { array, common, exports, imports, object } from '@sveltejs/cli-core/js';
44
import { parseCss, parseScript, parseJson, parseSvelte } from '@sveltejs/cli-core/parsers';
55
import { addSlot } from '@sveltejs/cli-core/html';
66

@@ -80,17 +80,18 @@ export default defineAdder({
8080
const rootExport = object.createEmpty();
8181
if (typescript) {
8282
imports.addNamed(ast, 'tailwindcss', { Config: 'Config' }, true);
83-
root = common.typeAnnotateExpression(rootExport, 'Config');
83+
root = common.satisfiesExpression(rootExport, 'Config');
8484
}
8585

8686
const { astNode: exportDeclaration, value: node } = exports.defaultExport(
8787
ast,
8888
root ?? rootExport
8989
);
9090

91-
const config = (
92-
node.type === 'TSAsExpression' ? node.expression : node
93-
) as AstTypes.ObjectExpression;
91+
const config = node.type === 'TSSatisfiesExpression' ? node.expression : node;
92+
if (config.type !== 'ObjectExpression') {
93+
throw new Error(`Unexpected tailwind config shape: ${config.type}`);
94+
}
9495

9596
if (!typescript) {
9697
common.addJsDocTypeComment(exportDeclaration, "import('tailwindcss').Config");

packages/adders/vitest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default defineAdder({
8686
const importSpecifier = defineConfigImportDecl?.specifiers?.find(
8787
(sp) => sp.type === 'ImportSpecifier' && sp.imported.name === 'defineConfig'
8888
);
89-
const defineConfigAlias = importSpecifier?.local?.name ?? 'defineConfig';
89+
const defineConfigAlias = (importSpecifier?.local?.name ?? 'defineConfig') as string;
9090
imports.addNamed(ast, 'vitest/config', { defineConfig: defineConfigAlias });
9191

9292
object.properties(defaultExport.value.arguments[0], { test });

packages/ast-tooling/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import * as fleece from 'silver-fleece';
1717
import * as Walker from 'zimmerframe';
1818
import type { namedTypes as AstTypes } from 'ast-types';
19-
import type * as AstKinds from 'ast-types/gen/kinds.d.ts';
19+
import type * as AstKinds from 'ast-types/gen/kinds';
2020

2121
/**
2222
* Most of the AST tooling is pretty big in bundle size and bundling takes forever.

packages/ast-tooling/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"devDependencies": {
2727
"@babel/parser": "^7.24.7",
28-
"ast-types": "^0.14.2",
28+
"ast-types": "^0.16.1",
2929
"dom-serializer": "^2.0.0",
3030
"domhandler": "^5.0.3",
3131
"domutils": "^3.1.0",

packages/core/tooling/js/common.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ export function typeAnnotateExpression(
5454
return expression;
5555
}
5656

57+
export function satisfiesExpression(
58+
node: AstKinds.ExpressionKind,
59+
type: string
60+
): AstTypes.TSSatisfiesExpression {
61+
const expression: AstTypes.TSSatisfiesExpression = {
62+
type: 'TSSatisfiesExpression',
63+
expression: node,
64+
typeAnnotation: { type: 'TSTypeReference', typeName: { type: 'Identifier', name: type } }
65+
};
66+
67+
return expression;
68+
}
69+
5770
export function createSpreadElement(expression: AstKinds.ExpressionKind): AstTypes.SpreadElement {
5871
return {
5972
type: 'SpreadElement',

packages/core/tooling/js/kit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function addHooksHandle(
8585
if (handleSpecifier) {
8686
isSpecifier = true;
8787
// we'll search for the local name in case it's aliased (e.g. `export { foo as handle }`)
88-
handleName = handleSpecifier.local?.name ?? handleSpecifier.exported.name;
88+
handleName = (handleSpecifier.local?.name ?? handleSpecifier.exported.name) as string;
8989

9090
// find the definition
9191
const handleFunc = ast.body.find((n) => isFunctionDeclaration(n, handleName));

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)