eslint 9.25.0 → 9.25.1

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.
@@ -29,6 +29,8 @@ function alwaysFalse() {
29
29
  /** @type {import('../types').Rule.RuleModule} */
30
30
  module.exports = {
31
31
  meta: {
32
+ dialects: ["javascript", "typescript"],
33
+ language: "javascript",
32
34
  type: "suggestion",
33
35
 
34
36
  docs: {
@@ -83,6 +85,61 @@ module.exports = {
83
85
  },
84
86
  ] = context.options;
85
87
 
88
+ /**
89
+ * Has AST suggesting a directive.
90
+ * @param {ASTNode} node any node
91
+ * @returns {boolean} whether the given node structurally represents a directive
92
+ */
93
+ function looksLikeDirective(node) {
94
+ return (
95
+ node.type === "ExpressionStatement" &&
96
+ node.expression.type === "Literal" &&
97
+ typeof node.expression.value === "string"
98
+ );
99
+ }
100
+
101
+ /**
102
+ * Gets the leading sequence of members in a list that pass the predicate.
103
+ * @param {Function} predicate ([a] -> Boolean) the function used to make the determination
104
+ * @param {a[]} list the input list
105
+ * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
106
+ */
107
+ function takeWhile(predicate, list) {
108
+ for (let i = 0; i < list.length; ++i) {
109
+ if (!predicate(list[i])) {
110
+ return list.slice(0, i);
111
+ }
112
+ }
113
+ return list.slice();
114
+ }
115
+
116
+ /**
117
+ * Gets leading directives nodes in a Node body.
118
+ * @param {ASTNode} node a Program or BlockStatement node
119
+ * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
120
+ */
121
+ function directives(node) {
122
+ return takeWhile(looksLikeDirective, node.body);
123
+ }
124
+
125
+ /**
126
+ * Detect if a Node is a directive.
127
+ * @param {ASTNode} node any node
128
+ * @returns {boolean} whether the given node is considered a directive in its current position
129
+ */
130
+ function isDirective(node) {
131
+ /**
132
+ * https://tc39.es/ecma262/#directive-prologue
133
+ *
134
+ * Only `FunctionBody`, `ScriptBody` and `ModuleBody` can have directive prologue.
135
+ * Class static blocks do not have directive prologue.
136
+ */
137
+ return (
138
+ astUtils.isTopLevelExpressionStatement(node) &&
139
+ directives(node.parent).includes(node)
140
+ );
141
+ }
142
+
86
143
  /**
87
144
  * The member functions return `true` if the type has no side-effects.
88
145
  * Unknown nodes are handled as `false`, then this rule ignores those.
@@ -154,7 +211,7 @@ module.exports = {
154
211
  ExpressionStatement(node) {
155
212
  if (
156
213
  Checker.isDisallowed(node.expression) &&
157
- !astUtils.isDirective(node)
214
+ !isDirective(node)
158
215
  ) {
159
216
  context.report({ node, messageId: "unusedExpression" });
160
217
  }
@@ -1088,13 +1088,13 @@ function isConstant(scope, node, inBooleanPosition) {
1088
1088
  }
1089
1089
 
1090
1090
  /**
1091
- * Checks whether a node is an ExpressionStatement at the top level of a file or function body.
1091
+ * Checks whether a node is an ExpressionStatement at the top level of a file, function body, or TypeScript module block.
1092
1092
  * A top-level ExpressionStatement node is a directive if it contains a single unparenthesized
1093
1093
  * string literal and if it occurs either as the first sibling or immediately after another
1094
1094
  * directive.
1095
1095
  * @param {ASTNode} node The node to check.
1096
1096
  * @returns {boolean} Whether or not the node is an ExpressionStatement at the top level of a
1097
- * file or function body.
1097
+ * file, function body, or TypeScript module block.
1098
1098
  */
1099
1099
  function isTopLevelExpressionStatement(node) {
1100
1100
  if (node.type !== "ExpressionStatement") {
@@ -1104,6 +1104,7 @@ function isTopLevelExpressionStatement(node) {
1104
1104
 
1105
1105
  return (
1106
1106
  parent.type === "Program" ||
1107
+ parent.type === "TSModuleBlock" ||
1107
1108
  (parent.type === "BlockStatement" && isFunction(parent.parent))
1108
1109
  );
1109
1110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "9.25.0",
3
+ "version": "9.25.1",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "type": "commonjs",
@@ -110,7 +110,7 @@
110
110
  "@eslint/config-helpers": "^0.2.1",
111
111
  "@eslint/core": "^0.13.0",
112
112
  "@eslint/eslintrc": "^3.3.1",
113
- "@eslint/js": "9.25.0",
113
+ "@eslint/js": "9.25.1",
114
114
  "@eslint/plugin-kit": "^0.2.8",
115
115
  "@humanfs/node": "^0.16.6",
116
116
  "@humanwhocodes/module-importer": "^1.0.1",