eslint-plugin-svelte 3.17.0 → 3.18.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/README.md CHANGED
@@ -301,6 +301,7 @@ These rules relate to possible syntax or logic errors in Svelte code:
301
301
  | [svelte/no-dupe-on-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-on-directives/) | disallow duplicate `on:` directives | :star: |
302
302
  | [svelte/no-dupe-style-properties](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-style-properties/) | disallow duplicate style properties | :star: |
303
303
  | [svelte/no-dupe-use-directives](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-dupe-use-directives/) | disallow duplicate `use:` directives | :star: |
304
+ | [svelte/no-nested-style-tag](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-nested-style-tag/) | disallow `<style>` elements nested inside other elements or blocks | |
304
305
  | [svelte/no-not-function-handler](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/) | disallow use of not function in event handler | :star: |
305
306
  | [svelte/no-object-in-text-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-object-in-text-mustaches/) | disallow objects in text mustache interpolation | :star: |
306
307
  | [svelte/no-raw-special-elements](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-raw-special-elements/) | Checks for invalid raw HTML elements | :star::wrench: |
@@ -348,6 +349,7 @@ These rules relate to better ways of doing things to help you avoid problems:
348
349
  | [svelte/no-useless-children-snippet](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-children-snippet/) | disallow explicit children snippet where it's not needed | :star: |
