Skip to content

Commit

Permalink
Add support for Svelte V4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekaterina Mulyukina committed Oct 26, 2024
1 parent ec7741e commit 16d8ed8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 6 additions & 4 deletions lib/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -85,6 +86,7 @@ function detectVersionFromStructure(structure, defaultVersion) {
module.exports = {
SVELTE_VERSION_2,
SVELTE_VERSION_3,
SVELTE_VERSION_4,
detectVersionFromOptions,
detectVersionFromStructure
};
3 changes: 2 additions & 1 deletion test/integration/detector/detector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down

0 comments on commit 16d8ed8

Please sign in to comment.