From 16d8ed866ee82c16632e75ab2c838c815613e1ba Mon Sep 17 00:00:00 2001 From: Ekaterina Mulyukina Date: Sat, 26 Oct 2024 14:26:56 +0300 Subject: [PATCH] Add support for Svelte V4 --- README.md | 6 +++--- index.js | 2 +- lib/detector.js | 10 ++++++---- test/integration/detector/detector.spec.js | 3 ++- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b0b3ae9..2876efa 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ npm install --save sveltedoc-parser - Extract component actions - Extract component transitions -### Svelte 3 +### Svelte 3/4 - Extract global component description and keywords from JSDoc comment - Top level comment must include `@component`. [Example script](/test/svelte3/integration/globalComment/globalComment.script.svelte), [Example html](/test/svelte3/integration/globalComment/globalComment.markup.svelte) @@ -149,14 +149,14 @@ Here are the properties supported by `options` (see the [Usage](#Usage) section | **features** | The component features to parse and extract. | string[] | [All supported features](#Supported-feature-names) | | **ignoredVisibilities** | The list of ignored visibilities. Symbols with a visibility that is ignored will not be included in the output. | string[] | `['private', 'protected']` | | **includeSourceLocations** | Flag, which indicates that source locations should be provided for component symbols. | boolean | `false` | -| **version** | Optional. Use `2` (will deprecating) or `3` to specify which svelte syntax should be used. When that is not provided, parser try to detect version of the syntax. | number? | `undefined` | +| **version** | Optional. Use `2` (will deprecating), `3` or `4` to specify which svelte syntax should be used. When that is not provided, parser try to detect version of the syntax. | number? | `undefined` | | **defaultVersion** | Optional. Specify default version of svelte syntax, if auto-detector can't identify correct version. | number? | `undefined` | ### Supported feature names These are the values that can be included in the `options.features` array: -| Feature | Svelte 2 | Svelte 3 | Description | +| Feature | Svelte 2 | Svelte 3/4 | Description | |---------------|:--------:|:--------:|-------------------------------------------------------| | `name` | ✔ | ✔ | Component's name | | `description` | ✔ | ✔ | Component's description | diff --git a/index.js b/index.js index 120fd11..39d2951 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,7 @@ function buildSvelte3Parser(options) { } function buildSvelteParser(options, version) { - if (version === SvelteVersionDetector.SVELTE_VERSION_3) { + if (version === SvelteVersionDetector.SVELTE_VERSION_3 || version === SvelteVersionDetector.SVELTE_VERSION_4) { return buildSvelte3Parser(options); } diff --git a/lib/detector.js b/lib/detector.js index 7147bb2..4e3ea70 100644 --- a/lib/detector.js +++ b/lib/detector.js @@ -2,6 +2,7 @@ const { loadFileStructureFromOptions: loadFileContentFromOptions } = require('./ const SVELTE_VERSION_2 = 2; const SVELTE_VERSION_3 = 3; +const SVELTE_VERSION_4 = 4; function isElseIfWithoutSpaceInTemplate(template) { return /\{:elseif\s/gi.test(template); @@ -47,24 +48,24 @@ function detectVersionFromStructure(structure, defaultVersion) { // The `{:else if ...}` statement is valid for Svelte 3 if (isElseIfWithSpaceInTemplate(structure.template)) { - return SVELTE_VERSION_3; + return SVELTE_VERSION_4; } // The `let:item=...` syntax is valid for Svelte 3 if (isLetUsedForSlotInTemplate(structure.template)) { - return SVELTE_VERSION_3; + return SVELTE_VERSION_4; } } if (structure.scripts) { // In Svelte 3 is possible to use more than one script blocks if (structure.scripts.length > 1) { - return SVELTE_VERSION_3; + return SVELTE_VERSION_4; } // In Svelte 3 is possible to define context attribute for script blocks if (structure.scripts.some(scriptBlock => isContextUsedForScriptBlock(scriptBlock))) { - return SVELTE_VERSION_3; + return SVELTE_VERSION_4; } // Export default statement can be used only in Svelte 2 @@ -85,6 +86,7 @@ function detectVersionFromStructure(structure, defaultVersion) { module.exports = { SVELTE_VERSION_2, SVELTE_VERSION_3, + SVELTE_VERSION_4, detectVersionFromOptions, detectVersionFromStructure }; diff --git a/test/integration/detector/detector.spec.js b/test/integration/detector/detector.spec.js index eea2e2a..93bafd0 100644 --- a/test/integration/detector/detector.spec.js +++ b/test/integration/detector/detector.spec.js @@ -43,7 +43,8 @@ describe('SvelteDoc file version detector', () => { filename: path.join(folderPath, file) }); - expect(version).is.equal(detector.SVELTE_VERSION_3); + // Difference between V3 and V4 is not mach in code, so we can detect it as V4 + expect(version).is.equal(detector.SVELTE_VERSION_4); }); }); });