Skip to content

Commit

Permalink
fix(options): Add integration tests for version auto-detect
Browse files Browse the repository at this point in the history
- revert detectedVersion => version
- Fix typedef for FileStructure
- Small code changes
- Small test changes
  • Loading branch information
soft-decay committed Dec 3, 2020
1 parent f5d0d39 commit a438a40
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<SvelteParserOptions, FileOptionKeys> & { structure?: FileStructure }} FileOptions
*/

Expand Down
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down
1 change: 0 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/parse/basicV2.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export default {};
</script>
3 changes: 3 additions & 0 deletions test/integration/parse/basicV3.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script context="module">
let staticVariable = 0;
</script>
35 changes: 35 additions & 0 deletions test/integration/parse/parse.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
7 changes: 1 addition & 6 deletions test/unit/options/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
Expand Down

0 comments on commit a438a40

Please sign in to comment.