Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"schemaVersion": "1.0.0",
"readme": "",
"modules": [
{
"kind": "javascript-module",
"path": "my-element.js",
"declarations": [
{
"kind": "class",
"description": "",
"name": "NameTag",
"superclass": {
"name": "FASTElement",
"package": "@microsoft/fast-element"
},
"tagName": "name-tag",
"customElement": true
}
],
"exports": [
{
"kind": "js",
"name": "NameTag",
"declaration": {
"name": "NameTag",
"module": "my-element.js"
}
},
{
"kind": "custom-element-definition",
"name": "name-tag",
"declaration": {
"name": "NameTag",
"module": "my-element.js"
}
}
]
}
]
}
41 changes: 41 additions & 0 deletions packages/analyzer/fixtures/09-plugin-fast/03-define/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"schemaVersion": "1.0.0",
"readme": "",
"modules": [
{
"kind": "javascript-module",
"path": "my-element.js",
"declarations": [
{
"kind": "class",
"description": "",
"name": "NameTag",
"superclass": {
"name": "FASTElement",
"package": "@microsoft/fast-element"
},
"tagName": "name-tag",
"customElement": true
}
],
"exports": [
{
"kind": "js",
"name": "NameTag",
"declaration": {
"name": "NameTag",
"module": "my-element.js"
}
},
{
"kind": "custom-element-definition",
"name": "name-tag",
"declaration": {
"name": "NameTag",
"module": "my-element.js"
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fastPlugin } from '../../../../src/features/framework-plugins/fast/fast.js';

export default {
plugins: [...fastPlugin()]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FASTElement, html } from '@microsoft/fast-element';

const template = html<NameTag>`
<p>Hello</p>
`;

export class NameTag extends FASTElement {}

NameTag.define({
name: 'name-tag',
template
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getDeclarationInFile, hasIgnoreJSDoc } from '../../../utils/ast-helpers.js';
import { resolveModuleOrPackageSpecifier } from '../../../utils/index.js';

/**
* FAST-DEFINE
*
* Handles FASTElement's static define method.
* @example MyElement.define({ name: 'my-element' });
*/
export function fastDefinePlugin() {
return {
name: 'FAST - DEFINE',
analyzePhase({ts, node, moduleDoc, context}) {
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) {
return;
}

const className = node.expression.expression?.getText();
const options = node.arguments[0];
if (node.expression.name?.getText() !== 'define' ||
!className ||
!ts.isObjectLiteralExpression(options)) {
return;
}

const klass = getDeclarationInFile(className, node.getSourceFile());
const extendsFastElement = klass?.heritageClauses?.some(heritageClause =>
heritageClause.token === ts.SyntaxKind.ExtendsKeyword &&
heritageClause.types.some(type => type.expression?.getText() === 'FASTElement')
);
if (!extendsFastElement || hasIgnoreJSDoc(klass)) {
return;
}

const nameProperty = options.properties.find(property =>
ts.isPropertyAssignment(property) && property.name?.getText() === 'name'
);
if (!nameProperty || !ts.isStringLiteralLike(nameProperty.initializer)) {
return;
}
const tagName = nameProperty.initializer.text;

const definitionDoc = {
kind: 'custom-element-definition',
name: tagName,
declaration: {
name: className,
...resolveModuleOrPackageSpecifier(moduleDoc, context, className)
},
};

moduleDoc.exports = [...(moduleDoc.exports || []), definitionDoc];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { attrDecoratorPlugin } from '../decorators/attr.js';
import { customElementDecoratorPlugin } from '../decorators/custom-element-decorator.js';
import { fastDefinePlugin } from './fast-define.js';

export const fastPlugin = () => [
attrDecoratorPlugin(),
customElementDecoratorPlugin()
customElementDecoratorPlugin(),
fastDefinePlugin()
]