Skip to content

Commit

Permalink
fix (no-invalid-extends): respect elementBaseClasses (#102)
Browse files Browse the repository at this point in the history
We should respect `elementBaseClasses` as acceptable base classes for
custom elements when running the logic in this rule.

If it isn't set, we can default to `HTMLElement` as being the only base
class.
  • Loading branch information
43081j authored Aug 7, 2023
1 parent 1ca8c9a commit 8ec34da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/rules/no-invalid-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ const rule: Rule.RuleModule = {
const source = context.getSourceCode();
const elementClasses = new Set<ESTree.Class>();
const userAllowedSuperNames = context.options[0]?.allowedSuperNames ?? [];
const elementBaseClasses = ['HTMLElement'];

if (Array.isArray(context.settings.wc?.elementBaseClasses)) {
elementBaseClasses.push(
...(context.settings.wc.elementBaseClasses as string[])
);
}

return {
'ClassExpression,ClassDeclaration': (node: ESTree.Class): void => {
Expand Down Expand Up @@ -135,7 +142,9 @@ const rule: Rule.RuleModule = {
]);
} else {
allowedSuperNames = new Set<string>(userAllowedSuperNames);
allowedSuperNames.add('HTMLElement');
for (const baseClass of elementBaseClasses) {
allowedSuperNames.add(baseClass);
}
}

const formattedSuperNames = formatNames(allowedSuperNames);
Expand Down
9 changes: 9 additions & 0 deletions src/test/rules/no-invalid-extends_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ ruleTester.run('no-invalid-extends', rule, {
code: `@customElement('x-foo')
class A extends SomeElement {}`,
parser
},
{
code: `class A extends SomeElement {}
customElements.define('a', A);`,
settings: {
wc: {
elementBaseClasses: ['SomeElement']
}
}
}
],

Expand Down

0 comments on commit 8ec34da

Please sign in to comment.