-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (lifecycle-super): add new lifecycle-super rule (#173)
- Loading branch information
Showing
5 changed files
with
380 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Enforces calling `super` in lifecycle methods (lifecycle-super) | ||
|
||
Enforces that `super` is called in lifecycle methods which require it. | ||
|
||
For example, the `connectedCallback` should call `super.connectedCallback()` to | ||
avoid interrupting lit's rendering. | ||
|
||
## Rule Details | ||
|
||
This rule enforces calling of `super` in the following lifecycle methods: | ||
|
||
- `update` | ||
- `connectedCallback` | ||
- `disconnectedCallback` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```ts | ||
class Foo extends LitElement { | ||
connectedCallback() { | ||
doSomething(); | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```ts | ||
class Foo extends LitElement { | ||
connectedCallback() { | ||
super.connectedCallback(); | ||
doSomething(); | ||
} | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you want to override lit's default implementation of a lifecycle method, | ||
you should disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* @fileoverview Enforces calling `super` in lifecycle methods | ||
* @author James Garbutt <https://github.com/43081j> | ||
*/ | ||
|
||
import {Rule} from 'eslint'; | ||
import * as ESTree from 'estree'; | ||
|
||
const methodNames = ['connectedCallback', 'disconnectedCallback', 'update']; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: 'Enforces calling `super` in lifecycle methods', | ||
recommended: false, | ||
url: 'https://github.com/43081j/eslint-plugin-lit/blob/master/docs/rules/lifecycle-super.md' | ||
}, | ||
schema: [], | ||
messages: { | ||
callSuper: | ||
'You must call `super.{{method}}` to avoid interrupting ' + | ||
'the lit rendering lifecycle' | ||
} | ||
}, | ||
|
||
create(context): Rule.RuleListener { | ||
// variables should be defined here | ||
let inElement = false; | ||
let currentMethod: string | null = null; | ||
let superSeen = false; | ||
|
||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
|
||
/** | ||
* Class entered | ||
* | ||
* @param {ESTree.Class} node Node entered | ||
* @return {void} | ||
*/ | ||
function classEnter(node: ESTree.Class): void { | ||
if ( | ||
!node.superClass || | ||
node.superClass.type !== 'Identifier' || | ||
node.superClass.name !== 'LitElement' | ||
) { | ||
return; | ||
} | ||
|
||
inElement = true; | ||
} | ||
|
||
/** | ||
* Class exited | ||
* | ||
* @return {void} | ||
*/ | ||
function classExit(): void { | ||
inElement = false; | ||
} | ||
|
||
/** | ||
* Method entered | ||
* | ||
* @param {ESTree.MethodDefinition} node Node entered | ||
* @return {void} | ||
*/ | ||
function methodEnter(node: ESTree.MethodDefinition): void { | ||
if ( | ||
!inElement || | ||
node.static === true || | ||
node.kind !== 'method' || | ||
node.key.type !== 'Identifier' || | ||
!methodNames.includes(node.key.name) | ||
) { | ||
return; | ||
} | ||
|
||
currentMethod = node.key.name; | ||
} | ||
|
||
/** | ||
* Method exited | ||
* | ||
* @param {ESTree.MethodDefinition} node Node entered | ||
* @return {void} | ||
*/ | ||
function methodExit(node: ESTree.MethodDefinition): void { | ||
if (currentMethod !== null && !superSeen) { | ||
context.report({ | ||
node, | ||
messageId: 'callSuper', | ||
data: { | ||
method: currentMethod | ||
} | ||
}); | ||
} | ||
|
||
currentMethod = null; | ||
superSeen = false; | ||
} | ||
|
||
/** | ||
* Call expression entered | ||
* @param {ESTree.CallExpression} node Node entered | ||
* @return {void} | ||
*/ | ||
function callExpressionEnter(node: ESTree.CallExpression): void { | ||
if (currentMethod === null) { | ||
return; | ||
} | ||
|
||
if ( | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'Super' && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name === currentMethod | ||
) { | ||
superSeen = true; | ||
} | ||
} | ||
|
||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
|
||
return { | ||
ClassExpression: (node: ESTree.Node): void => | ||
classEnter(node as ESTree.Class), | ||
ClassDeclaration: (node: ESTree.Node): void => | ||
classEnter(node as ESTree.Class), | ||
'ClassExpression:exit': classExit, | ||
'ClassDeclaration:exit': classExit, | ||
MethodDefinition: (node: ESTree.Node): void => | ||
methodEnter(node as ESTree.MethodDefinition), | ||
'MethodDefinition:exit': methodExit, | ||
CallExpression: (node: ESTree.CallExpression): void => | ||
callExpressionEnter(node) | ||
}; | ||
} | ||
}; | ||
|
||
export = rule; |
Oops, something went wrong.