eslint-plugin-chai-friendly 1.1.0 → 1.2.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/lib/index.js CHANGED
@@ -14,35 +14,32 @@ const plugin = {
14
14
  'no-unused-expressions': require('./rules/no-unused-expressions')
15
15
  },
16
16
  processors: {},
17
- configs: {}
18
- };
19
-
20
- // assign configs here so we can reference `plugin`
21
- Object.assign(plugin.configs, {
22
- // Compatible with ESLint v9 flat configs
23
- recommendedFlat: {
24
- name: 'chai-friendly/recommendedFlat',
25
- plugins: {
26
- 'chai-friendly': plugin
17
+ configs: {
18
+ // Compatible with ESLint v9 flat configs
19
+ recommendedFlat: {
20
+ name: 'chai-friendly/recommendedFlat',
21
+ plugins: {},
22
+ rules: {
23
+ 'chai-friendly/no-unused-expressions': 'error',
24
+ 'no-unused-expressions': 'off',
25
+ '@typescript-eslint/no-unused-expressions': 'off' // disable TypeScript ESLint version
26
+ }
27
27
  },
28
- rules: {
29
- 'chai-friendly/no-unused-expressions': 'error',
30
- 'no-unused-expressions': 'off',
31
- '@typescript-eslint/no-unused-expressions': 'off' // disable TypeScript ESLint version
32
- }
33
- },
34
28
 
35
- // Compatible with ESLint <9 eslintrc configs
36
- recommended: {
37
- plugins: [
38
- 'chai-friendly'
39
- ],
40
- rules: {
41
- 'no-unused-expressions': 0, // disable original rule
42
- '@typescript-eslint/no-unused-expressions': 0, // disable TypeScript ESLint version
43
- 'chai-friendly/no-unused-expressions': 2
29
+ // Compatible with ESLint <9 eslintrc configs
30
+ recommended: {
31
+ plugins: [
32
+ 'chai-friendly'
33
+ ],
34
+ rules: {
35
+ 'no-unused-expressions': 0, // disable original rule
36
+ '@typescript-eslint/no-unused-expressions': 0, // disable TypeScript ESLint version
37
+ 'chai-friendly/no-unused-expressions': 2
38
+ }
44
39
  }
45
40
  }
46
- },)
41
+ };
42
+
43
+ plugin.configs.recommendedFlat.plugins['chai-friendly'] = plugin;
47
44
 
48
45
  module.exports = plugin;
@@ -51,6 +51,9 @@ module.exports = {
51
51
  enforceForJSX: {
52
52
  type: "boolean",
53
53
  default: false
54
+ },
55
+ ignoreDirectives: {
56
+ type: "boolean"
54
57
  }
55
58
  },
56
59
  additionalProperties: false
@@ -66,7 +69,8 @@ module.exports = {
66
69
  allowShortCircuit = config.allowShortCircuit || false,
67
70
  allowTernary = config.allowTernary || false,
68
71
  allowTaggedTemplates = config.allowTaggedTemplates || false,
69
- enforceForJSX = config.enforceForJSX || false;
72
+ enforceForJSX = config.enforceForJSX || false,
73
+ ignoreDirectives = config.ignoreDirectives || false;
70
74
 
71
75
  /**
72
76
  * @param {ASTNode} node - any node
@@ -101,12 +105,19 @@ module.exports = {
101
105
 
102
106
  /**
103
107
  * @param {ASTNode} node - any node
104
- * @param {ASTNode[]} ancestors - the given node's ancestors
108
+ * @returns {boolean} whether the parser marked this node as a directive
109
+ */
110
+ function isParserDirective(node) {
111
+ return typeof node.directive === "string";
112
+ }
113
+
114
+ /**
115
+ * @param {ASTNode} node - any node
105
116
  * @returns {boolean} whether the given node is considered a directive in its current position
106
117
  */
107
- function isDirective(node, ancestors) {
108
- var parent = ancestors[ancestors.length - 1],
109
- grandparent = ancestors[ancestors.length - 2];
118
+ function isDirective(node) {
119
+ var parent = node.parent,
120
+ grandparent = node.parent.parent;
110
121
 
111
122
  /**
112
123
  * https://tc39.es/ecma262/#directive-prologue
@@ -167,6 +178,20 @@ module.exports = {
167
178
  ThisExpression: alwaysTrue,
168
179
  UnaryExpression(node) {
169
180
  return node.operator !== "void" && node.operator !== "delete";
181
+ },
182
+
183
+ // TypeScript-specific node types
184
+ TSAsExpression(node) {
185
+ return Checker.isDisallowed(node.expression);
186
+ },
187
+ TSTypeAssertion(node) {
188
+ return Checker.isDisallowed(node.expression);
189
+ },
190
+ TSNonNullExpression(node) {
191
+ return Checker.isDisallowed(node.expression);
192
+ },
193
+ TSInstantiationExpression(node) {
194
+ return Checker.isDisallowed(node.expression);
170
195
  }
171
196
  });
172
197
 
@@ -255,18 +280,14 @@ module.exports = {
255
280
  return null;
256
281
  }
257
282
 
258
- const sourceCode = context.sourceCode ?? context.getSourceCode();
259
283
  return {
260
284
  ExpressionStatement: function(node) {
261
- var valid = !Checker.isDisallowed(node.expression)
262
- || isDirective(node,
263
- (sourceCode.getAncestors
264
- ? sourceCode.getAncestors(node)
265
- : context.getAncestors()
266
- ))
267
- || isChaiExpectCall(node)
268
- || isChaiShouldCall(node);
269
- if (!valid) {
285
+ if (Checker.isDisallowed(node.expression)
286
+ && !isParserDirective(node)
287
+ && !(ignoreDirectives && isDirective(node))
288
+ && !isChaiExpectCall(node)
289
+ && !isChaiShouldCall(node)
290
+ ) {
270
291
  context.report({node, messageId: "unusedExpression"});
271
292
  }
272
293
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-chai-friendly",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "This plugin makes 'no-unused-expressions' rule friendly towards chai expect statements.",
5
5
  "keywords": [
6
6
  "eslint",
@@ -27,7 +27,9 @@
27
27
  ],
28
28
  "scripts": {
29
29
  "lint": "eslint .",
30
- "test": "nyc mocha tests --recursive && npm run integration-test:all",
30
+ "test": "npm run test:unit && npm run integration-test:all",
31
+ "test:types": "npx tsc -p tests/types/tsconfig.json",
32
+ "test:unit": "nyc mocha tests/ --recursive",
31
33
  "integration-test": "npx eslint examples/test.js --no-ignore",
32
34
  "integration-test:ts": "npx eslint --config examples/eslint.config.js examples/test-ts-specific.ts --no-ignore",
33
35
  "integration-test:all": "npx eslint --config examples/eslint.config.js examples/test.js examples/test-ts-specific.ts --no-ignore"
@@ -38,14 +40,26 @@
38
40
  "devDependencies": {
39
41
  "@babel/core": "^7.25.2",
40
42
  "@babel/eslint-parser": "^7.25.1",
41
- "@typescript-eslint/eslint-plugin": "^8.34.0",
42
- "@typescript-eslint/parser": "^8.34.0",
43
+ "@typescript-eslint/eslint-plugin": "^8.57.0",
44
+ "@typescript-eslint/parser": "^8.57.0",
43
45
  "chai": "^5.1.1",
44
- "eslint": "^9.3.0",
45
- "mocha": "^10.0.0",
46
- "nyc": "^15.1.0",
46
+ "eslint": "^9.39.4",
47
+ "mocha": "^11.7.5",
48
+ "nyc": "^18.0.0",
47
49
  "typescript": "^5.8.3"
48
50
  },
51
+ "overrides": {
52
+ "flatted": "3.4.2",
53
+ "serialize-javascript": "7.0.5",
54
+ "brace-expansion": "5.0.5",
55
+ "@istanbuljs/load-nyc-config": {
56
+ "js-yaml": "3.14.2"
57
+ },
58
+ "mocha": {
59
+ "diff": "8.0.3",
60
+ "js-yaml": "4.1.1"
61
+ }
62
+ },
49
63
  "engines": {
50
64
  "node": ">=0.10.0"
51
65
  },