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

Account for class only having template tag in no-empty-glimmer-component-classes rule #1866

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
11 changes: 11 additions & 0 deletions lib/rules/no-empty-glimmer-component-classes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use strict';

const { isGlimmerComponent } = require('../utils/ember');
const { isClassPropertyOrPropertyDefinition } = require('../utils/types');

const ERROR_MESSAGE = 'Do not create empty backing classes for Glimmer components.';
const ERROR_MESSAGE_TEMPLATE_TAG =
'Do not create empty backing classes for Glimmer template tag only components.';

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -23,12 +26,20 @@ module.exports = {
},

ERROR_MESSAGE,
ERROR_MESSAGE_TEMPLATE_TAG,

create(context) {
return {
ClassDeclaration(node) {
if (isGlimmerComponent(context, node) && !node.decorators && node.body.body.length === 0) {
context.report({ node, message: ERROR_MESSAGE });
} else if (
isGlimmerComponent(context, node) &&
chrisrng marked this conversation as resolved.
Show resolved Hide resolved
node.body.body.length === 1 &&
isClassPropertyOrPropertyDefinition(node.body.body[0]) &&
node.body.body[0].key?.callee?.name === '__GLIMMER_TEMPLATE'
) {
context.report({ node, message: ERROR_MESSAGE_TEMPLATE_TAG });
}
},
};
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules-preprocessor/gjs-gts-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ const valid = [
];

const invalid = [
{
filename: 'my-component.gjs',
code: `import Component from '@glimmer/component';
export default class Chris extends Component {
<template>Hello!</template>
}`,
errors: [
{
message: 'Do not create empty backing classes for Glimmer template tag only components.',
line: 2,
column: 20,
endColumn: 6,
},
],
},
{
filename: 'my-component.gjs',
code: `
Expand Down