Skip to content

Commit 9f3cc15

Browse files
jycouetmanuel3108
andauthored
chore(core): streamline object helpers (#685)
* bring ensureNestedProperty * rmv `addProperties` * expose less & align more * update lucia * playwrite * adapter * refacto to use overrideProperty & path * discard one change that is not part * linting friend * changeset * rmv leading comment * have a way to transform property * fix naming * remove unused `removeProperty` * thx types! * use `object.create` like syntax * add transformProperty <3 * pos * reduce types * focus object test of object * check * playwrite using the latest syntax * adding another test case * adding tests for auto adapter * let's confirm that only the value is changed. YES * adapter okay * add propertyNode * simplify api by removing transformProperty! You want to go raw, use object.properyNode(). * Check import & comments * ;) * Opti tests, no need preview when page not requested. * Revert "Opti tests, no need preview when page not requested." This reverts commit 52961f0. --------- Co-authored-by: Manuel Serret <[email protected]>
1 parent ab57f4b commit 9f3cc15

File tree

20 files changed

+226
-200
lines changed

20 files changed

+226
-200
lines changed

.changeset/eighty-deer-bake.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+
chore(core): streamline object helpers

packages/addons/_tests/sveltekit-adapter/test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => {
1616
ctx.onTestFinished(async () => await close());
1717

1818
expect(await readFile(join(cwd, 'svelte.config.js'), 'utf8')).not.toMatch('adapter-auto');
19+
expect(await readFile(join(cwd, 'svelte.config.js'), 'utf8')).not.toMatch(
20+
'adapter-auto only supports some environments'
21+
);
22+
});
23+
24+
test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => {
25+
const cwd = await ctx.run(variant, { [addonId]: { adapter: 'auto' } });
26+
27+
const { close } = await prepareServer({ cwd, page });
28+
// kill server process when we're done
29+
ctx.onTestFinished(async () => await close());
30+
31+
expect(await readFile(join(cwd, 'svelte.config.js'), 'utf8')).toMatch('adapter-auto');
32+
expect(await readFile(join(cwd, 'svelte.config.js'), 'utf8')).toMatch(
33+
'adapter-auto only supports some environments'
34+
);
1935
});

