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: improve LitElement identification #206

Merged
merged 2 commits into from
Aug 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/rules/lifecycle-super.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {Rule} from 'eslint';
import * as ESTree from 'estree';
import {isLitClass} from "../util";

Check failure on line 8 in src/rules/lifecycle-super.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Strings must use singlequote

Check failure on line 8 in src/rules/lifecycle-super.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Strings must use singlequote

Check failure on line 8 in src/rules/lifecycle-super.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Strings must use singlequote

const methodNames = ['connectedCallback', 'disconnectedCallback', 'update'];

Expand Down Expand Up @@ -44,11 +45,7 @@
* @return {void}
*/
function classEnter(node: ESTree.Class): void {
if (
!node.superClass ||
node.superClass.type !== 'Identifier' ||
node.superClass.name !== 'LitElement'
) {
if (!isLitClass(node)) {
return;
}

Expand Down
8 changes: 2 additions & 6 deletions src/rules/no-property-change-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {Rule} from 'eslint';
import * as ESTree from 'estree';
import {getPropertyMap, PropertyMapEntry} from '../util';
import {getPropertyMap, isLitClass, PropertyMapEntry} from '../util';

const superUpdateQuery =
'CallExpression' +
Expand Down Expand Up @@ -49,11 +49,7 @@ const rule: Rule.RuleModule = {
* @return {void}
*/
function classEnter(node: ESTree.Class): void {
if (
!node.superClass ||
node.superClass.type !== 'Identifier' ||
node.superClass.name !== 'LitElement'
) {
if (!isLitClass(node)) {
return;
}

Expand Down
7 changes: 2 additions & 5 deletions src/rules/no-this-assign-in-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {Rule} from 'eslint';
import * as ESTree from 'estree';
import {isLitClass} from "../util";

Check failure on line 8 in src/rules/no-this-assign-in-render.ts

View workflow job for this annotation

GitHub Actions / test (16.x)

Strings must use singlequote

Check failure on line 8 in src/rules/no-this-assign-in-render.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Strings must use singlequote

Check failure on line 8 in src/rules/no-this-assign-in-render.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Strings must use singlequote

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -38,11 +39,7 @@
* @return {void}
*/
function classEnter(node: ESTree.Class): void {
if (
!node.superClass ||
node.superClass.type !== 'Identifier' ||
node.superClass.name !== 'LitElement'
) {
if (!isLitClass(node)) {
return;
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/rules/attribute-names_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ ruleTester.run('attribute-names', rule, {
messageId: 'casedPropertyWithoutAttribute'
}
]
},
{
code: `@customElement('foo-bar')
class FooBar extends FooElement {
@property({ type: String })
camelCase = 'foo';
}`,
parser,
parserOptions,
errors: [
{
line: 4,
column: 9,
messageId: 'casedPropertyWithoutAttribute'
}
]
},
{
code: `@foo
@customElement('foo-bar')
@bar
class FooBar extends FooElement {
@property({ type: String })
camelCase = 'foo';
}`,
parser,
parserOptions,
errors: [
{
line: 6,
column: 9,
messageId: 'casedPropertyWithoutAttribute'
}
]
}
]
});
26 changes: 26 additions & 0 deletions src/test/rules/lifecycle-super_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const ruleTester = new RuleTester({
}
});

const parser = require.resolve('@babel/eslint-parser');
const parserOptions = {
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', {version: '2023-11'}]]
}
};

ruleTester.run('lifecycle-super', rule, {
valid: [
'class Foo { }',
Expand Down Expand Up @@ -184,6 +192,24 @@ ruleTester.run('lifecycle-super', rule, {
column: 9
}
]
},
{
code: `@customElement('foo')
class Foo extends FooElement {
connectedCallback() {
super.foo.connectedCallback();
}
}`,
parser,
parserOptions,
errors: [
{
messageId: 'callSuper',
data: {method: 'connectedCallback'},
line: 3,
column: 9
}
]
}
]
});
21 changes: 21 additions & 0 deletions src/test/rules/no-property-change-update_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ ruleTester.run('no-property-change-update', rule, {
column: 11
}
]
},
{
code: `@customElement('foo')
class Foo extends FooElement {
static get properties() {
return { prop: { type: String } };
}
update(change) {
super.update();
this.prop = 'foo';
}
}`,
parser,
parserOptions,
errors: [
{
messageId: 'propertyChange',
line: 8,
column: 11
}
]
}
]
});
25 changes: 25 additions & 0 deletions src/test/rules/no-this-assign-in-render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const ruleTester = new RuleTester({
}
});

const parser = require.resolve('@babel/eslint-parser');
const parserOptions = {
requireConfigFile: false,
babelOptions: {
plugins: [['@babel/plugin-proposal-decorators', {version: '2023-11'}]]
}
};

ruleTester.run('no-this-assign-in-render', rule, {
valid: [
'const x = 808;',
Expand Down Expand Up @@ -126,6 +134,23 @@ ruleTester.run('no-this-assign-in-render', rule, {
column: 11
}
]
},
{
code: `@customElement('foo')
class Foo extends FooElement {
render() {
this['prop'] = 'foo';
}
}`,
parser,
parserOptions,
errors: [
{
messageId: 'noThis',
line: 4,
column: 11
}
]
}
]
});
32 changes: 29 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,39 @@ export type DecoratedNode = ESTree.Node & {
decorators?: BabelDecorator[];
};

export type NodeWithParent = ESTree.Node & {
43081j marked this conversation as resolved.
Show resolved Hide resolved
parent?: ESTree.Node;
}

/**
* Returns if given node has a lit identifier
* @param {ESTree.Node} node
* @param {NodeWithParent} node
* @return {boolean}
*/
function hasLitIdentifier(node: ESTree.Node): boolean {
return node.type === 'Identifier' && node.name === 'LitElement';
function hasLitIdentifier(node: NodeWithParent): boolean {
43081j marked this conversation as resolved.
Show resolved Hide resolved
if (node.type === 'Identifier' && node.name === 'LitElement') {
return true;
}

if (node.parent && node.parent.type === 'ClassDeclaration') {
const decoratedNode = node.parent as DecoratedNode;

if (!decoratedNode.decorators || !Array.isArray(decoratedNode.decorators)) {
return false;
}

for (const decorator of decoratedNode.decorators) {
if (
decorator.expression.type === 'CallExpression' &&
decorator.expression.callee.type === 'Identifier' &&
decorator.expression.callee.name === 'customElement'
) {
return true;
}
}
}

return false;
}

/**
Expand Down
Loading