Skip to content

chore: switch to estree compatible ast tooling #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 49 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
5604669
use `acorn` and `estree`
manuel3108 Dec 29, 2024
6542369
update tests
manuel3108 Dec 29, 2024
d345d91
fix most type errors
manuel3108 Dec 29, 2024
18e937d
update snapshots
manuel3108 Dec 29, 2024
c14011e
remove useless function
manuel3108 Dec 29, 2024
1f12c1f
fix
manuel3108 Dec 29, 2024
3e2ee46
fix check
manuel3108 Dec 31, 2024
72ad646
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Jan 10, 2025
e263f9e
update esrap
manuel3108 Jan 10, 2025
fe2f7a8
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Feb 9, 2025
197e350
fix
manuel3108 Feb 9, 2025
8a5f993
fixes
manuel3108 Feb 9, 2025
4216131
remove now duplicated code
manuel3108 Feb 9, 2025
a37ab52
fix sveltekit-adapter addon
manuel3108 Feb 9, 2025
9a57307
fix lucia
manuel3108 Feb 9, 2025
8b6ab0e
update `esrap`
manuel3108 Feb 11, 2025
2f57480
update `esrap`
manuel3108 Feb 21, 2025
7eacb93
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Feb 21, 2025
97cbf0c
remove unused code
manuel3108 Feb 22, 2025
9d1ad59
start work on indendation
manuel3108 Feb 26, 2025
a5b127d
migrate to `@sveltejs/acorn-typescript`
manuel3108 Feb 26, 2025
4292618
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Mar 7, 2025
0882e55
make it work
manuel3108 Mar 8, 2025
b85118f
changeset
manuel3108 Mar 8, 2025
24cabcf
fix eslint quotes
manuel3108 Mar 8, 2025
b6eb38a
fixes
manuel3108 Mar 8, 2025
0ff29ef
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Mar 9, 2025
609f010
remove useless import
manuel3108 Mar 9, 2025
1d852f5
update tests
manuel3108 Mar 9, 2025
cb64964
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Mar 15, 2025
635f554
remove todo
manuel3108 Mar 16, 2025
280c4bf
changeset for code formatting
manuel3108 Mar 16, 2025
9637514
dedupe
AdrianGonz97 Mar 17, 2025
677043a
should be evaluating the type of the `value`
AdrianGonz97 Mar 17, 2025
f7bf74e
tweaks
AdrianGonz97 Mar 17, 2025
5d896bd
simplify
AdrianGonz97 Mar 17, 2025
b04ea7f
use strict equality
AdrianGonz97 Mar 17, 2025
25ba5bc
simplify
AdrianGonz97 Mar 17, 2025
558bdae
fix and add missing test
AdrianGonz97 Mar 17, 2025
4177386
simplify more
AdrianGonz97 Mar 17, 2025
d8ecab8
tweak
AdrianGonz97 Mar 17, 2025
88c4c90
re-export `CssAst` from `cli-core/css`
manuel3108 Mar 19, 2025
96feb6a
fix arrowFunction
manuel3108 Mar 19, 2025
86aa691
add eslint strict equality rule
manuel3108 Mar 19, 2025
3d313af
removed unused interface
manuel3108 Mar 19, 2025
54aecc9
revert
manuel3108 Mar 19, 2025
2f9fd4f
improve comments
manuel3108 Mar 23, 2025
fd4ec66
Merge branch 'main' into chore/explore-parsers-and-printers
manuel3108 Mar 23, 2025
c850286
fix eslint
manuel3108 Mar 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-otters-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': minor
---

feat: enhanced code generation for more intuitive formatting
5 changes: 5 additions & 0 deletions .changeset/olive-hotels-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': minor
---