packages/addons/lucia/index.ts

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,20 @@ export default defineAddon({
145145
imports: ['sqliteTable', 'text', 'integer']
146146
});
147147
js.object.overrideProperties(userAttributes, {
148-
properties: {
149-
id: js.common.parseExpression("text('id').primaryKey()")
150-
}
148+
id: js.common.parseExpression("text('id').primaryKey()")
151149
});
152150
if (options.demo) {
153151
js.object.overrideProperties(userAttributes, {
154-
properties: {
155-
username: js.common.parseExpression("text('username').notNull().unique()"),
156-
passwordHash: js.common.parseExpression("text('password_hash').notNull()")
157-
}
152+
username: js.common.parseExpression("text('username').notNull().unique()"),
153+
passwordHash: js.common.parseExpression("text('password_hash').notNull()")
158154
});
159155
}
160156
js.object.overrideProperties(sessionAttributes, {
161-
properties: {
162-
id: js.common.parseExpression("text('id').primaryKey()"),
163-
userId: js.common.parseExpression(
164-
"text('user_id').notNull().references(() => user.id)"
165-
),
166-
expiresAt: js.common.parseExpression(
167-
"integer('expires_at', { mode: 'timestamp' }).notNull()"
168-
)
169-
}
157+
id: js.common.parseExpression("text('id').primaryKey()"),
158+
userId: js.common.parseExpression("text('user_id').notNull().references(() => user.id)"),
159+
expiresAt: js.common.parseExpression(
160+
"integer('expires_at', { mode: 'timestamp' }).notNull()"
161+
)
170162
});
171163
}
172164
if (drizzleDialect === 'mysql') {
@@ -175,30 +167,24 @@ export default defineAddon({
175167
imports: ['mysqlTable', 'varchar', 'datetime']
176168
});
177169
js.object.overrideProperties(userAttributes, {
178-
properties: {
179-
id: js.common.parseExpression("varchar('id', { length: 255 }).primaryKey()")
180-
}
170+
id: js.common.parseExpression("varchar('id', { length: 255 }).primaryKey()")
181171
});
182172
if (options.demo) {
183173
js.object.overrideProperties(userAttributes, {
184-
properties: {
185-
username: js.common.parseExpression(
186-
"varchar('username', { length: 32 }).notNull().unique()"
187-
),
188-
passwordHash: js.common.parseExpression(
189-
"varchar('password_hash', { length: 255 }).notNull()"
190-
)
191-
}
174+
username: js.common.parseExpression(
175+
"varchar('username', { length: 32 }).notNull().unique()"
176+
),
177+
passwordHash: js.common.parseExpression(
178+
"varchar('password_hash', { length: 255 }).notNull()"
179+
)
192180
});
193181
}
194182
js.object.overrideProperties(sessionAttributes, {
195-
properties: {
196-
id: js.common.parseExpression("varchar('id', { length: 255 }).primaryKey()"),
197-
userId: js.common.parseExpression(
198-
"varchar('user_id', { length: 255 }).notNull().references(() => user.id)"
199-
),
200-
expiresAt: js.common.parseExpression("datetime('expires_at').notNull()")
201-
}
183+
id: js.common.parseExpression("varchar('id', { length: 255 }).primaryKey()"),
184+
userId: js.common.parseExpression(
185+
"varchar('user_id', { length: 255 }).notNull().references(() => user.id)"
186+
),
187+
expiresAt: js.common.parseExpression("datetime('expires_at').notNull()")
202188
});
203189
}
204190
if (drizzleDialect === 'postgresql') {
@@ -207,28 +193,20 @@ export default defineAddon({
207193
imports: ['pgTable', 'text', 'timestamp']
208194
});
209195
js.object.overrideProperties(userAttributes, {
210-
properties: {
211-
id: js.common.parseExpression("text('id').primaryKey()")
212-
}
196+
id: js.common.parseExpression("text('id').primaryKey()")
213197
});
214198
if (options.demo) {
215199
js.object.overrideProperties(userAttributes, {
216-
properties: {
217-
username: js.common.parseExpression("text('username').notNull().unique()"),
218-
passwordHash: js.common.parseExpression("text('password_hash').notNull()")
219-
}
200+
username: js.common.parseExpression("text('username').notNull().unique()"),
201+
passwordHash: js.common.parseExpression("text('password_hash').notNull()")
220202
});
221203
}
222204
js.object.overrideProperties(sessionAttributes, {
223-
properties: {
224-
id: js.common.parseExpression("text('id').primaryKey()"),
225-
userId: js.common.parseExpression(
226-
"text('user_id').notNull().references(() => user.id)"
227-
),
228-
expiresAt: js.common.parseExpression(
229-
"timestamp('expires_at', { withTimezone: true, mode: 'date' }).notNull()"
230-
)
231-
}
205+
id: js.common.parseExpression("text('id').primaryKey()"),
206+
userId: js.common.parseExpression("text('user_id').notNull().references(() => user.id)"),
207+
expiresAt: js.common.parseExpression(
208+
"timestamp('expires_at', { withTimezone: true, mode: 'date' }).notNull()"
209+
)
232210
});
233211
}
234212

