Skip to content

Commit 6c5882c

Browse files
chore: switch to estree compatible ast tooling (#380)
* use `acorn` and `estree` * update tests * fix most type errors * update snapshots * remove useless function * fix * fix check * update esrap * fix * fixes * remove now duplicated code * fix sveltekit-adapter addon * fix lucia * update `esrap` * update `esrap` * remove unused code * start work on indendation * migrate to `@sveltejs/acorn-typescript` * make it work * changeset * fix eslint quotes * fixes * remove useless import * update tests * remove todo * changeset for code formatting * dedupe * should be evaluating the type of the `value` * tweaks * simplify * use strict equality * simplify * fix and add missing test * simplify more * tweak * re-export `CssAst` from `cli-core/css` * fix arrowFunction * add eslint strict equality rule * removed unused interface * revert * improve comments * fix eslint --------- Co-authored-by: AdrianGonz97 <[email protected]>
1 parent 8b4f2bb commit 6c5882c

File tree

78 files changed

+933
-619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+933
-619
lines changed

.changeset/little-otters-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': minor
3+
---
4+
5+
feat: enhanced code generation for more intuitive formatting

.changeset/olive-hotels-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': minor
3+
---
4+
5+
chore: switch to `estree` compatible ast tooling

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default [
1717
}
1818
},
1919
rules: {
20+
eqeqeq: 'error',
2021
'@typescript-eslint/await-thenable': 'error',
2122
'@typescript-eslint/no-unused-expressions': 'off',
2223
'@typescript-eslint/require-await': 'error'

packages/addons/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function addEslintConfigPrettier(content: string): string {
3939
if (!common.hasNode(eslintConfig, configSpread)) nodesToInsert.push(configSpread);
4040

4141
const elements =
42-
eslintConfig.type == 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
42+
eslintConfig.type === 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
4343
// finds index of `...svelte.configs["..."]`
4444
const idx = elements.findIndex(
4545
(el) =>

packages/addons/drizzle/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ export default defineAddon({
199199
url: common.expressionFromString('process.env.DATABASE_URL'),
200200
authToken
201201
}),
202-
verbose: { type: 'BooleanLiteral', value: true },
203-
strict: { type: 'BooleanLiteral', value: true }
202+
verbose: { type: 'Literal', value: true },
203+
strict: { type: 'Literal', value: true }
204204
});
205205

206206
const dialect = options.sqlite === 'turso' ? 'turso' : options.database;
@@ -338,8 +338,8 @@ export default defineAddon({
338338
const paramObject = object.create({
339339
schema: variables.identifier('schema')
340340
});
341-
if (options.database == 'mysql') {
342-
const mode = options.mysql == 'planetscale' ? 'planetscale' : 'default';
341+
if (options.database === 'mysql') {
342+
const mode = options.mysql === 'planetscale' ? 'planetscale' : 'default';
343343
object.property(paramObject, 'mode', common.createLiteral(mode));
344344
}
345345
drizzleCall.arguments.push(paramObject);

packages/addons/eslint/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
functions,
88
imports,
99
object,
10-
type AstKinds,
1110
type AstTypes
1211
} from '@sveltejs/cli-core/js';
1312
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
@@ -54,14 +53,12 @@ export default defineAddon({
5453
sv.file('eslint.config.js', (content) => {
5554
const { ast, generateCode } = parseScript(content);
5655

57-
const eslintConfigs: Array<
58-
AstKinds.ExpressionKind | AstTypes.SpreadElement | AstTypes.ObjectExpression
59-
> = [];
56+
const eslintConfigs: Array<AstTypes.Expression | AstTypes.SpreadElement> = [];
6057

6158
imports.addDefault(ast, './svelte.config.js', 'svelteConfig');
6259

6360
const gitIgnorePathStatement = common.statementFromString(
64-
'\nconst gitignorePath = fileURLToPath(new URL("./.gitignore", import.meta.url));'
61+
"\nconst gitignorePath = fileURLToPath(new URL('./.gitignore', import.meta.url));"
6562
);
6663
common.addStatement(ast, gitIgnorePathStatement);
6764

@@ -90,16 +87,19 @@ export default defineAddon({
9087
'"no-undef"': off
9188
});
9289

93-
if (rules.properties[0].type !== 'ObjectProperty') {
94-
throw new Error('rules.properties[0].type !== "ObjectProperty"');
90+
if (rules.properties[0].type !== 'Property') {
91+
throw new Error('rules.properties[0].type !== "Property"');
9592
}
96-
rules.properties[0].key.comments = [
93+
rules.properties[0].key.leadingComments = [
9794
{
98-
type: 'Block',
95+
type: 'Line',
9996
value:
100-
'*\n * typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.\n * see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors\n ',
101-
leading: true,
102-
trailing: false
97+
' typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.'
98+
},
99+
{
100+
type: 'Line',
101+
value:
102+
' see: https://typescript-eslint.io/troubleshooting/faqs/eslint/#i-get-errors-from-the-no-undef-rule-about-global-variables-not-being-defined-even-though-there-are-no-typescript-errors'
103103
}
104104
];
105105

@@ -114,8 +114,8 @@ export default defineAddon({
114114

115115
if (typescript) {
116116
const svelteTSParserConfig = object.create({
117-
files: common.expressionFromString('["**/*.svelte", "**/*.svelte.ts", "**/*.svelte.js"]'),
118-
ignores: common.expressionFromString('["eslint.config.js", "svelte.config.js"]'),
117+
files: common.expressionFromString("['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js']"),
118+
ignores: common.expressionFromString("['eslint.config.js', 'svelte.config.js']"),
119119
languageOptions: object.create({
120120
parserOptions: object.create({
121121
projectService: common.expressionFromString('true'),
@@ -128,7 +128,7 @@ export default defineAddon({
128128
eslintConfigs.push(svelteTSParserConfig);
129129
} else {
130130
const svelteTSParserConfig = object.create({
131-
files: common.expressionFromString('["**/*.svelte", "**/*.svelte.js"]'),
131+
files: common.expressionFromString("['**/*.svelte', '**/*.svelte.js']"),
132132
languageOptions: object.create({
133133
parserOptions: object.create({
134134
svelteConfig: common.expressionFromString('svelteConfig')

packages/addons/lucia/index.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,27 @@ export default defineAddon({
5555

5656
sv.file(`drizzle.config.${ext}`, (content) => {
5757
const { ast, generateCode } = parseScript(content);
58-
const isProp = (name: string, node: AstTypes.ObjectProperty) =>
58+
const isProp = (name: string, node: AstTypes.Property) =>
5959
node.key.type === 'Identifier' && node.key.name === name;
6060

61-
// prettier-ignore
62-
Walker.walk(ast as AstTypes.ASTNode, {}, {
63-
ObjectProperty(node) {
64-
if (isProp('dialect', node) && node.value.type === 'StringLiteral') {
61+
Walker.walk(ast as AstTypes.Node, null, {
62+
Property(node) {
63+
if (
64+
isProp('dialect', node) &&
65+
node.value.type === 'Literal' &&
66+
typeof node.value.value === 'string'
67+
) {
6568
drizzleDialect = node.value.value as Dialect;
6669
}
67-
if (isProp('schema', node) && node.value.type === 'StringLiteral') {
70+
if (
71+
isProp('schema', node) &&
72+
node.value.type === 'Literal' &&
73+
typeof node.value.value === 'string'
74+
) {
6875
schemaPath = node.value.value;
6976
}
7077
}
71-
})
78+
});
7279

7380
if (!drizzleDialect) {
7481
throw new Error('Failed to detect DB dialect in your `drizzle.config.[js|ts]` file');
@@ -601,13 +608,14 @@ function createLuciaType(name: string): AstTypes.TSInterfaceBody['body'][number]
601608
type: 'Identifier',
602609
name
603610
},
611+
computed: false,
604612
typeAnnotation: {
605613
type: 'TSTypeAnnotation',
606614
typeAnnotation: {
607615
type: 'TSIndexedAccessType',
608616
objectType: {
609617
type: 'TSImportType',
610-
argument: { type: 'StringLiteral', value: '$lib/server/auth' },
618+
argument: { type: 'Literal', value: '$lib/server/auth' },
611619
qualifier: {
612620
type: 'Identifier',
613621
name: 'SessionValidationResult'
@@ -616,7 +624,7 @@ function createLuciaType(name: string): AstTypes.TSInterfaceBody['body'][number]
616624
indexType: {
617625
type: 'TSLiteralType',
618626
literal: {
619-
type: 'StringLiteral',
627+
type: 'Literal',
620628
value: name
621629
}
622630
}
@@ -649,14 +657,13 @@ function getAuthHandleContent() {
649657
};`;
650658
}
651659

652-
function getCallExpression(ast: AstTypes.ASTNode): AstTypes.CallExpression | undefined {
660+
function getCallExpression(ast: AstTypes.Node): AstTypes.CallExpression | undefined {
653661
let callExpression;
654662

655-
// prettier-ignore
656-
Walker.walk(ast, {}, {
663+
Walker.walk(ast, null, {
657664
CallExpression(node) {
658665
callExpression ??= node;
659-
},
666+
}
660667
});
661668

662669
return callExpression;

packages/addons/sveltekit-adapter/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ export default defineAddon({
7171
if (adapterImportDecl) {
7272
// replaces the import's source with the new adapter
7373
adapterImportDecl.source.value = adapter.package;
74+
// reset raw value, so that the string is re-generated
75+
adapterImportDecl.source.raw = undefined;
76+
7477
adapterName = adapterImportDecl.specifiers?.find((s) => s.type === 'ImportDefaultSpecifier')
7578
?.local?.name as string;
7679
} else {
@@ -79,16 +82,15 @@ export default defineAddon({
7982

8083
const { value: config } = exports.defaultExport(ast, object.createEmpty());
8184
const kitConfig = config.properties.find(
82-
(p) => p.type === 'ObjectProperty' && p.key.type === 'Identifier' && p.key.name === 'kit'
83-
) as AstTypes.ObjectProperty | undefined;
85+
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'kit'
86+
) as AstTypes.Property | undefined;
8487

8588
if (kitConfig && kitConfig.value.type === 'ObjectExpression') {
8689
const adapterProp = kitConfig.value.properties.find(
87-
(p) =>
88-
p.type === 'ObjectProperty' && p.key.type === 'Identifier' && p.key.name === 'adapter'
90+
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'adapter'
8991
);
9092
if (adapterProp) {
91-
adapterProp.comments = [];
93+
adapterProp.leadingComments = [];
9294
}
9395

9496
// only overrides the `adapter` property so we can reset it's args

0 commit comments

Comments
 (0)