chore: switch to `estree` compatible ast tooling
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default [
}
},
rules: {
eqeqeq: 'error',
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/no-unused-expressions': 'off',
'@typescript-eslint/require-await': 'error'
Expand Down
2 changes: 1 addition & 1 deletion packages/addons/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function addEslintConfigPrettier(content: string): string {
if (!common.hasNode(eslintConfig, configSpread)) nodesToInsert.push(configSpread);

const elements =
eslintConfig.type == 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
eslintConfig.type === 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
// finds index of `...svelte.configs["..."]`
const idx = elements.findIndex(
(el) =>
Expand Down
8 changes: 4 additions & 4 deletions packages/addons/drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export default defineAddon({
url: common.expressionFromString('process.env.DATABASE_URL'),
authToken
}),
verbose: { type: 'BooleanLiteral', value: true },
strict: { type: 'BooleanLiteral', value: true }
verbose: { type: 'Literal', value: true },
strict: { type: 'Literal', value: true }
});

const dialect = options.sqlite === 'turso' ? 'turso' : options.database;
Expand Down Expand Up @@ -338,8 +338,8 @@ export default defineAddon({
const paramObject = object.create({
schema: variables.identifier('schema')
});
if (options.database == 'mysql') {
const mode = options.mysql == 'planetscale' ? 'planetscale' : 'default';
if (options.database === 'mysql') {
const mode = options.mysql === 'planetscale' ? 'planetscale' : 'default';
object.property(paramObject, 'mode', common.createLiteral(mode));
}
drizzleCall.arguments.push(paramObject);
Expand Down
30 changes: 15 additions & 15 deletions packages/addons/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
functions,
imports,
object,
type AstKinds,
type AstTypes
} from '@sveltejs/cli-core/js';
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
Expand Down Expand Up @@ -54,14 +53,12 @@ export default defineAddon({
sv.file('eslint.config.js', (content) => {
const { ast, generateCode } = parseScript(content);

const eslintConfigs: Array<
AstKinds.ExpressionKind | AstTypes.SpreadElement | AstTypes.ObjectExpression
> = [];
const eslintConfigs: Array<AstTypes.Expression | AstTypes.SpreadElement> = [];

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

const gitIgnorePathStatement = common.statementFromString(
'\nconst gitignorePath = fileURLToPath(new URL("./.gitignore", import.meta.url));'
"\nconst gitignorePath = fileURLToPath(new URL('./.gitignore', import.meta.url));"
);
common.addStatement(ast, gitIgnorePathStatement);

Expand Down Expand Up @@ -90,16 +87,19 @@ export default defineAddon({
'"no-undef"': off
});

if (rules.properties[0].type !== 'ObjectProperty') {
throw new Error('rules.properties[0].type !== "ObjectProperty"');
if (rules.properties[0].type !== 'Property') {
throw new Error('rules.properties[0].type !== "Property"');
}
rules.properties[0].key.comments = [
rules.properties[0].key.leadingComments = [
{
type: 'Block',
type: 'Line',
value:
'*\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 ',
leading: true,
trailing: false
' typescript-eslint strongly recommend that you do not use the no-undef lint rule on TypeScript projects.'
},
{
type: 'Line',
value:
' 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'
}
];

Expand All @@ -114,8 +114,8 @@ export default defineAddon({

if (typescript) {
const svelteTSParserConfig = object.create({
files: common.expressionFromString('["**/*.svelte", "**/*.svelte.ts", "**/*.svelte.js"]'),
ignores: common.expressionFromString('["eslint.config.js", "svelte.config.js"]'),
files: common.expressionFromString("['**/*.svelte', '**/*.svelte.ts', '**/*.svelte.js']"),
ignores: common.expressionFromString("['eslint.config.js', 'svelte.config.js']"),
languageOptions: object.create({
parserOptions: object.create({
projectService: common.expressionFromString('true'),
Expand All @@ -128,7 +128,7 @@ export default defineAddon({
eslintConfigs.push(svelteTSParserConfig);
} else {
const svelteTSParserConfig = object.create({
files: common.expressionFromString('["**/*.svelte", "**/*.svelte.js"]'),
files: common.expressionFromString("['**/*.svelte', '**/*.svelte.js']"),
languageOptions: object.create({
parserOptions: object.create({
svelteConfig: common.expressionFromString('svelteConfig')
Expand Down
33 changes: 20 additions & 13 deletions packages/addons/lucia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,27 @@ export default defineAddon({

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

// prettier-ignore
Walker.walk(ast as AstTypes.ASTNode, {}, {
ObjectProperty(node) {
if (isProp('dialect', node) && node.value.type === 'StringLiteral') {
Walker.walk(ast as AstTypes.Node, null, {
Property(node) {
if (
isProp('dialect', node) &&
node.value.type === 'Literal' &&
typeof node.value.value === 'string'
) {
drizzleDialect = node.value.value as Dialect;
}
if (isProp('schema', node) && node.value.type === 'StringLiteral') {
if (
isProp('schema', node) &&
node.value.type === 'Literal' &&
typeof node.value.value === 'string'
) {
schemaPath = node.value.value;
}
}
})
});

if (!drizzleDialect) {
throw new Error('Failed to detect DB dialect in your `drizzle.config.[js|ts]` file');
Expand Down Expand Up @@ -601,13 +608,14 @@ function createLuciaType(name: string): AstTypes.TSInterfaceBody['body'][number]
type: 'Identifier',
name
},
computed: false,
typeAnnotation: {
type: 'TSTypeAnnotation',
typeAnnotation: {
type: 'TSIndexedAccessType',
objectType: {
type: 'TSImportType',
argument: { type: 'StringLiteral', value: '$lib/server/auth' },
argument: { type: 'Literal', value: '$lib/server/auth' },
qualifier: {
type: 'Identifier',
name: 'SessionValidationResult'
Expand All @@ -616,7 +624,7 @@ function createLuciaType(name: string): AstTypes.TSInterfaceBody['body'][number]
indexType: {
type: 'TSLiteralType',
literal: {
type: 'StringLiteral',
type: 'Literal',
value: name
}
}
Expand Down Expand Up @@ -649,14 +657,13 @@ function getAuthHandleContent() {
};`;
}

function getCallExpression(ast: AstTypes.ASTNode): AstTypes.CallExpression | undefined {
function getCallExpression(ast: AstTypes.Node): AstTypes.CallExpression | undefined {
let callExpression;

// prettier-ignore
Walker.walk(ast, {}, {
Walker.walk(ast, null, {
CallExpression(node) {
callExpression ??= node;
},
}
});

return callExpression;
Expand Down
12 changes: 7 additions & 5 deletions packages/addons/sveltekit-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export default defineAddon({
if (adapterImportDecl) {
// replaces the import's source with the new adapter
adapterImportDecl.source.value = adapter.package;
// reset raw value, so that the string is re-generated
adapterImportDecl.source.raw = undefined;

adapterName = adapterImportDecl.specifiers?.find((s) => s.type === 'ImportDefaultSpecifier')
?.local?.name as string;
} else {
Expand All @@ -79,16 +82,15 @@ export default defineAddon({

const { value: config } = exports.defaultExport(ast, object.createEmpty());
const kitConfig = config.properties.find(
(p) => p.type === 'ObjectProperty' && p.key.type === 'Identifier' && p.key.name === 'kit'
) as AstTypes.ObjectProperty | undefined;
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'kit'
) as AstTypes.Property | undefined;

if (kitConfig && kitConfig.value.type === 'ObjectExpression') {
const adapterProp = kitConfig.value.properties.find(
(p) =>
p.type === 'ObjectProperty' && p.key.type === 'Identifier' && p.key.name === 'adapter'
(p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'adapter'
);
if (adapterProp) {
adapterProp.comments = [];
adapterProp.leadingComments = [];
}

// only overrides the `adapter` property so we can reset it's args
Expand Down
Loading