Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: invalid empty bundle types filtered from result #412

Merged
merged 6 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/resolve/adapters/bundleSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { MixedContentSourceAdapter } from './mixedContentSourceAdapter';
import { SourcePath } from '../../common';
import { SourceComponent } from '../sourceComponent';

/**
* Handles _bundle_ types. A bundle component has all its source files, including the
Expand All @@ -26,4 +28,30 @@ import { MixedContentSourceAdapter } from './mixedContentSourceAdapter';
*/
export class BundleSourceAdapter extends MixedContentSourceAdapter {
protected ownFolder = true;

/**
* Excludes empty bundle directories.
*
* e.g.
* lwc/
* ├── myFoo/
* | ├── myFoo.js
* | ├── myFooStyle.css
* | ├── myFoo.html
* | ├── myFoo.js-meta.xml
* ├── emptyLWC/
*
* so we shouldn't populate with the `emptyLWC` directory
*
* @param trigger Path that `getComponent` was called with
* @param component Component to populate properties on
* @protected
*/
protected populate(trigger: SourcePath, component?: SourceComponent): SourceComponent {
if (this.tree.isDirectory(trigger) && !this.tree.readDirectory(trigger)?.length) {
// if it's an empty directory, don't include it (e.g., lwc/emptyLWC)
return;
}
return super.populate(trigger, component);
}
}
19 changes: 19 additions & 0 deletions test/mock/registry/type-constants/bundleConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ export const COMPONENT = SourceComponent.createVirtualComponent(
},
]
);

export const EMPTY_BUNDLE = SourceComponent.createVirtualComponent(
{
name: 'a',
type,
xml: XML_PATH,
content: CONTENT_PATH,
},
[
{
dirPath: TYPE_DIRECTORY,
children: [COMPONENT_NAME],
},
{
dirPath: CONTENT_PATH,
children: [],
},
]
);
17 changes: 16 additions & 1 deletion test/resolve/adapters/bundleSourceAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { mockRegistry, bundle } from '../../mock/registry';
import { expect } from 'chai';
import { BundleSourceAdapter } from '../../../src/resolve/adapters/bundleSourceAdapter';
import { BundleSourceAdapter } from '../../../src/resolve/adapters';
import { CONTENT_PATH } from '../../mock/registry/type-constants/bundleConstants';

describe('BundleSourceAdapter', () => {
const adapter = new BundleSourceAdapter(
Expand All @@ -21,6 +22,20 @@ describe('BundleSourceAdapter', () => {
expect(adapter.getComponent(bundle.XML_PATH)).to.deep.equal(bundle.COMPONENT);
});

it('Should return expected SourceComponent when given a bundle directory', () => {
expect(adapter.getComponent(bundle.CONTENT_PATH)).to.deep.equal(bundle.COMPONENT);
});

it('Should exclude empty bundle directories', () => {
const emptyBundleAdapter = new BundleSourceAdapter(
bundle.EMPTY_BUNDLE.type,
mockRegistry,
undefined,
bundle.EMPTY_BUNDLE.tree
);
expect(emptyBundleAdapter.getComponent(CONTENT_PATH)).to.be.undefined;
});

it('Should return expected SourceComponent when given a source path', () => {
const randomSource = bundle.SOURCE_PATHS[1];
expect(adapter.getComponent(randomSource)).to.deep.equal(bundle.COMPONENT);
Expand Down