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

[FEATURE] generate Signature in component blueprints #20356

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions blueprints/component/files/__root__/__path__/__name__.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%= importComponent %>
<%= importTemplate %>
<%= componentSignature %>
export default <%= defaultExport %>
46 changes: 36 additions & 10 deletions blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,8 @@ module.exports = {
},

locals(options) {
let sanitizedModuleName = options.entity.name.replace(/\//g, '-');
let classifiedModuleName = stringUtil.classify(sanitizedModuleName);

let templatePath = '';
let importComponent = '';
let importTemplate = '';
let defaultExport = '';

// if we're in an addon, build import statement
let templatePath = '';
if (options.project.isEmberCLIAddon() || (options.inRepoAddon && !options.inDummy)) {
if (options.pod) {
templatePath = './template';
Expand All @@ -251,6 +244,14 @@ module.exports = {
? options.componentClass
: '@ember/component';

let sanitizedModuleName = options.entity.name.replace(/\//g, '-');
let classifiedModuleName = stringUtil.classify(sanitizedModuleName);

let importComponent = '';
let importTemplate = '';
let defaultExport = '';
let componentSignature = '';

switch (componentClass) {
case '@ember/component':
importComponent = `import Component from '@ember/component';`;
Expand All @@ -263,20 +264,45 @@ module.exports = {
break;
case '@glimmer/component':
importComponent = `import Component from '@glimmer/component';`;
defaultExport = `class ${classifiedModuleName}Component extends Component {}`;
componentSignature = signatureFor(classifiedModuleName);
defaultExport = `class ${classifiedModuleName}Component extends Component<${classifiedModuleName}Signature> {}`;
break;
case '@ember/component/template-only':
importComponent = `import templateOnly from '@ember/component/template-only';`;
defaultExport = `templateOnly();`;
componentSignature = signatureFor(classifiedModuleName);
defaultExport = `templateOnly<${classifiedModuleName}Signature>();`;
break;
}

return {
importTemplate,
importComponent,
componentSignature,
defaultExport,
path: getPathOption(options),
componentClass: options.componentClass,
};
},
};

function signatureFor(classifiedModuleName) {
let args = ` // The arguments accepted by the component${EOL} Args: {};`;

let blocks =
` // Any blocks yielded by the component${EOL}` +
` Blocks: {${EOL}` +
` default: []${EOL}` +
` };`;

let element =
` // The element to which \`...attributes\` is applied in the component template${EOL}` +
` Element: null;`;

return (
`interface ${classifiedModuleName}Signature {${EOL}` +
`${args}${EOL}` +
`${blocks}${EOL}` +
`${element}${EOL}` +
`}${EOL}`
);
}