@gost-kit/eslint-plugin 0.0.0-stage → 0.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/LICENSE +21 -0
  3. package/README.md +52 -2
  4. package/RULES.md +312 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.js +15 -0
  7. package/dist/lib/boolean-type.d.ts +2 -0
  8. package/dist/lib/boolean-type.js +14 -0
  9. package/dist/lib/common-source.d.ts +2 -0
  10. package/dist/lib/common-source.js +39 -0
  11. package/dist/lib/component-refs.d.ts +5 -0
  12. package/dist/lib/component-refs.js +58 -0
  13. package/dist/lib/dispatch-type.d.ts +2 -0
  14. package/dist/lib/dispatch-type.js +103 -0
  15. package/dist/lib/handler-body.d.ts +2 -0
  16. package/dist/lib/handler-body.js +32 -0
  17. package/dist/lib/member-key.d.ts +4 -0
  18. package/dist/lib/member-key.js +18 -0
  19. package/dist/lib/optional-call.d.ts +2 -0
  20. package/dist/lib/optional-call.js +55 -0
  21. package/dist/lib/raw-state.d.ts +2 -0
  22. package/dist/lib/raw-state.js +27 -0
  23. package/dist/lib/reactive-context.d.ts +6 -0
  24. package/dist/lib/reactive-context.js +48 -0
  25. package/dist/lib/ready-boolean-expression.d.ts +2 -0
  26. package/dist/lib/ready-boolean-expression.js +17 -0
  27. package/dist/lib/rune-callee.d.ts +2 -0
  28. package/dist/lib/rune-callee.js +19 -0
  29. package/dist/lib/selector-fix.d.ts +2 -0
  30. package/dist/lib/selector-fix.js +122 -0
  31. package/dist/lib/store-state.d.ts +2 -0
  32. package/dist/lib/store-state.js +57 -0
  33. package/dist/lib/svelte-scripts.d.ts +5 -0
  34. package/dist/lib/svelte-scripts.js +25 -0
  35. package/dist/lib/ui-handlers.d.ts +10 -0
  36. package/dist/lib/ui-handlers.js +84 -0
  37. package/dist/lib/value-variable.d.ts +2 -0
  38. package/dist/lib/value-variable.js +25 -0
  39. package/dist/rules/no-component-state.d.ts +2 -0
  40. package/dist/rules/no-component-state.js +46 -0
  41. package/dist/rules/ready-boolean-condition.d.ts +2 -0
  42. package/dist/rules/ready-boolean-condition.js +49 -0
  43. package/dist/rules/require-feature-state-selector.d.ts +2 -0
  44. package/dist/rules/require-feature-state-selector.js +58 -0
  45. package/dist/rules/single-intent-dispatch.d.ts +2 -0
  46. package/dist/rules/single-intent-dispatch.js +89 -0
  47. package/package.json +55 -4