349
350
  | [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :star::wrench: |
350
351
  | [svelte/prefer-const](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/) | Require `const` declarations for variables that are never reassigned after declared | :wrench: |
352
+ | [svelte/prefer-derived-over-derived-by](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-derived-over-derived-by/) | disallow unnecessary `$derived.by()` when `$derived()` is sufficient | :wrench: |
351
353
  | [svelte/prefer-destructured-store-props](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
352
354
  | [svelte/prefer-writable-derived](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-writable-derived/) | Prefer using writable $derived instead of $state and $effect | :star::bulb: |
353
355
  | [svelte/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | :star: |
package/lib/main.d.ts CHANGED
@@ -14,7 +14,7 @@ export declare const configs: {
14
14
  export declare const rules: Record<string, Rule.RuleModule>;
15
15
  export declare const meta: {
16
16
  name: "eslint-plugin-svelte";
17
- version: "3.17.0";
17
+ version: "3.18.0";
18
18
  };
19
19
  export declare const processors: {
20
20
  '.svelte': typeof processor;
package/lib/meta.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare const name: "eslint-plugin-svelte";
2
- export declare const version: "3.17.0";
2
+ export declare const version: "3.18.0";
package/lib/meta.js CHANGED
@@ -2,4 +2,4 @@
2
2
  // This file has been automatically generated,
3
3
  // in order to update its content execute "pnpm run update"
4
4
  export const name = 'eslint-plugin-svelte';
5
- export const version = '3.17.0';
5
+ export const version = '3.18.0';
@@ -193,6 +193,11 @@ export interface RuleOptions {
193
193
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-navigation-without-resolve/
194
194
  */
195
195
  'svelte/no-navigation-without-resolve'?: Linter.RuleEntry<SvelteNoNavigationWithoutResolve>;
196
+ /**
197
+ * disallow `<style>` elements nested inside other elements or blocks
198
+ * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-nested-style-tag/
199
+ */
200
+ 'svelte/no-nested-style-tag'?: Linter.RuleEntry<[]>;
196
201
  /**
197
202
  * disallow use of not function in event handler
198
203
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-not-function-handler/
@@ -308,6 +313,11 @@ export interface RuleOptions {
308
313
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-const/
309
314
  */
310
315
  'svelte/prefer-const'?: Linter.RuleEntry<SveltePreferConst>;
316
+ /**
317
+ * disallow unnecessary `$derived.by()` when `$derived()` is sufficient
318
+ * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-derived-over-derived-by/
319
+ */
320
+ 'svelte/prefer-derived-over-derived-by'?: Linter.RuleEntry<[]>;
311
321
  /**
312
322
  * destructure values from object stores for better change tracking & fewer redraws
313
323
  * @see https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/
@@ -2,6 +2,7 @@ import { createRule } from '../utils/index.js';
2
2
  import { ReferenceTracker } from '@eslint-community/eslint-utils';
3
3
  import { FindVariableContext } from '../utils/ast-utils.js';
4
4
  import { findVariable } from '../utils/ast-utils.js';
5
+ import { getTypeScriptTools, isNullType, isUndefinedType } from '../utils/ts-utils/index.js';
5
6
  export default createRule('no-navigation-without-resolve', {
6
7
  meta: {
7
8
  docs: {
@@ -43,6 +44,7 @@ export default createRule('no-navigation-without-resolve', {
43
44
  ]
44
45
  },
45
46
  create(context) {
47
+ const tsTools = getTypeScriptTools(context);
46
48
  let resolveReferences = new Set();
47
49
  const ignoreGoto = context.options[0]?.ignoreGoto ?? false;
48
50
  const ignorePushState = context.options[0]?.ignorePushState ?? false;
@@ -55,27 +57,27 @@ export default createRule('no-navigation-without-resolve', {
55
57
  const { goto: gotoCalls, pushState: pushStateCalls, replaceState: replaceStateCalls } = extractFunctionCallReferences(referenceTracker);
56
58
  if (!ignoreGoto) {
57
59
  for (const gotoCall of gotoCalls) {
58
- checkGotoCall(context, gotoCall, resolveReferences);
60
+ checkGotoCall(context, gotoCall, resolveReferences, tsTools);
59
61
  }
60
62
  }
61
63
  if (!ignorePushState) {
62
64
  for (const pushStateCall of pushStateCalls) {
63
- checkShallowNavigationCall(context, pushStateCall, resolveReferences, 'pushStateWithoutResolve');
65
+ checkShallowNavigationCall(context, pushStateCall, resolveReferences, tsTools, 'pushStateWithoutResolve');
64
66
  }
65
67
  }
66
68
  if (!ignoreReplaceState) {
67
69
  for (const replaceStateCall of replaceStateCalls) {
68
- checkShallowNavigationCall(context, replaceStateCall, resolveReferences, 'replaceStateWithoutResolve');
70
+ checkShallowNavigationCall(context, replaceStateCall, resolveReferences, tsTools, 'replaceStateWithoutResolve');
69
71
  }
70
72
  }
71
73
  },
72
74
  ...(!ignoreLinks && {
73
75
  SvelteShorthandAttribute(node) {
74
- checkLinkAttribute(context, node, node.value, resolveReferences);
76
+ checkLinkAttribute(context, node, node.value, resolveReferences, tsTools);
75
77
  },
76
78
  SvelteAttribute(node) {
77
79
  if (node.value.length > 0) {
78
- checkLinkAttribute(context, node, node.value[0].type === 'SvelteMustacheTag' ? node.value[0].expression : node.value[0], resolveReferences);
80
+ checkLinkAttribute(context, node, node.value[0].type === 'SvelteMustacheTag' ? node.value[0].expression : node.value[0], resolveReferences, tsTools);
79
81
  }
80
82
  }
81
83
  })
@@ -143,28 +145,28 @@ function extractFunctionCallReferences(referenceTracker) {
143
145
  };
144
146
  }
145
147
  // Actual function checking
146
- function checkGotoCall(context, call, resolveReferences) {
148
+ function checkGotoCall(context, call, resolveReferences, tsTools) {
147
149
  if (call.arguments.length > 0 &&
148
- !isValueAllowed(new FindVariableContext(context), call.arguments[0], resolveReferences, {})) {
150
+ !isValueAllowed(new FindVariableContext(context), call.arguments[0], resolveReferences, tsTools, {})) {
149
151
  context.report({ loc: call.arguments[0].loc, messageId: 'gotoWithoutResolve' });
150
152
  }
151
153
  }
152
- function checkShallowNavigationCall(context, call, resolveReferences, messageId) {
154
+ function checkShallowNavigationCall(context, call, resolveReferences, tsTools, messageId) {
153
155
  if (call.arguments.length > 0 &&
154
- !isValueAllowed(new FindVariableContext(context), call.arguments[0], resolveReferences, {
156
+ !isValueAllowed(new FindVariableContext(context), call.arguments[0], resolveReferences, tsTools, {
155
157
  allowEmpty: true
156
158
  })) {
157
159
  context.report({ loc: call.arguments[0].loc, messageId });
158
160
  }
159
161
  }
160
- function checkLinkAttribute(context, attribute, value, resolveReferences) {
162
+ function checkLinkAttribute(context, attribute, value, resolveReferences, tsTools) {
161
163
  if (attribute.parent.parent.type === 'SvelteElement' &&
162
164
  attribute.parent.parent.kind === 'html' &&
163
165
  attribute.parent.parent.name.type === 'SvelteName' &&
164
166
  attribute.parent.parent.name.name === 'a' &&
165
167
  attribute.key.name === 'href' &&
166
168
  !hasRelExternal(new FindVariableContext(context), attribute.parent) &&
167
- !isValueAllowed(new FindVariableContext(context), value, resolveReferences, {
169
+ !isValueAllowed(new FindVariableContext(context), value, resolveReferences, tsTools, {
168
170
  allowAbsolute: true,
169
171
  allowFragment: true,
170
172
  allowNullish: true
@@ -201,30 +203,66 @@ function hasRelExternal(ctx, element) {
201
203
  }
202
204
  return false;
203
205
  }
204
- function isValueAllowed(ctx, value, resolveReferences, config) {
206
+ function isValueAllowed(ctx, value, resolveReferences, tsTools, config) {
205
207
  if (value.type === 'Identifier') {
206
208
  const variable = ctx.findVariable(value);
207
209
  if (variable !== null &&
208
210
  variable.identifiers.length > 0 &&
209
- variable.identifiers[0].parent.type === 'VariableDeclarator' &&
210
- variable.identifiers[0].parent.init !== null) {
211
- return isValueAllowed(ctx, variable.identifiers[0].parent.init, resolveReferences, config);
211
+ variable.identifiers[0].parent.type === 'VariableDeclarator') {
212
+ if (expressionIsAllowedType(variable.identifiers[0], config.allowNullish, tsTools)) {
213
+ return true;
214
+ }
215
+ if (variable.identifiers[0].parent.init !== null) {
216
+ return isValueAllowed(ctx, variable.identifiers[0].parent.init, resolveReferences, tsTools, config);
217
+ }
212
218
  }
213
219
  }
214
220
  if (value.type === 'ConditionalExpression') {
215
- return (isValueAllowed(ctx, value.consequent, resolveReferences, config) &&
216
- isValueAllowed(ctx, value.alternate, resolveReferences, config));
221
+ return (isValueAllowed(ctx, value.consequent, resolveReferences, tsTools, config) &&
222
+ isValueAllowed(ctx, value.alternate, resolveReferences, tsTools, config));
217
223
  }
218
224
  if ((config.allowAbsolute && expressionIsAbsoluteUrl(ctx, value)) ||
219
225
  (config.allowEmpty && expressionIsEmpty(value)) ||
220
226
  (config.allowFragment && expressionStartsWith(ctx, value, '#')) ||
221
227
  (config.allowNullish && expressionIsNullish(value)) ||
228
+ expressionIsAllowedType(value, config.allowNullish, tsTools) ||
222
229
  expressionIsResolveCall(ctx, value, resolveReferences)) {
223
230
  return true;
224
231
  }
225
232
  return false;
226
233
  }
227
234
  // Helper functions
235
+ function expressionIsAllowedType(value, allowNullish, tsTools) {
236
+ if (tsTools === null) {
237
+ return false;
238
+ }
239
+ const checker = tsTools.service.program.getTypeChecker();
240
+ const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(value);
241
+ if (tsNode === undefined) {
242
+ return false;
243
+ }
244
+ let nodeType = checker.getTypeAtLocation(tsNode);
245
+ if (allowNullish === true) {
246
+ if (isNullType(nodeType, tsTools.ts) || isUndefinedType(nodeType, tsTools.ts)) {
247
+ return true;
248
+ }
249
+ nodeType = checker.getNonNullableType(nodeType);
250
+ }
251
+ const appTypesModule = checker.getAmbientModules().find((m) => m.name === '"$app/types"');
252
+ if (!appTypesModule) {
253
+ return false;
254
+ }
255
+ const resolvedPathnameSymbol = checker
256
+ .getExportsOfModule(appTypesModule)
257
+ .find((e) => e.name === 'ResolvedPathname');
258
+ if (!resolvedPathnameSymbol) {
259
+ return false;
260
+ }
261
+ const resolvedPathnameType = checker.getDeclaredTypeOfSymbol(resolvedPathnameSymbol);
262
+ // getTypeAtLocation returns the resolved (structural) type without alias information, so we cannot compare aliasSymbols directly. Instead we check structural equivalence by testing assignability in both directions: this correctly rejects strict subtypes like Pathname (Pathname ⊂ ResolvedPathname, so only one direction holds).
263
+ return (checker.isTypeAssignableTo(nodeType, resolvedPathnameType) &&
264
+ checker.isTypeAssignableTo(resolvedPathnameType, nodeType));
265
+ }
228
266
  function expressionIsResolveCall(ctx, node, resolveReferences) {
229
267
  if (node.type === 'CallExpression' &&
230
268
  ((node.callee.type === 'Identifier' && resolveReferences.has(node.callee)) ||
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../types.js").RuleModule;
2
+ export default _default;
@@ -0,0 +1,25 @@
1
+ import { createRule } from '../utils/index.js';
2
+ export default createRule('no-nested-style-tag', {
3
+ meta: {
4
+ docs: {
5
+ description: 'disallow `<style>` elements nested inside other elements or blocks',
6
+ category: 'Possible Errors',
7
+ recommended: false
8
+ },
9
+ schema: [],
10
+ messages: {
11
+ nestedStyle: 'Nested `<style>` elements are not scoped and may lead to unintended styles being applied.'
12
+ },
13
+ type: 'problem'
14
+ },
15
+ create(context) {
16
+ return {
17
+ SvelteElement(node) {
18
+ if (node.kind !== 'html' || node.name.type !== 'SvelteName' || node.name.name !== 'style') {
19
+ return;
20
+ }
21
+ context.report({ node, messageId: 'nestedStyle' });
22
+ }
23
+ };
24
+ }
25
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("../types.js").RuleModule;
2
+ export default _default;
@@ -0,0 +1,68 @@
1
+ import { createRule } from '../utils/index.js';
2
+ export default createRule('prefer-derived-over-derived-by', {
3
+ meta: {
4
+ docs: {
5
+ description: 'disallow unnecessary `$derived.by()` when `$derived()` is sufficient',
6
+ category: 'Best Practices',
7
+ recommended: false
8
+ },
9
+ fixable: 'code',
10
+ schema: [],
11
+ messages: {
12
+ unnecessary: 'Unnecessary use of `$derived.by()`. Use `$derived()` directly for simple expressions.'
13
+ },
14
+ type: 'suggestion',
15
+ conditions: [
16
+ {
17
+ svelteVersions: ['5'],
18
+ runes: [true, 'undetermined']
19
+ }
20
+ ]
21
+ },
22
+ create(context) {
23
+ return {
24
+ CallExpression(node) {
25
+ const { callee } = node;
26
+ if (callee.type !== 'MemberExpression' ||
27
+ callee.computed ||
28
+ callee.object.type !== 'Identifier' ||
29
+ callee.object.name !== '$derived' ||
30
+ callee.property.type !== 'Identifier' ||
31
+ callee.property.name !== 'by') {
32
+ return;
33
+ }
34
+ if (node.arguments.length !== 1) {
35
+ return;
36
+ }
37
+ const arg = node.arguments[0];
38
+ if (arg.type !== 'ArrowFunctionExpression' && arg.type !== 'FunctionExpression') {
39
+ return;
40
+ }
41
+ if (arg.params.length !== 0 || arg.async || arg.generator) {
42
+ return;
43
+ }
44
+ let expressionNode = null;
45
+ if (arg.type === 'ArrowFunctionExpression' && arg.body.type !== 'BlockStatement') {
46
+ expressionNode = arg.body;
47
+ }
48
+ else if (arg.body.type === 'BlockStatement' &&
49
+ arg.body.body.length === 1 &&
50
+ arg.body.body[0].type === 'ReturnStatement' &&
51
+ arg.body.body[0].argument !== null) {
52
+ expressionNode = arg.body.body[0].argument;
53
+ }
54
+ if (expressionNode === null) {
55
+ return;
56
+ }
57
+ context.report({
58
+ node,
59
+ messageId: 'unnecessary',
60
+ fix(fixer) {
61
+ const expressionText = context.sourceCode.getText(expressionNode);
62
+ return fixer.replaceText(node, `$derived(${expressionText})`);
63
+ }
64
+ });
65
+ }
66
+ };
67
+ }
68
+ });
@@ -35,6 +35,7 @@ import noInnerDeclarations from '../rules/no-inner-declarations.js';
35
35
  import noInspect from '../rules/no-inspect.js';
36
36
  import noNavigationWithoutBase from '../rules/no-navigation-without-base.js';
37
37
  import noNavigationWithoutResolve from '../rules/no-navigation-without-resolve.js';
38
+ import noNestedStyleTag from '../rules/no-nested-style-tag.js';
38
39
  import noNotFunctionHandler from '../rules/no-not-function-handler.js';
39
40
  import noObjectInTextMustaches from '../rules/no-object-in-text-mustaches.js';
40
41
  import noRawSpecialElements from '../rules/no-raw-special-elements.js';
@@ -58,6 +59,7 @@ import noUselessChildrenSnippet from '../rules/no-useless-children-snippet.js';
58
59
  import noUselessMustaches from '../rules/no-useless-mustaches.js';
59
60
  import preferClassDirective from '../rules/prefer-class-directive.js';
60
61
  import preferConst from '../rules/prefer-const.js';
62
+ import preferDerivedOverDerivedBy from '../rules/prefer-derived-over-derived-by.js';
61
63
  import preferDestructuredStoreProps from '../rules/prefer-destructured-store-props.js';
62
64
  import preferStyleDirective from '../rules/prefer-style-directive.js';
63
65
  import preferSvelteReactivity from '../rules/prefer-svelte-reactivity.js';
@@ -116,6 +118,7 @@ export const rules = [
116
118
  noInspect,
117
119
  noNavigationWithoutBase,
118
120
  noNavigationWithoutResolve,
121
+ noNestedStyleTag,
119
122
  noNotFunctionHandler,
120
123
  noObjectInTextMustaches,
121
124
  noRawSpecialElements,
@@ -139,6 +142,7 @@ export const rules = [
139
142
  noUselessMustaches,
140
143
  preferClassDirective,
141
144
  preferConst,
145
+ preferDerivedOverDerivedBy,
142
146
  preferDestructuredStoreProps,
143
147
  preferStyleDirective,
144
148
  preferSvelteReactivity,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-svelte",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "description": "ESLint plugin for Svelte using AST",
5
5
  "repository": {
6
6
  "type": "git",
@@ -66,7 +66,7 @@
66
66
  "@typescript-eslint/types": "^8.48.1",
67
67
  "acorn": "^8.15.0",
68
68
  "assert": "^2.1.0",
69
- "esbuild": "^0.27.0",
69
+ "esbuild": "^0.28.0",
70
70
  "eslint-scope": "^9.0.0",
71
71
  "eslint-typegen": "^2.3.0",
72
72
  "eslint-visitor-keys": "^5.0.0",