eslint-plugin-lit 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 James Garbutt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -52,6 +52,18 @@ export default [
52
52
  ];
53
53
  ```
54
54
 
55
+ You can also specify settings that will be shared across all the plugin rules.
56
+
57
+ ```json5
58
+ {
59
+ settings: {
60
+ lit: {
61
+ elementBaseClasses: ['ClassExtendingLitElement'] // Recognize `ClassExtendingLitElement` as a sub-class of LitElement
62
+ }
63
+ }
64
+ }
65
+ ```
66
+
55
67
  ### Custom Configuration
56
68
 
57
69
  If you want more fine-grained configuration, you can instead add a snippet like this to your ESLint configuration file:
@@ -124,10 +136,7 @@ Then extend the recommended eslint config:
124
136
 
125
137
  ```json
126
138
  {
127
- "extends": [
128
- "plugin:wc/recommended",
129
- "plugin:lit/recommended"
130
- ]
139
+ "extends": ["plugin:wc/recommended", "plugin:lit/recommended"]
131
140
  }
132
141
  ```
133
142
 
@@ -38,7 +38,7 @@ export const rule = {
38
38
  return {
39
39
  ClassDeclaration: (node) => {
40
40
  var _a, _b;
41
- if (isLitClass(node)) {
41
+ if (isLitClass(node, context)) {
42
42
  const propertyMap = getPropertyMap(node);
43
43
  for (const [prop, propConfig] of propertyMap.entries()) {
44
44
  if (!propConfig.attribute || propConfig.state) {
@@ -35,7 +35,7 @@ export const rule = {
35
35
  * @return {void}
36
36
  */
37
37
  function classEnter(node) {
38
- if (!isLitClass(node)) {
38
+ if (!isLitClass(node, context)) {
39
39
  return;
40
40
  }
41
41
  inElement = true;
@@ -22,7 +22,7 @@ export const rule = {
22
22
  create(context) {
23
23
  return {
24
24
  ClassDeclaration: (node) => {
25
- if (isLitClass(node)) {
25
+ if (isLitClass(node, context)) {
26
26
  const propertyMap = getPropertyMap(node);
27
27
  const classMembers = getClassFields(node);
28
28
  for (const [prop, { key }] of propertyMap.entries()) {
@@ -37,7 +37,7 @@ export const rule = {
37
37
  * @return {void}
38
38
  */
39
39
  function classEnter(node) {
40
- if (!isLitClass(node)) {
40
+ if (!isLitClass(node, context)) {
41
41
  return;
42
42
  }
43
43
  const props = getPropertyMap(node);
@@ -30,7 +30,7 @@ export const rule = {
30
30
  * @return {void}
31
31
  */
32
32
  function classEnter(node) {
33
- if (!isLitClass(node)) {
33
+ if (!isLitClass(node, context)) {
34
34
  return;
35
35
  }
36
36
  inComponent = true;
@@ -29,7 +29,7 @@ export const rule = {
29
29
  const prefer = context.options[0] !== 'never';
30
30
  return {
31
31
  'ClassExpression,ClassDeclaration': (node) => {
32
- if (!prefer && isLitClass(node)) {
32
+ if (!prefer && isLitClass(node, context)) {
33
33
  for (const member of node.body.body) {
34
34
  if (member.type === 'MethodDefinition' &&
35
35
  member.kind === 'get' &&
package/lib/util.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as ESTree from 'estree';
2
+ import { Rule } from 'eslint';
2
3
  export interface BabelDecorator extends ESTree.BaseNode {
3
4
  type: 'Decorator';
4
5
  expression: ESTree.Expression;
@@ -12,9 +13,10 @@ export type DecoratedNode = ESTree.Node & {
12
13
  /**
13
14
  * Returns if the given node is a lit class
14
15
  * @param {ESTree.Class} clazz
16
+ * @param {Rule.RuleContext} context
15
17
  * @return { boolean }
16
18
  */
17
- export declare function isLitClass(clazz: ESTree.Class): boolean;
19
+ export declare function isLitClass(clazz: ESTree.Class, context: Rule.RuleContext): boolean;
18
20
  /**
19
21
  * Get the name of a node
20
22
  *
@@ -92,3 +94,10 @@ export declare function toSnakeCase(camelCaseStr: string): string;
92
94
  * @return {string}
93
95
  */
94
96
  export declare function toKebabCase(camelCaseStr: string): string;
97
+ /**
98
+ * Retrieves the configured element base class list
99
+ *
100
+ * @param {Rule.RuleContext} context ESLint rule context
101
+ * @return {string[]}
102
+ */
103
+ export declare function getElementBaseClasses(context: Rule.RuleContext): Set<string>;
package/lib/util.js CHANGED
@@ -20,23 +20,25 @@ function hasCustomElementDecorator(node) {
20
20
  /**
21
21
  * Returns if given node has a lit identifier
22
22
  * @param {ESTree.Node} node
23
+ * @param {Set<string>} customElementBases
23
24
  * @return {boolean}
24
25
  */
25
- function hasLitIdentifier(node) {
26
- return node.type === 'Identifier' && node.name === 'LitElement';
26
+ function hasLitIdentifier(node, customElementBases) {
27
+ return node.type === 'Identifier' && customElementBases.has(node.name);
27
28
  }
28
29
  /**
29
30
  * Returns if the given node is a lit element by expression
30
31
  * @param {ESTree.Node} node
32
+ * @param {Set<string>} customElementBases
31
33
  * @return {boolean}
32
34
  */
33
- function isLitByExpression(node) {
35
+ function isLitByExpression(node, customElementBases) {
34
36
  if (node) {
35
- if (hasLitIdentifier(node)) {
37
+ if (hasLitIdentifier(node, customElementBases)) {
36
38
  return true;
37
39
  }
38
40
  if (node.type === 'CallExpression') {
39
- return node.arguments.some(isLitByExpression);
41
+ return node.arguments.some((n) => isLitByExpression(n, customElementBases));
40
42
  }
41
43
  }
42
44
  return false;
@@ -44,16 +46,19 @@ function isLitByExpression(node) {
44
46
  /**
45
47
  * Returns if the given node is a lit class
46
48
  * @param {ESTree.Class} clazz
49
+ * @param {Rule.RuleContext} context
47
50
  * @return { boolean }
48
51
  */
49
- export function isLitClass(clazz) {
52
+ export function isLitClass(clazz, context) {
50
53
  if (hasCustomElementDecorator(clazz)) {
51
54
  return true;
52
55
  }
56
+ const customElementBases = getElementBaseClasses(context);
53
57
  if (clazz.superClass) {
54
- return (hasLitIdentifier(clazz.superClass) || isLitByExpression(clazz.superClass));
58
+ return (hasLitIdentifier(clazz.superClass, customElementBases) ||
59
+ isLitByExpression(clazz.superClass, customElementBases));
55
60
  }
56
- return hasLitIdentifier(clazz);
61
+ return hasLitIdentifier(clazz, customElementBases);
57
62
  }
58
63
  /**
59
64
  * Get the name of a node
@@ -287,3 +292,20 @@ export function toSnakeCase(camelCaseStr) {
287
292
  export function toKebabCase(camelCaseStr) {
288
293
  return camelCaseStr.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
289
294
  }
295
+ /**
296
+ * Retrieves the configured element base class list
297
+ *
298
+ * @param {Rule.RuleContext} context ESLint rule context
299
+ * @return {string[]}
300
+ */
301
+ export function getElementBaseClasses(context) {
302
+ var _a;
303
+ const bases = new Set(['LitElement']);
304
+ if (Array.isArray((_a = context.settings.lit) === null || _a === void 0 ? void 0 : _a.elementBaseClasses)) {
305
+ const configuredBases = context.settings.lit.elementBaseClasses;
306
+ for (const base of configuredBases) {
307
+ bases.add(base);
308
+ }
309
+ }
310
+ return bases;
311
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-lit",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "description": "lit-html support for ESLint",
6
6
  "main": "lib/index.js",