@@ -0,0 +1,84 @@
1
+ import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
2
+ import { findValueVariable } from './value-variable.js';
3
+ import { inScript } from './svelte-scripts.js';
4
+ export const createUiHandlerCollector = (source, scripts) => {
5
+ const handlers = new Set();
6
+ const resolve = (node, inline, seen) => {
7
+ if (node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
8
+ node.type === AST_NODE_TYPES.FunctionExpression ||
9
+ node.type === AST_NODE_TYPES.FunctionDeclaration) {
10
+ return inline || inScript(node, scripts) ? node : undefined;
11
+ }
12
+ if (node.type === AST_NODE_TYPES.TSSatisfiesExpression ||
13
+ node.type === AST_NODE_TYPES.TSAsExpression ||
14
+ node.type === AST_NODE_TYPES.TSTypeAssertion ||
15
+ node.type === AST_NODE_TYPES.TSNonNullExpression ||
16
+ node.type === AST_NODE_TYPES.ChainExpression) {
17
+ return resolve(node.expression, inline, seen);
18
+ }
19
+ if (node.type !== AST_NODE_TYPES.Identifier) {
20
+ return undefined;
21
+ }
22
+ const variable = findValueVariable(source.getScope(node), node.name);
23
+ if (!variable ||
24
+ seen.has(variable) ||
25
+ variable.references.some((reference) => {
26
+ return reference.isWrite() && !reference.init;
27
+ })) {
28
+ return undefined;
29
+ }
30
+ const next = new Set(seen).add(variable);
31
+ return variable.defs
32
+ .map((definition) => {
33
+ if (definition.type === 'FunctionName' &&
34
+ definition.node.type === AST_NODE_TYPES.FunctionDeclaration) {
35
+ return resolve(definition.node, false, next);
36
+ }
37
+ if (definition.type === 'Variable' &&
38
+ definition.parent.kind === 'const' &&
39
+ definition.node.id.type === AST_NODE_TYPES.Identifier &&
40
+ definition.node.init) {
41
+ return resolve(definition.node.init, false, next);
42
+ }
43
+ return undefined;
44
+ })
45
+ .find((handler) => {
46
+ return !!handler;
47
+ });
48
+ };
49
+ const add = (expression) => {
50
+ const handler = resolve(expression, true, new Set());
51
+ if (handler) {
52
+ handlers.add(handler);
53
+ }
54
+ };
55
+ return {
56
+ handlers,
57
+ attribute(node) {
58
+ // Parser-specific listeners are the only boundary for extended Svelte nodes.
59
+ const attribute = node;
60
+ if (!attribute.key.name.startsWith('on') ||
61
+ attribute.key.name.length <= 2) {
62
+ return;
63
+ }
64
+ attribute.value.forEach((value) => {
65
+ if (value.type === 'SvelteMustacheTag') {
66
+ add(value.expression);
67
+ }
68
+ });
69
+ },
70
+ shorthand(node) {
71
+ const attribute = node;
72
+ if (attribute.key.name.startsWith('on') &&
73
+ attribute.key.name.length > 2) {
74
+ add(attribute.value);
75
+ }
76
+ },
77
+ directive(node) {
78
+ const directive = node;
79
+ if (directive.kind === 'EventHandler' && directive.expression) {
80
+ add(directive.expression);
81
+ }
82
+ },
83
+ };
84
+ };
@@ -0,0 +1,2 @@
1
+ import { type TSESLint } from '@typescript-eslint/utils';
2
+ export declare const findValueVariable: (initialScope: TSESLint.Scope.Scope, name: string) => import("@typescript-eslint/scope-manager").Variable | undefined;
@@ -0,0 +1,25 @@
1
+ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
2
+ // A nearer type-only declaration does not shadow a runtime value in an outer scope.
3
+ export const findValueVariable = (initialScope, name) => {
4
+ for (let scope = initialScope; scope; scope = scope.upper) {
5
+ const variable = scope.set.get(name);
6
+ // Plain JavaScript parsers do not supply the TypeScript namespace flag.
7
+ if (variable &&
8
+ variable.isValueVariable !== false &&
9
+ (variable.defs.length === 0 ||
10
+ variable.defs.some((definition) => {
11
+ if (definition.isVariableDefinition === false) {
12
+ return false;
13
+ }
14
+ if (definition.type !== 'ImportBinding') {
15
+ return true;
16
+ }
17
+ return (definition.parent.importKind !== 'type' &&
18
+ !(definition.node.type === AST_NODE_TYPES.ImportSpecifier &&
19
+ definition.node.importKind === 'type'));
20
+ }))) {
21
+ return variable;
22
+ }
23
+ }
24
+ return undefined;
25
+ };
@@ -0,0 +1,2 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const noComponentState: ESLintUtils.RuleModule<"componentState", [], unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,46 @@
1
+ import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
2
+ import { runeCallee } from '../lib/rune-callee.js';
3
+ import { createComponentRefCheck } from '../lib/component-refs.js';
4
+ export const noComponentState = ESLintUtils.RuleCreator.withoutDocs({
5
+ meta: {
6
+ type: 'suggestion',
7
+ docs: {
8
+ description: 'Disallow component-owned logical state in explicitly selected files.',
9
+ },
10
+ schema: [],
11
+ messages: {
12
+ componentState: '{{rune}} creates component-owned state. Move logical state to root/feature state and use intent actions, or exclude this independent UI component from the rule scope.',
13
+ },
14
+ },
15
+ defaultOptions: [],
16
+ create(context) {
17
+ const source = context.sourceCode;
18
+ const refs = createComponentRefCheck(source);
19
+ const calls = [];
20
+ return {
21
+ SvelteDirective: refs.recordBinding,
22
+ CallExpression(node) {
23
+ const callee = node.callee;
24
+ const identifier = runeCallee(callee, source, '$state', 'raw') ??
25
+ runeCallee(callee, source, '$bindable');
26
+ if (!identifier) {
27
+ return;
28
+ }
29
+ calls.push({
30
+ node,
31
+ rune: callee.type === AST_NODE_TYPES.MemberExpression
32
+ ? '$state.raw'
33
+ : identifier.name,
34
+ });
35
+ },
36
+ 'Program:exit'() {
37
+ calls.forEach(({ node, rune }) => {
38
+ if (rune !== '$bindable' && refs.allows(node)) {
39
+ return;
40
+ }
41
+ context.report({ node, messageId: 'componentState', data: { rune } });
42
+ });
43
+ },
44
+ };
45
+ },
46
+ });
@@ -0,0 +1,2 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const readyBooleanCondition: ESLintUtils.RuleModule<"readyBoolean", [], unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,49 @@
1
+ import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
2
+ import { readyBooleanExpression } from '../lib/ready-boolean-expression.js';
3
+ import { booleanType } from '../lib/boolean-type.js';
4
+ export const readyBooleanCondition = ESLintUtils.RuleCreator.withoutDocs({
5
+ meta: {
6
+ type: 'suggestion',
7
+ docs: {
8
+ description: 'Require ready boolean reads in Svelte if/else-if conditions.',
9
+ },
10
+ schema: [],
11
+ messages: {
12
+ readyBoolean: 'Prepare a named boolean in a presenter and read it here instead of computing a condition or using truthiness.',
13
+ },
14
+ },
15
+ defaultOptions: [],
16
+ create(context) {
17
+ if (!context.filename.endsWith('.svelte')) {
18
+ return {};
19
+ }
20
+ let checkType;
21
+ return {
22
+ SvelteIfBlock(node) {
23
+ const expression = node.expression;
24
+ if (expression.type === AST_NODE_TYPES.Literal &&
25
+ (expression.value === true || expression.value === false)) {
26
+ return;
27
+ }
28
+ if (!readyBooleanExpression(expression)) {
29
+ context.report({ node: expression, messageId: 'readyBoolean' });
30
+ return;
31
+ }
32
+ if (!checkType) {
33
+ const services = ESLintUtils.getParserServices(context);
34
+ const options = services.program.getCompilerOptions();
35
+ if (!(options.strictNullChecks ?? options.strict ?? false)) {
36
+ throw new Error('gost/ready-boolean-condition requires strictNullChecks (or strict) for typed conditions.');
37
+ }
38
+ const checker = services.program.getTypeChecker();
39
+ checkType = (condition) => {
40
+ return booleanType(checker, services.getTypeAtLocation(condition));
41
+ };
42
+ }
43
+ if (!checkType(expression)) {
44
+ context.report({ node: expression, messageId: 'readyBoolean' });
45
+ }
46
+ },
47
+ };
48
+ },
49
+ });
@@ -0,0 +1,2 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const requireFeatureStateSelector: ESLintUtils.RuleModule<"selectFeature", [], unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,58 @@
1
+ import { svelteScripts, scriptHasAttribute, inScript, } from '../lib/svelte-scripts.js';
2
+ import { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils';
3
+ import { memberKey } from '../lib/member-key.js';
4
+ import { createStoreStateCheck } from '../lib/store-state.js';
5
+ import { reactiveContext } from '../lib/reactive-context.js';
6
+ import { createRawStateCheck } from '../lib/raw-state.js';
7
+ import { selectorFix } from '../lib/selector-fix.js';
8
+ export const requireFeatureStateSelector = ESLintUtils.RuleCreator.withoutDocs({
9
+ meta: {
10
+ type: 'suggestion',
11
+ docs: { description: 'Select Store state before reactive calls.' },
12
+ schema: [],
13
+ fixable: 'code',
14
+ messages: {
15
+ selectFeature: 'Select the feature with a separate $derived before computing its presentation: const user = $derived(store.state.user); const view = $derived(presenter(user)); This skips downstream updates when the feature reference is unchanged.',
16
+ },
17
+ },
18
+ defaultOptions: [],
19
+ create(context) {
20
+ const typedScripts = svelteScripts(context.sourceCode)?.filter((script) => {
21
+ return scriptHasAttribute(script, 'lang', 'ts');
22
+ });
23
+ if (typedScripts?.length === 0) {
24
+ return {};
25
+ }
26
+ const services = ESLintUtils.getParserServices(context);
27
+ const isStore = createStoreStateCheck(services);
28
+ const isRawState = createRawStateCheck(context.sourceCode);
29
+ return {
30
+ MemberExpression(node) {
31
+ if (typedScripts && !inScript(node, typedScripts)) {
32
+ return;
33
+ }
34
+ const storeState = memberKey(node) === 'state' && isStore(node.object);
35
+ if (!storeState && !isRawState(node.object)) {
36
+ return;
37
+ }
38
+ const reactive = reactiveContext(node, context.sourceCode);
39
+ if (!reactive) {
40
+ return;
41
+ }
42
+ const parent = node.parent;
43
+ const location = storeState &&
44
+ parent.type === AST_NODE_TYPES.MemberExpression &&
45
+ parent.object === node
46
+ ? parent
47
+ : node;
48
+ context.report({
49
+ node: location,
50
+ messageId: 'selectFeature',
51
+ fix: reactive.call.type === AST_NODE_TYPES.CallExpression
52
+ ? selectorFix(storeState ? node : node.object, reactive.derived, reactive.call, context.sourceCode)
53
+ : undefined,
54
+ });
55
+ },
56
+ };
57
+ },
58
+ });
@@ -0,0 +1,2 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ export declare const singleIntentDispatch: ESLintUtils.RuleModule<"multipleDispatch" | "conditionalDispatch", [], unknown, ESLintUtils.RuleListener>;
@@ -0,0 +1,89 @@
1
+ import { AST_NODE_TYPES, ESLintUtils, } from '@typescript-eslint/utils';
2
+ import { isOptionalCall } from '../lib/optional-call.js';
3
+ import { createDispatchCheck } from '../lib/dispatch-type.js';
4
+ import { createUiHandlerCollector } from '../lib/ui-handlers.js';
5
+ import { visitHandlerBody } from '../lib/handler-body.js';
6
+ import { scriptHasAttribute, svelteScripts } from '../lib/svelte-scripts.js';
7
+ export const singleIntentDispatch = ESLintUtils.RuleCreator.withoutDocs({
8
+ meta: {
9
+ type: 'suggestion',
10
+ docs: { description: 'Forward one intent in business UI callbacks.' },
11
+ schema: [],
12
+ messages: {
13
+ multipleDispatch: 'Forward one intent action; move business transitions to the reducer.',
14
+ conditionalDispatch: 'Move business decisions to the reducer and browser event handling to a UI-Kit component.',
15
+ },
16
+ },
17
+ defaultOptions: [],
18
+ create(context) {
19
+ const source = context.sourceCode;
20
+ const scripts = svelteScripts(source)?.filter((script) => {
21
+ return (scriptHasAttribute(script, 'lang', 'ts') &&
22
+ !scriptHasAttribute(script, 'module') &&
23
+ !scriptHasAttribute(script, 'context', 'module'));
24
+ });
25
+ if (!scripts?.length) {
26
+ return {};
27
+ }
28
+ const isDispatch = createDispatchCheck(ESLintUtils.getParserServices(context));
29
+ const collector = createUiHandlerCollector(source, scripts);
30
+ return {
31
+ SvelteAttribute: collector.attribute,
32
+ SvelteShorthandAttribute: collector.shorthand,
33
+ SvelteDirective: collector.directive,
34
+ 'Program:exit'() {
35
+ collector.handlers.forEach((handler) => {
36
+ let calls = 0;
37
+ let condition;
38
+ if (!handler.body) {
39
+ return;
40
+ }
41
+ const inspect = (node) => {
42
+ if (node.type === AST_NODE_TYPES.CallExpression &&
43
+ isDispatch(node.callee)) {
44
+ calls += 1;
45
+ if (isOptionalCall(node)) {
46
+ condition ??= node;
47
+ }
48
+ // A Dispatch default argument runs conditionally at invocation,
49
+ // even though its AssignmentPattern is not a logical expression.
50
+ condition ??= source.getAncestors(node).find((ancestor) => {
51
+ return (ancestor.type === AST_NODE_TYPES.AssignmentPattern &&
52
+ handler.params.some((parameter) => {
53
+ return (ancestor.range[0] >= parameter.range[0] &&
54
+ ancestor.range[1] <= parameter.range[1]);
55
+ }));
56
+ });
57
+ }
58
+ if (node.type === AST_NODE_TYPES.IfStatement ||
59
+ node.type === AST_NODE_TYPES.SwitchStatement ||
60
+ node.type === AST_NODE_TYPES.ForStatement ||
61
+ node.type === AST_NODE_TYPES.ForInStatement ||
62
+ node.type === AST_NODE_TYPES.ForOfStatement ||
63
+ node.type === AST_NODE_TYPES.WhileStatement ||
64
+ node.type === AST_NODE_TYPES.DoWhileStatement ||
65
+ node.type === AST_NODE_TYPES.ConditionalExpression ||
66
+ node.type === AST_NODE_TYPES.LogicalExpression ||
67
+ (node.type === AST_NODE_TYPES.AssignmentExpression &&
68
+ ['??=', '&&=', '||='].includes(node.operator))) {
69
+ condition ??= node;
70
+ }
71
+ };
72
+ handler.params.forEach((parameter) => {
73
+ visitHandlerBody(parameter, source, inspect);
74
+ });
75
+ visitHandlerBody(handler.body, source, inspect);
76
+ if (calls > 1) {
77
+ context.report({ node: handler, messageId: 'multipleDispatch' });
78
+ }
79
+ else if (calls === 1 && condition) {
80
+ context.report({
81
+ node: condition,
82
+ messageId: 'conditionalDispatch',
83
+ });
84
+ }
85
+ });
86
+ },
87
+ };
88
+ },
89
+ });
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@gost-kit/eslint-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "GOST architecture rules for agent-assisted frontend development.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": "^26.0.0"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "CHANGELOG.md",
13
+ "RULES.md"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "scripts": {
23
+ "build": "npm run clean && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
24
+ "check": "tsc -p tsconfig.json",
25
+ "test": "vitest run",
26
+ "prepack": "npm run build",
27
+ "clean": "node --input-type=module -e \"import { rmSync } from 'node:fs'; rmSync('dist', { recursive: true, force: true });\""
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/antonsmolko/gost-kit.git",
32
+ "directory": "packages/eslint-plugin"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "devDependencies": {
38
+ "@types/node": "26.6.5",
39
+ "typescript": "5.9.3",
40
+ "tsc-alias": "1.9.9",
41
+ "vitest": "5.0.5",
42
+ "eslint": "10.14.0",
43
+ "svelte-eslint-parser": "1.10.1",
44
+ "typescript-eslint": "8.72.1",
45
+ "svelte": "5.57.2",
46
+ "@gost-kit/common": "0.3.0",
47
+ "@gost-kit/svelte": "0.2.0",
48
+ "@typescript-eslint/rule-tester": "8.72.1"
49
+ },
50
+ "dependencies": {
51
+ "@typescript-eslint/utils": "8.72.1"
52
+ },
53
+ "peerDependencies": {
54
+ "eslint": "^10.14.0",
55
+ "typescript": "^5.9.3"
56
+ }
57
+ }