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 support for interfaces #75

Open
wants to merge 3 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
171 changes: 151 additions & 20 deletions packages/analyzer/custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,181 @@
"schemaVersion": "1.0.0",
"readme": "",
"modules": [
{
"kind": "javascript-module",
"path": "fixtures/-default/package/b.js",
"declarations": [
{
"kind": "interface",
"name": "F",
"members": [
{
"kind": "field",
"name": "f",
"type": {
"text": "string"
}
}
]
}
],
"exports": [
{
"kind": "js",
"name": "F",
"declaration": {
"name": "F",
"module": "fixtures/-default/package/b.js"
}
}
]
},
{
"kind": "javascript-module",
"path": "fixtures/-default/package/bar.js",
"declarations": [
{
"kind": "class",
"description": "",
"name": "MyEl",
"kind": "interface",
"name": "G",
"members": [
{
"kind": "field",
"name": "g",
"type": {
"text": "boolean"
}
}
]
},
{
"kind": "interface",
"name": "B",
"supertypes": [
{
"name": "F",
"package": "b.js"
},
{
"name": "G",
"module": "fixtures/-default/package/bar.js"
}
],
"members": [
{
"kind": "field",
"name": "bar",
"attribute": "bar"
"name": "b",
"type": {
"text": "string"
}
},
{
"kind": "field",
"name": "foo",
"privacy": "public",
"attribute": "foo"
"name": "f",
"type": {
"text": "string"
},
"inheritedFrom": {
"name": "F",
"module": "fixtures/-default/package/b.js"
}
},
{
"kind": "field",
"name": "g",
"type": {
"text": "boolean"
},
"inheritedFrom": {
"name": "G",
"module": "fixtures/-default/package/bar.js"
}
}
]
},
{
"kind": "interface",
"name": "A",
"supertypes": [
{
"name": "B",
"module": "fixtures/-default/package/bar.js"
},
{
"name": "C",
"package": "bar"
},
{
"name": "D",
"module": "fixtures/-default/package/bar.js"
}
],
"attributes": [
"members": [
{
"kind": "field",
"name": "a",
"type": {
"text": "string"
}
},
{
"kind": "field",
"name": "b",
"type": {
"text": "string"
},
"inheritedFrom": {
"name": "B",
"module": "fixtures/-default/package/bar.js"
}
},
{
"name": "bar",
"fieldName": "bar"
"kind": "field",
"name": "f",
"type": {
"text": "string"
},
"inheritedFrom": {
"name": "F",
"module": "fixtures/-default/package/b.js"
}
},
{
"name": "foo",
"fieldName": "foo"
"kind": "field",
"name": "g",
"type": {
"text": "boolean"
},
"inheritedFrom": {
"name": "G",
"module": "fixtures/-default/package/bar.js"
}
}
],
"superclass": {
"name": "LitElement",
"module": "fixtures/-default/package/bar.js"
},
"customElement": true
"description": "Description of the interface"
}
],
"exports": [
{
"kind": "js",
"name": "MyEl",
"name": "G",
"declaration": {
"name": "G",
"module": "fixtures/-default/package/bar.js"
}
},
{
"kind": "js",
"name": "B",
"declaration": {
"name": "B",
"module": "fixtures/-default/package/bar.js"
}
},
{
"kind": "js",
"name": "A",
"declaration": {
"name": "MyEl",
"name": "A",
"module": "fixtures/-default/package/bar.js"
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/analyzer/fixtures/-default/package/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export interface F {
f: string
}
47 changes: 37 additions & 10 deletions packages/analyzer/fixtures/-default/package/bar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
export class MyEl extends LitElement {
static get properties() {
return {
foo: {type: String}
}
}

@property()
bar;
}
import type C from 'bar';
import F from 'b.js';

export interface G {
g: boolean;
}
export interface B extends F, G {
b: string;
}

/**
* Description of the interface
*/
export interface A extends B, C implements D {
a: string;
}


// import type Bar from 'bar';

// /**
// * Description of the interface
// */
// export interface MyInterface extends Foo, Bar implements Baz {
// /**
// * the name of the class
// * @summary this is the summary
// */
// name: string;

// /** super classes and mixins */
// superClasses: SuperClass[];

// member: ClassMember;

// baz(a: string): void
// }
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function handleKind(functionLike, node) {
functionLike.kind = 'function';
break;
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
functionLike.kind = 'method';
break;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/analyzer/src/features/analyse-phase/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ export function exportsPlugin() {
});
}

/**
* @example export interface Foo {}
*/
if(node.kind === ts.SyntaxKind.InterfaceDeclaration) {
const _export = {
kind: 'js',
name: node?.name?.getText() || '',
declaration: {
name: node?.name?.getText() || '',
module: moduleDoc.path,
}
}
moduleDoc.exports = [...(moduleDoc.exports || []), _export];
}

/**
* @example export default var1;
*/
Expand Down
70 changes: 70 additions & 0 deletions packages/analyzer/src/features/analyse-phase/interfaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import ts from 'typescript';
import { has, resolveModuleOrPackageSpecifier } from '../../utils/index.js';
import { createField } from './creators/createClassField.js';
import { createFunctionLike } from './creators/createFunctionLike.js';
import { handleJsDoc } from './creators/handlers.js';

/**
* interfacesPlugin
*
* handles interfaces
*/
export function interfacesPlugin() {
return {
name: 'CORE - INTERFACES',
analyzePhase({ts, node, moduleDoc, context}){
switch(node.kind) {
case ts.SyntaxKind.InterfaceDeclaration:
const int = createInterface(node, moduleDoc, context);
moduleDoc.declarations.push(int);
break;
}
}
}
}

function createInterface(node, moduleDoc, context) {
let int = {
kind: 'interface',
name: node?.name?.getText?.() || '',
supertypes: [],
members: []
}

/** Add description */
int = handleJsDoc(int, node);

/** Heritage */
node?.heritageClauses?.forEach(clause => {
clause?.types?.forEach(type => {
const ref = {
name: type?.getText?.() || '',
...resolveModuleOrPackageSpecifier(moduleDoc, context, type?.getText?.())
}
int.supertypes.push(ref);
});
});

/** Members */
node?.members?.forEach(member => {
/** Properties */
if(member.kind === ts.SyntaxKind.PropertySignature) {
const field = createField(member);
int.members.push(field);
}

/** Methods */
if(member.kind === ts.SyntaxKind.MethodSignature) {
const method = createFunctionLike(member);
int.members.push(method);
}
});

['supertypes', 'members'].forEach(kind => {
if(!has(int[kind])) {
delete int[kind]
}
});

return int;
}
2 changes: 2 additions & 0 deletions packages/analyzer/src/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { collectImportsPlugin } from './collect-phase/collect-imports.js';
/**
* ANALYSE
*/
import { interfacesPlugin } from './analyse-phase/interfaces.js';
import { exportsPlugin } from './analyse-phase/exports.js';
import { customElementsDefineCallsPlugin } from './analyse-phase/custom-elements-define-calls.js';
import { functionLikePlugin } from './analyse-phase/function-like.js';
Expand Down Expand Up @@ -48,6 +49,7 @@ export const FEATURES = [
collectImportsPlugin(),

/** ANALYSE */
interfacesPlugin(),
exportsPlugin(),
customElementsDefineCallsPlugin(),
functionLikePlugin(),
Expand Down
Loading