Skip to content

Commit

Permalink
feat: update svelte to 5.0.0-next.115 && minor refactor (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 29, 2024
1 parent 7c0861b commit 37f0061
Show file tree
Hide file tree
Showing 92 changed files with 1,017 additions and 70 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-ways-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: add support for $host and $state.snapshot
5 changes: 5 additions & 0 deletions .changeset/violet-adults-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": minor
---

feat: update svelte to 5.0.0-next.115 && minor refactor
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
},
"peerDependencies": {
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.112"
"svelte": "^3.37.0 || ^4.0.0 || ^5.0.0-next.115"
},
"peerDependenciesMeta": {
"svelte": {
Expand All @@ -68,16 +68,16 @@
"@ota-meshi/eslint-plugin": "^0.15.3",
"@types/benchmark": "^2.1.5",
"@types/chai": "^4.3.14",
"@types/eslint": "^8.56.7",
"@types/eslint": "^8.56.10",
"@types/eslint-scope": "^3.7.7",
"@types/eslint-visitor-keys": "^3.3.0",
"@types/estree": "^1.0.5",
"@types/mocha": "^10.0.6",
"@types/node": "^20.12.5",
"@types/node": "^20.12.7",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "~7.7.0",
"@typescript-eslint/types": "~7.7.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "~7.7.1",
"@typescript-eslint/types": "~7.7.1",
"benchmark": "^2.1.4",
"chai": "^4.4.1",
"env-cmd": "^10.1.0",
Expand All @@ -87,27 +87,27 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-json-schema-validator": "^5.1.0",
"eslint-plugin-jsonc": "^2.15.0",
"eslint-plugin-n": "^17.0.0",
"eslint-plugin-jsonc": "^2.15.1",
"eslint-plugin-n": "^17.3.1",
"eslint-plugin-node-dependencies": "^0.12.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-regexp": "^2.4.0",
"eslint-plugin-svelte": "^2.35.1",
"eslint-plugin-regexp": "^2.5.0",
"eslint-plugin-svelte": "^2.38.0",
"eslint-plugin-yml": "^1.14.0",
"estree-walker": "^3.0.3",
"locate-character": "^3.0.0",
"magic-string": "^0.30.9",
"magic-string": "^0.30.10",
"mocha": "^10.4.0",
"mocha-chai-jest-snapshot": "^1.1.4",
"nyc": "^15.1.0",
"prettier": "~3.2.5",
"prettier-plugin-pkg": "^0.18.1",
"prettier-plugin-svelte": "^3.2.2",
"prettier-plugin-svelte": "^3.2.3",
"rimraf": "^5.0.5",
"semver": "^7.6.0",
"svelte": "^5.0.0-next.112",
"svelte2tsx": "^0.7.6",
"typescript": "~5.4.4",
"svelte": "^5.0.0-next.115",
"svelte2tsx": "^0.7.7",
"typescript": "~5.4.5",
"typescript-eslint-parser-for-extra-files": "^0.6.0"
},
"publishConfig": {
Expand Down
58 changes: 13 additions & 45 deletions src/parser/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type ESTree from "estree";
import type * as SvAST from "./svelte-ast-types";
import type * as Compiler from "svelte/compiler";
import { parseAttributes } from "./html";

export type Child =
| Compiler.Text
Expand Down Expand Up @@ -31,16 +30,22 @@ export function getModuleFromRoot(
}
export function getOptionsFromRoot(
svelteAst: Compiler.Root | SvAST.AstLegacy,
code: string,
): Compiler.SvelteOptionsRaw | null {
const root = svelteAst as Compiler.Root;
if (root.options) {
if ((root.options as any).__raw__) {
return (root.options as any).__raw__;
}
// If there is no `__raw__` property in the `SvelteOptions` node,
// we will parse `<svelte:options>` ourselves.
return parseSvelteOptions(root.options, code);
return {
type: "SvelteOptions",
name: "svelte:options",
attributes: root.options.attributes,
fragment: {
type: "Fragment",
nodes: [],
transparent: true,
},
start: root.options.start,
end: root.options.end,
parent: null as any,
};
}
return null;
}
Expand Down Expand Up @@ -239,40 +244,3 @@ export function getDeclaratorFromConstTag(
(node as SvAST.ConstTag).expression
);
}

function parseSvelteOptions(
options: Compiler.SvelteOptions,
code: string,
): Compiler.SvelteOptionsRaw {
const { start, end } = options;
const nameEndName = start + "<svelte:options".length;
const { attributes, index: tagEndIndex } = parseAttributes(
code,
nameEndName + 1,
);
const fragment: Compiler.Fragment = {
type: "Fragment",
nodes: [],
transparent: true,
};
if (code.startsWith(">", tagEndIndex)) {
const childEndIndex = code.indexOf("</svelte:options", tagEndIndex);
fragment.nodes.push({
type: "Text",
data: code.slice(tagEndIndex + 1, childEndIndex),
start: tagEndIndex + 1,
end: childEndIndex,
raw: code.slice(tagEndIndex + 1, childEndIndex),
parent: fragment,
});
}
return {
type: "SvelteOptions",
name: "svelte:options",
attributes,
fragment,
start,
end,
parent: null as any,
};
}
2 changes: 1 addition & 1 deletion src/parser/converts/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function convertSvelteRoot(
const fragment = getFragmentFromRoot(svelteAst);
if (fragment) {
let children = getChildren(fragment);
const options = getOptionsFromRoot(svelteAst, ctx.code);
const options = getOptionsFromRoot(svelteAst);
if (options) {
children = [...children];
if (
Expand Down
1 change: 1 addition & 0 deletions src/parser/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const globalsForRunes = [
"$props",
"$bindable",
"$inspect",
"$host",
] as const;
const globalsForSvelte5 = [...globalsForSvelte4, ...globalsForRunes];
export const globals = svelteVersion.gte(5)
Expand Down
12 changes: 9 additions & 3 deletions src/parser/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ export function parseTemplate(
svelteAst: Compiler.Root | SvAST.AstLegacy;
} {
try {
const svelteAst = parse(code, {
const options: {
filename?: string | undefined;
modern: true;
} = {
filename: parserOptions.filePath,
...(svelteVersion.gte(5) ? { modern: true } : {}),
}) as never as Compiler.Root | SvAST.AstLegacy;
...(svelteVersion.gte(5) ? { modern: true } : ({} as never)),
};
const svelteAst = parse(code, options) as never as
| Compiler.Root
| SvAST.AstLegacy;
const ast = convertSvelteRoot(svelteAst, ctx);

return {
Expand Down
23 changes: 17 additions & 6 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ function analyzeDollarDollarVariables(
case "$props":
case "$bindable":
case "$inspect":
case "$host":
// Processed by `analyzeRuneVariables`.
break;
default: {
Expand Down Expand Up @@ -318,7 +319,7 @@ function analyzeRuneVariables(
continue;
}
switch (globalName) {
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2492
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2585
case "$state": {
appendDeclareFunctionVirtualScripts(globalName, [
"<T>(initial: T): T",
Expand All @@ -328,9 +329,12 @@ function analyzeRuneVariables(
"export function frozen<T>(initial: T): Readonly<T>;",
"export function frozen<T>(): Readonly<T> | undefined;",
]);
appendDeclareNamespaceVirtualScripts(globalName, [
"export function snapshot<T>(state: T): T;",
]);
break;
}
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2535
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2648
case "$derived": {
appendDeclareFunctionVirtualScripts(globalName, [
"<T>(expression: T): T",
Expand All @@ -340,7 +344,7 @@ function analyzeRuneVariables(
]);
break;
}
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2574
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2687
case "$effect": {
appendDeclareFunctionVirtualScripts(globalName, [
"(fn: () => void | (() => void)): void",
Expand All @@ -352,23 +356,30 @@ function analyzeRuneVariables(
]);
break;
}
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2655
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2768
case "$props": {
appendDeclareFunctionVirtualScripts(globalName, ["(): any"]);
break;
}
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2666
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2779
case "$bindable": {
appendDeclareFunctionVirtualScripts(globalName, ["<T>(t?: T): T"]);
break;
}
// See https://github.com/sveltejs/svelte/blob/dfc55c18f010f620b966e13eb0ad1f9791b5798f/packages/svelte/types/index.d.ts#L2686
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2799
case "$inspect": {
appendDeclareFunctionVirtualScripts(globalName, [
`<T extends any[]>(...values: T): { with: (fn: (type: 'init' | 'update', ...values: T) => void) => void }`,
]);
break;
}
// See https://github.com/sveltejs/svelte/blob/bda32edb1a4f2d383d96071750d6bfa9421b2175/packages/svelte/types/index.d.ts#L2822
case "$host": {
appendDeclareFunctionVirtualScripts(globalName, [
`<El extends HTMLElement = HTMLElement>(): El`,
]);
break;
}
default: {
const _: never = globalName;
throw Error(`Unknown global: ${_}`);
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/parser/ast/$$slots-scope-output-svelte5.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
"identifiers": [],
"defs": [],
"references": []
},
{
"name": "$host",
"identifiers": [],
"defs": [],
"references": []
}
],
"references": [],
Expand Down

0 comments on commit 37f0061

Please sign in to comment.