diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b048bd4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}\\index.js" + } + ] +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a21485d..b5dd763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## UNRELEASED - [Added] Svelte V3: Implement support of script element locations +- [Fixed] Svelte V3: Fix parsing when component have multiple ` + + \ No newline at end of file diff --git a/test/svelte3/integration/locations/locations.spec.js b/test/svelte3/integration/locations/locations.spec.js new file mode 100644 index 0000000..8b66935 --- /dev/null +++ b/test/svelte3/integration/locations/locations.spec.js @@ -0,0 +1,41 @@ +const path = require('path'); +const chai = require('chai'); +const expect = chai.expect; + +const parser = require('../../../../index'); + +function assertDataItemLocation(dataItem, expectedLocationStart, expectedLocationEnd) { + expect(dataItem.locations, `Code location for data item should be included for "${dataItem.name}"`).to.be.exist; + expect(dataItem.locations.length, `Code location for data item have values for "${dataItem.name}"`).to.be.equal(1); + const location = dataItem.locations[0]; + expect(location, `Location should be correct identified for "${dataItem.name}"`).is.deep.equals({ start: expectedLocationStart, end: expectedLocationEnd }); +} + +describe('SvelteDoc v3 - Locations', () => { + it('Locations for multiple scripts should be found correct', (done) => { + parser.parse({ + version: 3, + filename: path.resolve(__dirname, 'locations.multiscripts.svelte'), + features: ['data'], + includeSourceLocations: true, + ignoredVisibilities: [] + }).then((doc) => { + expect(doc, 'Document should be provided').to.exist; + expect(doc.data, 'Document events should be parsed').to.exist; + + const static = doc.data.find(p => p.name === 'staticVariable'); + expect(static, '"staticVariable" should be presented in data items of the doc').to.exist; + expect(static.static).to.be.true; + assertDataItemLocation(static, 83, 97); + + const local = doc.data.find(p => p.name === 'variable'); + expect(local, '"variable" should be presented in data items of the doc').to.exist; + expect(local.static).to.be.false; + assertDataItemLocation(local, 135, 143); + + done(); + }).catch(e => { + done(e); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/helpers/helpers.spec.js b/test/unit/helpers/helpers.spec.js new file mode 100644 index 0000000..acc4778 --- /dev/null +++ b/test/unit/helpers/helpers.spec.js @@ -0,0 +1,120 @@ +const chai = require('chai'); +const expect = chai.expect; + +const helpers = require("../../../lib/helpers"); + +describe('Helpers parser module tests', () => { + describe('helpers.extractHtmlBlock', () => { + it('When html block exists should be provide a correct result', () => { + const content = ` +

Some content here

+ +`; + + const result = helpers.extractHtmlBlock(content, 'script'); + + expect(result).is.exist; + const block = result.block; + expect(block).is.exist; + expect(block.content).is.eq('\nlet variable = 1;\n'); + expect(block.offset).is.eq(34); + expect(block.outerPosition).is.exist; + expect(block.outerPosition.start).is.eq(26); + expect(block.outerPosition.end).is.eq(62); + expect(block.attributes).is.empty; + }); + + it('When html block have attribute, should be correct extracted', () => { + const content = ` +

Some content here

+ +`; + + const result = helpers.extractHtmlBlock(content, 'script'); + + expect(result).is.exist; + const block = result.block; + expect(block).is.exist; + expect(block.attributes).is.eq(' scope="module"'); + }); + + it('When startIndex are specified, should be skip first content and provide the next item', () => { + const content = ` +

Some content here

+ + + `; + + const result = helpers.extractHtmlBlock(content, 'script', 62); + + expect(result).is.exist; + const block = result.block; + expect(block).is.exist; + expect(block.content).is.eq('\nlet variable = 2;\n'); + expect(block.offset).is.eq(71); + expect(block.outerPosition).is.exist; + expect(block.outerPosition.start).is.eq(63); + expect(block.outerPosition.end).is.eq(99); + expect(block.attributes).is.empty; + }); + }); + + describe('helpers.extractAllHtmlBlocks', () => { + it('Extract one script block', () => { + const content = ` +

Some content here

+ + `; + const result = helpers.extractAllHtmlBlocks(content, 'script'); + + expect(result).is.exist; + expect(result.blocks).is.exist; + expect(result.blocks.length).is.eq(1); + + const block = result.blocks[0]; + expect(block).is.exist; + expect(block.offset).is.eq(34); + expect(block.content).is.eq('\nlet variable = 1;\n'); + expect(block.attributes).is.empty; + }); + + it('Extract two script blocks', () => { + const content = ` +

Some content here

+ + + `; + const result = helpers.extractAllHtmlBlocks(content, 'script'); + + expect(result).is.exist; + expect(result.blocks).is.exist; + expect(result.blocks.length).is.eq(2); + + let block = result.blocks[0]; + expect(block).is.exist; + expect(block.offset).is.eq(34); + expect(block.content).is.eq('\nlet variable = 1;\n'); + expect(block.attributes).is.empty; + + block = result.blocks[1]; + expect(block).is.exist; + expect(block.offset).is.eq(71); + expect(block.content).is.eq('\nlet variable = 2;\n'); + expect(block.attributes).is.empty; + }); + }); +}); \ No newline at end of file