packages/addons/mdsvex/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export default defineAddon({
3030
const previousElement = preprocessorArray;
3131
preprocessorArray = array.create();
3232
array.append(preprocessorArray, previousElement);
33-
object.overrideProperty(exportDefault, {
34-
name: 'preprocess',
35-
value: preprocessorArray
33+
object.overrideProperties(exportDefault, {
34+
preprocess: preprocessorArray
3635
});
3736
}
3837

packages/addons/playwright/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,23 @@ export default defineAddon({
4949
const { value: defaultExport } = exports.createDefault(ast, { fallback: defineConfig });
5050

5151
const config = {
52-
webServer: object.create({
52+
webServer: {
5353
command: 'npm run build && npm run preview',
5454
port: 4173
55-
}),
56-
testDir: common.createLiteral('e2e')
55+
},
56+
testDir: 'e2e'
5757
};
5858

5959
if (
6060
defaultExport.type === 'CallExpression' &&
6161
defaultExport.arguments[0]?.type === 'ObjectExpression'
6262
) {
6363
// uses the `defineConfig` helper
64-
imports.addNamed(ast, {
65-
from: '@playwright/test',
66-
imports: ['defineConfig']
67-
});
68-
object.addProperties(defaultExport.arguments[0], { properties: config });
64+
imports.addNamed(ast, { imports: ['defineConfig'], from: '@playwright/test' });
65+
object.overrideProperties(defaultExport.arguments[0], config);
6966
} else if (defaultExport.type === 'ObjectExpression') {
70-
// if the config is just an object expression, just add the property
71-
object.addProperties(defaultExport, { properties: config });
67+
// if the config is just an object expression, just add the properties
68+
object.overrideProperties(defaultExport, config);
7269
} else {
7370
// unexpected config shape
7471
log.warn('Unexpected playwright config for playwright add-on. Could not update.');

packages/addons/sveltekit-adapter/index.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core';
2-
import { exports, functions, imports, object, type AstTypes } from '@sveltejs/cli-core/js';
2+
import { exports, functions, imports, object } from '@sveltejs/cli-core/js';
33
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
44

55
const adapters = [
@@ -74,33 +74,20 @@ export default defineAddon({
7474
}
7575

7676
const { value: config } = exports.createDefault(ast, { fallback: object.create({}) });
77-
const kitConfig = config.properties.find(
78-
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'kit'
79-
) as AstTypes.Property | undefined;
8077

81-
if (kitConfig && kitConfig.value.type === 'ObjectExpression') {
82-
const adapterProp = kitConfig.value.properties.find(
83-
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'adapter'
84-
);
85-
if (adapterProp) {
86-
adapterProp.leadingComments = [];
78+
// override the adapter property
79+
object.overrideProperties(config, {
80+
kit: {
81+
adapter: functions.createCall({ name: adapterName, args: [], useIdentifiers: true })
8782
}
83+
});
8884

89-
// only overrides the `adapter` property so we can reset it's args
90-
object.overrideProperties(kitConfig.value, {
91-
properties: {
92-
adapter: functions.createCall({ name: adapterName, args: [], useIdentifiers: true })
93-
}
94-
});
95-
} else {
96-
// creates the `kit` property when absent
97-
object.addProperties(config, {
98-
properties: {
99-
kit: object.create({
100-
adapter: functions.createCall({ name: adapterName, args: [], useIdentifiers: true })
101-
})
102-
}
103-
});
85+
// reset the comment for non-auto adapters
86+
if (adapter.package !== '@sveltejs/adapter-auto') {
87+
const fallback = object.create({});
88+
const cfgKitValue = object.property(config, { name: 'kit', fallback });
89+
const cfgAdapter = object.propertyNode(cfgKitValue, { name: 'adapter', fallback });
90+
cfgAdapter.leadingComments = [];
10491
}
10592

10693
return generateCode();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const test = {
2+
a: {
3+
/** a comment */
4+
keep: 'me'
5+
}
6+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const test = { a: { /** a comment */ keep: 'you', b: { c: '007' } } };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { object, type AstTypes } from '@sveltejs/cli-core/js';
2+
import { getTestObjectExpression } from '../objectTestHelper.ts';
3+
4+
export function run(ast: AstTypes.Program): void {
5+
const obj = getTestObjectExpression(ast);
6+
7+
object.overrideProperties(obj, {
8+
a: { b: { c: '007' } }
9+
});
10+
11+
object.overrideProperties(obj, {
12+
a: { keep: 'you' }
13+
});
14+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { variables, object, type AstTypes } from '@sveltejs/cli-core/js';
22

3-
export function run(ast: AstTypes.Program): void {
3+
export const getTestObjectExpression = (ast: AstTypes.Program): AstTypes.ObjectExpression => {
44
const variable = variables.declaration(ast, {
55
kind: 'const',
66
name: 'test',
77
value: object.create({})
88
});
9+
910
const objectDeclarator = variable.declarations[0] as AstTypes.VariableDeclarator;
1011
const objectExpression = objectDeclarator.init as AstTypes.ObjectExpression;
11-
object.removeProperty(objectExpression, { name: 'foo' });
12-
object.removeProperty(objectExpression, { name: 'bar' });
13-
}
12+
13+
return objectExpression;
14+
};

0 commit comments

Comments
 (0)