From a438a40627bb81b83474465fdf0b63b856e53958 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rousseau Date: Wed, 2 Dec 2020 22:19:06 -0500 Subject: [PATCH] fix(options): Add integration tests for version auto-detect - revert detectedVersion => version - Fix typedef for FileStructure - Small code changes - Small test changes --- index.js | 10 ++++---- lib/helpers.js | 2 +- lib/options.js | 2 +- lib/parser.js | 1 - test/integration/parse/basicV2.svelte | 3 +++ test/integration/parse/basicV3.svelte | 3 +++ test/integration/parse/parse.spec.js | 35 +++++++++++++++++++++++++++ test/unit/options/options.spec.js | 7 +----- 8 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 test/integration/parse/basicV2.svelte create mode 100644 test/integration/parse/basicV3.svelte create mode 100644 test/integration/parse/parse.spec.js diff --git a/index.js b/index.js index 9b98509..dc5cfd9 100644 --- a/index.js +++ b/index.js @@ -109,9 +109,9 @@ function mergeItems(itemType, currentItem, newItem, ignoreLocations) { return currentItem; } -function subscribeOnParserEvents(parser, ignoredVisibilities, detectedVersion, resolve, reject) { +function subscribeOnParserEvents(parser, ignoredVisibilities, version, resolve, reject) { const component = { - version: detectedVersion + version: version }; parser.features.forEach((feature) => { @@ -195,11 +195,11 @@ module.exports.parse = (options) => new Promise((resolve, reject) => { options.structure = loadFileStructureFromOptions(fileOptions); - const detectedVersion = options.version || SvelteVersionDetector.detectVersionFromStructure(structure, options.defaultVersion); + const version = options.version || SvelteVersionDetector.detectVersionFromStructure(options.structure, options.defaultVersion); - const parser = buildSvelteParser(options, detectedVersion); + const parser = buildSvelteParser(options, version); - subscribeOnParserEvents(parser, options.ignoredVisibilities, detectedVersion, resolve, reject); + subscribeOnParserEvents(parser, options.ignoredVisibilities, version, resolve, reject); parser.walk(); } catch (error) { diff --git a/lib/helpers.js b/lib/helpers.js index 31fdd19..2362d87 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -4,7 +4,7 @@ const path = require('path'); /** * @typedef {import('../typings').SvelteParserOptions} SvelteParserOptions * @typedef {'fileContent' | 'filename' | 'encoding'} FileOptionKeys - * @typedef {{content?: string; template?: string, scripts?: unknown[], styles?: unknown[] }} FileStructure + * @typedef {{content?: string; template?: string, scripts?: HtmlBlock[], styles?: HtmlBlock[] }} FileStructure * @typedef {Pick & { structure?: FileStructure }} FileOptions */ diff --git a/lib/options.js b/lib/options.js index 3fc3933..dbbe408 100644 --- a/lib/options.js +++ b/lib/options.js @@ -42,7 +42,7 @@ const ErrorMessage = Object.freeze({ EncodingNotSupported: (enc) => getUnsupportedEncodingString(enc), IgnoredVisibilitiesMissing: 'Internal Error: options.ignoredVisibilities is not set.', IgnoredVisibilitiesFormat: ERROR_VISIBILITIES_FORMAT + INFO_VISIBILITIES_SUPPORTED, - IgnoredVisibilitiesNotSupported: (arr) => `${getUnsupportedVisibilitiesString(arr)}`, + IgnoredVisibilitiesNotSupported: (arr) => getUnsupportedVisibilitiesString(arr), }); /** @type {BufferEncoding} */ diff --git a/lib/parser.js b/lib/parser.js index bf17524..6137d8d 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -6,7 +6,6 @@ const path = require('path'); const utils = require('./utils'); const jsdoc = require('./jsdoc'); -// TODO: Remove this and use from utils directly const hasOwnProperty = utils.hasOwnProperty; const DEFAULT_OPTIONS = { diff --git a/test/integration/parse/basicV2.svelte b/test/integration/parse/basicV2.svelte new file mode 100644 index 0000000..215ded0 --- /dev/null +++ b/test/integration/parse/basicV2.svelte @@ -0,0 +1,3 @@ + diff --git a/test/integration/parse/basicV3.svelte b/test/integration/parse/basicV3.svelte new file mode 100644 index 0000000..7534c7d --- /dev/null +++ b/test/integration/parse/basicV3.svelte @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/test/integration/parse/parse.spec.js b/test/integration/parse/parse.spec.js new file mode 100644 index 0000000..46e67c2 --- /dev/null +++ b/test/integration/parse/parse.spec.js @@ -0,0 +1,35 @@ +const path = require('path'); +const chai = require('chai'); +const expect = chai.expect; + +const parser = require('./../../../index'); + +describe('parse - Integration', () => { + it('should correctly auto-detect svelte V2 component', (done) => { + parser.parse({ + filename: path.resolve(__dirname, 'basicV2.svelte'), + }).then((doc) => { + expect(doc, 'Document should exist').to.exist; + // v3-parser converts component name to kebab-case + expect(doc.name).to.equal('basic-v2'); + + done(); + }).catch(e => { + done(e); + }); + }); + + it('should correctly auto-detect svelte V3 component', (done) => { + parser.parse({ + filename: path.resolve(__dirname, 'basicV3.svelte'), + }).then((doc) => { + expect(doc, 'Document should be provided').to.exist; + // v3-parser converts component name to PascalCase + expect(doc.name).to.equal('BasicV3'); + + done(); + }).catch(e => { + done(e); + }); + }); +}); diff --git a/test/unit/options/options.spec.js b/test/unit/options/options.spec.js index 5225566..76d2f0a 100644 --- a/test/unit/options/options.spec.js +++ b/test/unit/options/options.spec.js @@ -90,12 +90,7 @@ describe('Options Module', () => { describe('options.retrieveFileOptions', () => { it('Should return all file-related keys from options', () => { - const options = { - ...baseOptions, - ignoredVisibilities: ['protected', 'public'] - }; - - expect(retrieveFileOptions(options)).to.have.keys( + expect(retrieveFileOptions(baseOptions)).to.have.keys( 'filename', 'fileContent', 'structure', 'encoding' ); });