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

Feat/add helpers package #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ jobs:
yarn
cd packages/analyzer
npm test
cd ../helpers
npm test
149 changes: 149 additions & 0 deletions packages/helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# @custom-elements-manifest/helpers

Custom-elements.json is a file format that describes custom elements. This format will allow tooling and IDEs to give rich information about the custom elements in a given project.

This library aims to ship some helpers to ease working with the `custom-elements.json` format.

## Install

```bash
npm i -S @custom-elements-manifest/helpers
```

## Usage

```js
// Node
import { CustomElementsJson } from '@custom-elements-manifest/helpers';

const manifest = JSON.parse(fs.readFileSync('./custom-elements.json', 'utf-8'));
const customElementsJson = new CustomElementsJson(manifest);

// Browser
import { CustomElementsJson } from 'https://unpkg.com/@custom-elements-manifest?module';
import manifest from './custom-elements.json' assert { type: 'json' };

const customElementsJson = new CustomElementsJson(manifest);
```

## Methods

### `getByTagName`

Gets a declaration by tagName

```js
customElementsJson.getByTagName('my-element');
```

### `getByClassName`
Get a declaration by className

```js
customElementsJson.getByClassName('MyElement');
```

### `getByMixinName`
Gets a declaration by mixinName

```js
customElementsJson.getByMixinName('MyElement');
```
### `getCustomElements`
Gets all custom elements

```js
customElementsJson.getCustomElements();
```

### `getClasses`
Gets all classes. Note that this may include classes that are not custom elements

```js
customElementsJson.getClasses();
```

### `getFunctions`
Gets all functions

```js
customElementsJson.getFunctions();
```

### `getVariables`
Gets all variables

```js
customElementsJson.getVariables();
```

### `getDefinitions`
Gets all custom element definitions.

```js
customElementsJson.getDefinitions();
```

### `getMixins`
Gets all mixins

```js
customElementsJson.getMixins();
```

### `getInheritanceTree`

Gets an elements inheritance tree, including superclasses and mixins

```js
customElementsJson.getInheritanceTree('MyElement');
```

### `getModuleForClass`

Gets the module path for a given class

```js
customElementsJson.getModuleForClass('MyElement');
```

### `getModuleForMixin`

Gets the module path for a given mixin

```js
customElementsJson.getModuleForMixin('FooMixin');
```

## Helpers

### Package
- `hasModules`
- `hasExports`
- `hasDeclarations`
- `isJavascriptModule`

### Exports
- `isCustomElementExport`
- `isJavaScriptExport`

### Declarations
- `isClass`
- `isMixin`
- `isCustomElement`
- `isFunction`
- `isVariable`

### CustomElement
- `hasAttributes`
- `hasCssParts`
- `hasCssProperties`
- `hasEvents`
- `hasSlots`
- `hasMethods`
- `hasFields`
- `hasMixins`

### ClassMember
- `isField`
- `isMethod`
97 changes: 97 additions & 0 deletions packages/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
export const has = arr => Array.isArray(arr) && arr?.length > 0;

/** Package */
export function hasModules(_package) {
return has(_package?.modules);
}

/** JavaScriptModule */
export function hasExports(_module) {
return has(_module?.exports);
}

export function hasDeclarations(_module) {
return has(_module?.declarations);
}

export function isJavaScriptModule(_module) {
return _module.kind === 'javascript-module';
}

/** Exports */
export function isCustomElementExport(_export) {
return _export.kind === 'custom-element-definition';
}

export function isJavaScriptExport(_export) {
return _export.kind === 'js';
}

/** Declarations */
export function isClass(item) {
return item.kind === 'class';
}

export function isMixin(item) {
return item.kind === 'mixin';
}

export function isCustomElement(item) {
return item.customElement;
}

export function isFunction(item) {
return item.kind === 'function';
}

export function isVariable(item) {
return item.kind === 'variable';
}

/** CustomElement */
export function hasAttributes(customElement) {
return has(customElement?.attributes)
}

export function hasCssParts(customElement) {
return has(customElement?.cssParts)
}

export function hasCssProperties(customElement) {
return has(customElement?.cssProperties)
}

export function hasEvents(customElement) {
return has(customElement?.events)
}

export function hasSlots(customElement) {
return has(customElement?.slots)
}

export function hasMethods(customElement) {
return (
has(customElement?.members) &&
customElement?.members?.some(member => member.kind === 'method')
);
}

export function hasFields(customElement) {
return (
has(customElement?.members) &&
customElement?.members?.some(member => member.kind === 'field')
);
}

export function hasMixins(customElement) {
return has(customElement?.mixins);
}

/** ClassMember */
export function isField(member) {
return member.kind === 'field';
}

export function isMethod(member) {
return member.kind === 'method';
}
Loading