@venizia/dev-configs 0.5.0-8 → 0.5.0-9
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 +15 -11
- package/dist/checks/purity/manifest.js +11 -3
- package/dist/checks/purity/manifest.js.map +1 -1
- package/dist/checks/purity/probe.js +19 -2
- package/dist/checks/purity/probe.js.map +1 -1
- package/dist/checks/split-report.js +2 -1
- package/dist/checks/split-report.js.map +1 -1
- package/dist/checks/test-controls.d.ts +9 -1
- package/dist/checks/test-controls.js +9 -3
- package/dist/checks/test-controls.js.map +1 -1
- package/dist/checks/test-dead-code.d.ts +9 -1
- package/dist/checks/test-dead-code.js +13 -2
- package/dist/checks/test-dead-code.js.map +1 -1
- package/dist/checks/test-mutate.d.ts +9 -1
- package/dist/checks/test-mutate.js +12 -6
- package/dist/checks/test-mutate.js.map +1 -1
- package/dist/cli/check.d.ts +0 -1
- package/dist/cli/check.js +41 -67
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/clean.js +3 -2
- package/dist/cli/clean.js.map +1 -1
- package/dist/cli/codemod/any-aliases.js +13 -12
- package/dist/cli/codemod/any-aliases.js.map +1 -1
- package/dist/cli/codemod/async-style.d.ts +1 -1
- package/dist/cli/codemod/async-style.js +17 -16
- package/dist/cli/codemod/async-style.js.map +1 -1
- package/dist/cli/codemod/binding-keys.js +14 -13
- package/dist/cli/codemod/binding-keys.js.map +1 -1
- package/dist/cli/codemod/class-renames.d.ts +1 -1
- package/dist/cli/codemod/class-renames.js +30 -11
- package/dist/cli/codemod/class-renames.js.map +1 -1
- package/dist/cli/codemod/connector-imports.js +12 -11
- package/dist/cli/codemod/connector-imports.js.map +1 -1
- package/dist/cli/codemod/const-classes.d.ts +1 -1
- package/dist/cli/codemod/const-classes.js +21 -20
- package/dist/cli/codemod/const-classes.js.map +1 -1
- package/dist/cli/codemod/drop-redundant-casts.d.ts +6 -1
- package/dist/cli/codemod/drop-redundant-casts.js +27 -19
- package/dist/cli/codemod/drop-redundant-casts.js.map +1 -1
- package/dist/cli/codemod/env-get.d.ts +3 -3
- package/dist/cli/codemod/env-get.js +18 -17
- package/dist/cli/codemod/env-get.js.map +1 -1
- package/dist/cli/codemod/files.js +2 -2
- package/dist/cli/codemod/files.js.map +1 -1
- package/dist/cli/codemod/function-to-arrow.d.ts +76 -0
- package/dist/cli/codemod/function-to-arrow.js +357 -0
- package/dist/cli/codemod/function-to-arrow.js.map +1 -0
- package/dist/cli/codemod/index.js +19 -4
- package/dist/cli/codemod/index.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/index.js +13 -12
- package/dist/cli/codemod/namespace-functions/index.js.map +1 -1
- package/dist/cli/codemod/typed-route-context.d.ts +27 -0
- package/dist/cli/codemod/typed-route-context.js +570 -0
- package/dist/cli/codemod/typed-route-context.js.map +1 -0
- package/dist/cli/index.js +5 -5
- package/dist/cli/index.js.map +1 -1
- package/dist/common/cli-output.d.ts +16 -0
- package/dist/common/cli-output.js +49 -0
- package/dist/common/cli-output.js.map +1 -0
- package/dist/compilers/bun.js +1 -1
- package/dist/compilers/bun.js.map +1 -1
- package/dist/linters/eslint.js +1 -1
- package/dist/linters/eslint.js.map +1 -1
- package/dist/linters/house/browser-api-patterns.d.ts +1 -3
- package/dist/linters/house/browser-api-patterns.js +1 -3
- package/dist/linters/house/browser-api-patterns.js.map +1 -1
- package/dist/linters/house/common.d.ts +8 -2
- package/dist/linters/house/common.js +8 -2
- package/dist/linters/house/common.js.map +1 -1
- package/dist/linters/house/configs.js +8 -6
- package/dist/linters/house/configs.js.map +1 -1
- package/dist/linters/house/const-class-values.rule.d.ts +2 -2
- package/dist/linters/house/const-class-values.rule.js +14 -5
- package/dist/linters/house/const-class-values.rule.js.map +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.d.ts +2 -2
- package/dist/linters/house/effect-cleanup-consistency.rule.js +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.js.map +1 -1
- package/dist/linters/house/framework-api-patterns.d.ts +54 -0
- package/dist/linters/house/framework-api-patterns.js +62 -0
- package/dist/linters/house/framework-api-patterns.js.map +1 -0
- package/dist/linters/house/framework-exports.js +3 -3
- package/dist/linters/house/framework-exports.js.map +1 -1
- package/dist/linters/house/index.d.ts +3 -4
- package/dist/linters/house/index.js +4 -6
- package/dist/linters/house/index.js.map +1 -1
- package/dist/linters/house/max-params-own.rule.d.ts +2 -2
- package/dist/linters/house/max-params-own.rule.js +1 -1
- package/dist/linters/house/max-params-own.rule.js.map +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.d.ts +2 -2
- package/dist/linters/house/no-async-hook-callback.rule.js +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.js.map +1 -1
- package/dist/linters/house/no-await-new-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-new-chain.rule.js +1 -1
- package/dist/linters/house/no-await-new-chain.rule.js.map +1 -1
- package/dist/linters/house/no-await-promise-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-promise-chain.rule.js +1 -1
- package/dist/linters/house/no-await-promise-chain.rule.js.map +1 -1
- package/dist/linters/house/no-inline-await.rule.d.ts +2 -2
- package/dist/linters/house/no-inline-await.rule.js +1 -1
- package/dist/linters/house/no-inline-await.rule.js.map +1 -1
- package/dist/linters/house/no-leaked-render.rule.d.ts +2 -2
- package/dist/linters/house/no-leaked-render.rule.js +1 -1
- package/dist/linters/house/no-leaked-render.rule.js.map +1 -1
- package/dist/linters/house/no-new-error.rule.d.ts +2 -2
- package/dist/linters/house/no-new-error.rule.js +2 -2
- package/dist/linters/house/no-new-error.rule.js.map +1 -1
- package/dist/linters/house/no-parent-import.rule.d.ts +5 -2
- package/dist/linters/house/no-parent-import.rule.js +5 -2
- package/dist/linters/house/no-parent-import.rule.js.map +1 -1
- package/dist/linters/house/no-return-await.rule.d.ts +2 -2
- package/dist/linters/house/no-return-await.rule.js +1 -1
- package/dist/linters/house/no-return-await.rule.js.map +1 -1
- package/dist/linters/house/no-shadow-framework-type.rule.d.ts +2 -2
- package/dist/linters/house/no-shadow-framework-type.rule.js +2 -2
- package/dist/linters/house/no-shadow-framework-type.rule.js.map +1 -1
- package/dist/linters/house/no-silent-catch.rule.d.ts +2 -2
- package/dist/linters/house/no-silent-catch.rule.js +3 -3
- package/dist/linters/house/no-silent-catch.rule.js.map +1 -1
- package/dist/linters/house/no-unbraced-case.rule.d.ts +2 -2
- package/dist/linters/house/no-unbraced-case.rule.js +1 -1
- package/dist/linters/house/no-unbraced-case.rule.js.map +1 -1
- package/dist/linters/house/plugin.d.ts +1 -1
- package/dist/linters/house/plugin.js +6 -5
- package/dist/linters/house/plugin.js.map +1 -1
- package/dist/linters/house/prefer-framework-api.rule.d.ts +39 -7
- package/dist/linters/house/prefer-framework-api.rule.js +285 -12
- package/dist/linters/house/prefer-framework-api.rule.js.map +1 -1
- package/dist/linters/house/prefer-promise-sleep.rule.d.ts +2 -2
- package/dist/linters/house/prefer-promise-sleep.rule.js +3 -2
- package/dist/linters/house/prefer-promise-sleep.rule.js.map +1 -1
- package/dist/linters/house/prefer-tnullable.rule.d.ts +2 -2
- package/dist/linters/house/prefer-tnullable.rule.js +2 -2
- package/dist/linters/house/prefer-tnullable.rule.js.map +1 -1
- package/dist/linters/house/typed-route-context.rule.d.ts +19 -0
- package/dist/linters/house/typed-route-context.rule.js +322 -0
- package/dist/linters/house/typed-route-context.rule.js.map +1 -0
- package/dist/linters/house/zod-schema-convention.rule.d.ts +82 -0
- package/dist/linters/house/zod-schema-convention.rule.js +477 -0
- package/dist/linters/house/zod-schema-convention.rule.js.map +1 -0
- package/dist/linters/oxlint.js +11 -1
- package/dist/linters/oxlint.js.map +1 -1
- package/dist/linters/react-eslint.js.map +1 -1
- package/dist/linters/rules/naming-convention.rule.d.ts +4 -1
- package/dist/linters/rules/naming-convention.rule.js +30 -1
- package/dist/linters/rules/naming-convention.rule.js.map +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js.map +1 -1
- package/dist/testing/spawn.utility.js +1 -1
- package/dist/testing/spawn.utility.js.map +1 -1
- package/package.json +5 -5
- package/dist/checks/use-when-baseline.d.ts +0 -25
- package/dist/checks/use-when-baseline.js +0 -38
- package/dist/checks/use-when-baseline.js.map +0 -1
- package/dist/linters/house/framework-rule-apis.d.ts +0 -24
- package/dist/linters/house/framework-rule-apis.js +0 -48
- package/dist/linters/house/framework-rule-apis.js.map +0 -1
- package/dist/linters/house/public-surface.d.ts +0 -57
- package/dist/linters/house/public-surface.js +0 -174
- package/dist/linters/house/public-surface.js.map +0 -1
- package/dist/linters/house/published-package-configs.d.ts +0 -23
- package/dist/linters/house/published-package-configs.js +0 -42
- package/dist/linters/house/published-package-configs.js.map +0 -1
- package/dist/linters/house/require-use-when.rule.d.ts +0 -42
- package/dist/linters/house/require-use-when.rule.js +0 -175
- package/dist/linters/house/require-use-when.rule.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-promise-sleep.rule.js","sourceRoot":"","sources":["../../../src/linters/house/prefer-promise-sleep.rule.ts"],"names":[],"mappings":";;;AAEA,qCAAyE;AACzE,qDAAiD;AAEjD;;;;;;;GAOG;AACH,MAAa,sBAAsB;aACjB,SAAI,GAAG,GAAG,+BAAsB,uBAAuB,CAAC;aAExD,mBAAc,GAAG,6BAA6B,CAAC;IAE/D,8FAA8F;IACtF,MAAM,CAAC,OAAO,CAAC,IAA2B;QAChD,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QAC1B,MAAM,IAAI,GAAG,gBAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;QAEjD,IACE,IAAI,KAAK,qBAAY,CAAC,yBAAyB;YAC/C,IAAI,KAAK,qBAAY,CAAC,mBAAmB,EACzC,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,MAAM,GAAY,gBAAO,CAAC,SAAS,CAAC,EAAE,IAAI,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C,KAAK,qBAAY,CAAC,mBAAmB,EACrE,CAAC;wBACD,OAAO;oBACT,CAAC;oBAED,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI;wBACJ,SAAS,EAAE,OAAO;wBAClB,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE;4BACb,IACE,MAAM,KAAK,SAAS;gCACpB,CAAC,8BAAa,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,CAAC,EAC7D,CAAC;gCACD,OAAO,IAAI,CAAC;4BACd,CAAC;4BAED,MAAM,OAAO,GAAG,8BAAa,CAAC,MAAM,CAAC;gCACnC,OAAO;gCACP,KAAK;gCACL,IAAI,EAAE,gBAAgB;gCACtB,MAAM,EAAE,MAAM,IAAI,sBAAsB,CAAC,cAAc;gCACvD,MAAM,EAAE,KAAK;6BACd,CAAC,CAAC;4BAEH,OAAO,OAAO;gCACZ,CAAC,CAAC;oCACE,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,sBAAsB,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;oCACxE,GAAG,OAAO;iCACX;gCACH,CAAC,CAAC,IAAI,CAAC;wBACX,CAAC;qBACF,CAAC,CAAC;gBACL,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,MAAM,CAAC,KAAK,CAAC,OAA6D,EAAE;QAC1E,MAAM,EAAE,QAAQ,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAE5C,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;IACpE,CAAC;;AAxLH,wDAyLC","sourcesContent":["import type { Linter, Rule, Scope } from 'eslint';\nimport type { TRuleSeverity } from '../rules/common';\nimport { AstNode, AstNodeTypes, HOUSE_PLUGIN_NAMESPACE } from './common';\nimport { ImportSupport } from './import-support';\n\n/**\n * `venizia/prefer-promise-sleep`. A sleep written by hand, `new Promise((resolve) => setTimeout(resolve, ms))`, is\n * `PromiseUtility.sleep({ ms })` (`@venizia/ignis-helpers/core`). Only that exact shape is reported: a callback that\n * does anything else is not a sleep.\n *\n * The fix replaces the expression and adds the import; `source` is where `PromiseUtility` comes from, `null` turns\n * the fix off.\n */\nexport class PreferPromiseSleepRule {\n static readonly NAME = `${HOUSE_PLUGIN_NAMESPACE}/prefer-promise-sleep`;\n\n static readonly DEFAULT_SOURCE = '@venizia/ignis-helpers/core';\n\n /** The `setTimeout(<resolve>, <delay>)` call a sleep callback consists of, or `undefined`. */\n private static delayOf(opts: { callback: unknown }): Rule.Node | undefined {\n const { callback } = opts;\n const type = AstNode.getType({ node: callback });\n\n if (\n type !== AstNodeTypes.ARROW_FUNCTION_EXPRESSION &&\n type !== AstNodeTypes.FUNCTION_EXPRESSION\n ) {\n return undefined;\n }\n\n const params: unknown = AstNode.readField({ node: callback, key: 'params' });\n const [param] = Array.isArray(params) ? params : [];\n\n if (\n !Array.isArray(params) ||\n params.length !== 1 ||\n AstNode.getType({ node: param }) !== AstNodeTypes.IDENTIFIER\n ) {\n return undefined;\n }\n\n let body: unknown = AstNode.readField({ node: callback, key: 'body' });\n\n if (AstNode.getType({ node: body }) === AstNodeTypes.BLOCK_STATEMENT) {\n const statements: unknown = AstNode.readField({ node: body, key: 'body' });\n const [only] = Array.isArray(statements) ? statements : [];\n\n if (!Array.isArray(statements) || statements.length !== 1) {\n return undefined;\n }\n\n body =\n AstNode.getType({ node: only }) === AstNodeTypes.EXPRESSION_STATEMENT\n ? AstNode.readField({ node: only, key: 'expression' })\n : undefined;\n }\n\n const callee = AstNode.readField({ node: body, key: 'callee' });\n const args: unknown = AstNode.readField({ node: body, key: 'arguments' });\n\n if (\n AstNode.getType({ node: body }) !== AstNodeTypes.CALL_EXPRESSION ||\n AstNode.readField({ node: callee, key: 'name' }) !== 'setTimeout' ||\n !Array.isArray(args) ||\n args.length !== 2\n ) {\n return undefined;\n }\n\n const [resolver, delay] = args;\n const isResolver =\n AstNode.getType({ node: resolver }) === AstNodeTypes.IDENTIFIER &&\n AstNode.readField({ node: resolver, key: 'name' }) ===\n AstNode.readField({ node: param, key: 'name' });\n\n return isResolver && AstNode.isNode(delay) ? delay : undefined;\n }\n\n /** `PromiseUtility.sleep({ ms })`, with the shorthand when the delay is a variable named `ms`. */\n private static call(opts: { context: Rule.RuleContext; delay: Rule.Node }): string {\n const { context, delay } = opts;\n const utility =\n ImportSupport.importedAs({ context, name: 'PromiseUtility', asValue: true }) ??\n 'PromiseUtility';\n const text = context.sourceCode.getText(delay);\n\n return `${utility}.sleep({ ${text === 'ms' ? 'ms' : `ms: ${text}`} })`;\n }\n\n /** Whether `setTimeout` at `node` resolves to a binding of the file (not the global timer). */\n private static isRebound(opts: { context: Rule.RuleContext; node: Rule.Node }): boolean {\n const { context, node } = opts;\n let scope: Scope.Scope | null = context.sourceCode.getScope(node);\n\n while (scope) {\n const variable = scope.set.get('setTimeout');\n\n if (variable && variable.defs.length > 0) {\n return true;\n }\n\n scope = scope.upper;\n }\n\n return false;\n }\n\n static readonly RULE: Rule.RuleModule = {\n meta: {\n type: 'suggestion',\n fixable: 'code',\n schema: [\n {\n type: 'object',\n properties: { source: { type: ['string', 'null'] } },\n additionalProperties: false,\n },\n ],\n messages: {\n sleep:\n 'Use `await PromiseUtility.sleep({ ms })` (from @venizia/ignis-helpers/core), not a hand-written `new Promise(resolve => setTimeout(resolve, ms))`.',\n },\n },\n create: (context) => {\n const options: unknown = context.options[0];\n const configured = AstNode.readField({ node: options, key: 'source' });\n const source =\n configured === null\n ? undefined\n : typeof configured === 'string'\n ? configured\n : PreferPromiseSleepRule.DEFAULT_SOURCE;\n\n return {\n [AstNodeTypes.NEW_EXPRESSION]: (node: Rule.Node) => {\n const callee = AstNode.readField({ node, key: 'callee' });\n const args: unknown = AstNode.readField({ node, key: 'arguments' });\n\n if (\n AstNode.readField({ node: callee, key: 'name' }) !== 'Promise' ||\n !Array.isArray(args) ||\n args.length !== 1\n ) {\n return;\n }\n\n const delay = PreferPromiseSleepRule.delayOf({ callback: args[0] });\n\n if (delay === undefined) {\n return;\n }\n\n // A `setTimeout` the file binds itself (a parameter, a local, an import) is not the timer, and a comma expression is\n // not one delay: neither is a sleep.\n if (\n PreferPromiseSleepRule.isRebound({ context, node }) ||\n AstNode.getType({ node: delay }) === AstNodeTypes.SEQUENCE_EXPRESSION\n ) {\n return;\n }\n\n context.report({\n node,\n messageId: 'sleep',\n fix: (fixer) => {\n if (\n source === undefined &&\n !ImportSupport.isInScope({ context, name: 'PromiseUtility' })\n ) {\n return null;\n }\n\n const imports = ImportSupport.ensure({\n context,\n fixer,\n name: 'PromiseUtility',\n source: source ?? PreferPromiseSleepRule.DEFAULT_SOURCE,\n isType: false,\n });\n\n return imports\n ? [\n fixer.replaceText(node, PreferPromiseSleepRule.call({ context, delay })),\n ...imports,\n ]\n : null;\n },\n });\n },\n };\n },\n };\n\n static build(opts: { severity?: TRuleSeverity; source?: string | null } = {}): Linter.RuleEntry {\n const { severity = 'error', source } = opts;\n\n return source === undefined ? [severity] : [severity, { source }];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"prefer-promise-sleep.rule.js","sourceRoot":"","sources":["../../../src/linters/house/prefer-promise-sleep.rule.ts"],"names":[],"mappings":";;;AAEA,qCAAyE;AACzE,qDAAiD;AAEjD;;;;;;;GAOG;AACH,MAAa,sBAAsB;aACjB,SAAI,GAAG,GAAG,+BAAsB,uBAAuB,CAAC;aAExD,mBAAc,GAAG,6BAA6B,CAAC;IAE/D,8FAA8F;IACtF,MAAM,CAAC,OAAO,CAAC,IAA2B;QAChD,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QAC1B,MAAM,IAAI,GAAG,gBAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;QAEjD,IACE,IAAI,KAAK,qBAAY,CAA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,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;IACpE,CAAC;;AAzLH,wDA0LC","sourcesContent":["import type { Linter, Rule, Scope } from 'eslint';\nimport type { TRuleSeverity } from '../rules/common';\nimport { AstNode, AstNodeTypes, HOUSE_PLUGIN_NAMESPACE } from './common';\nimport { ImportSupport } from './import-support';\n\n/**\n * `@venizia/prefer-promise-sleep`. A sleep written by hand, `new Promise((resolve) => setTimeout(resolve, ms))`, is\n * `PromiseUtility.sleep({ ms })` (`@venizia/ignis-helpers/core`). Only that exact shape is reported: a callback that\n * does anything else is not a sleep.\n *\n * The fix replaces the expression and adds the import; `source` is where `PromiseUtility` comes from, `null` turns\n * the fix off.\n */\nexport class PreferPromiseSleepRule {\n static readonly NAME = `${HOUSE_PLUGIN_NAMESPACE}/prefer-promise-sleep`;\n\n static readonly DEFAULT_SOURCE = '@venizia/ignis-helpers/core';\n\n /** The `setTimeout(<resolve>, <delay>)` call a sleep callback consists of, or `undefined`. */\n private static delayOf(opts: { callback: unknown }): Rule.Node | undefined {\n const { callback } = opts;\n const type = AstNode.getType({ node: callback });\n\n if (\n type !== AstNodeTypes.ARROW_FUNCTION_EXPRESSION &&\n type !== AstNodeTypes.FUNCTION_EXPRESSION\n ) {\n return undefined;\n }\n\n const params: unknown = AstNode.readField({ node: callback, key: 'params' });\n const [param] = Array.isArray(params) ? params : [];\n\n if (\n !Array.isArray(params) ||\n params.length !== 1 ||\n AstNode.getType({ node: param }) !== AstNodeTypes.IDENTIFIER\n ) {\n return undefined;\n }\n\n let body: unknown = AstNode.readField({ node: callback, key: 'body' });\n\n if (AstNode.getType({ node: body }) === AstNodeTypes.BLOCK_STATEMENT) {\n const statements: unknown = AstNode.readField({ node: body, key: 'body' });\n const [only] = Array.isArray(statements) ? statements : [];\n\n if (!Array.isArray(statements) || statements.length !== 1) {\n return undefined;\n }\n\n body =\n AstNode.getType({ node: only }) === AstNodeTypes.EXPRESSION_STATEMENT\n ? AstNode.readField({ node: only, key: 'expression' })\n : undefined;\n }\n\n const callee = AstNode.readField({ node: body, key: 'callee' });\n const args: unknown = AstNode.readField({ node: body, key: 'arguments' });\n\n if (\n AstNode.getType({ node: body }) !== AstNodeTypes.CALL_EXPRESSION ||\n AstNode.readField({ node: callee, key: 'name' }) !== 'setTimeout' ||\n !Array.isArray(args) ||\n args.length !== 2\n ) {\n return undefined;\n }\n\n const [resolver, delay] = args;\n const isResolver =\n AstNode.getType({ node: resolver }) === AstNodeTypes.IDENTIFIER &&\n AstNode.readField({ node: resolver, key: 'name' }) ===\n AstNode.readField({ node: param, key: 'name' });\n\n return isResolver && AstNode.isNode(delay) ? delay : undefined;\n }\n\n /** `PromiseUtility.sleep({ ms })`, with the shorthand when the delay is a variable named `ms`. */\n private static call(opts: { context: Rule.RuleContext; delay: Rule.Node }): string {\n const { context, delay } = opts;\n const utility =\n ImportSupport.importedAs({ context, name: 'PromiseUtility', asValue: true }) ??\n 'PromiseUtility';\n const text = context.sourceCode.getText(delay);\n\n return `${utility}.sleep({ ${text === 'ms' ? 'ms' : `ms: ${text}`} })`;\n }\n\n /** Whether `setTimeout` at `node` resolves to a binding of the file (not the global timer). */\n private static isRebound(opts: { context: Rule.RuleContext; node: Rule.Node }): boolean {\n const { context, node } = opts;\n let scope: Scope.Scope | null = context.sourceCode.getScope(node);\n\n while (scope) {\n const variable = scope.set.get('setTimeout');\n\n if (variable && variable.defs.length > 0) {\n return true;\n }\n\n scope = scope.upper;\n }\n\n return false;\n }\n\n static readonly RULE: Rule.RuleModule = {\n meta: {\n type: 'suggestion',\n fixable: 'code',\n schema: [\n {\n type: 'object',\n properties: { source: { type: ['string', 'null'] } },\n additionalProperties: false,\n },\n ],\n messages: {\n sleep:\n \"Use `await PromiseUtility.sleep({ ms })`, not a hand-written `new Promise(resolve => setTimeout(resolve, ms))`: `import { PromiseUtility } from '{{source}}';`\",\n },\n },\n create: (context) => {\n const options: unknown = context.options[0];\n const configured = AstNode.readField({ node: options, key: 'source' });\n const source =\n configured === null\n ? undefined\n : typeof configured === 'string'\n ? configured\n : PreferPromiseSleepRule.DEFAULT_SOURCE;\n\n return {\n [AstNodeTypes.NEW_EXPRESSION]: (node: Rule.Node) => {\n const callee = AstNode.readField({ node, key: 'callee' });\n const args: unknown = AstNode.readField({ node, key: 'arguments' });\n\n if (\n AstNode.readField({ node: callee, key: 'name' }) !== 'Promise' ||\n !Array.isArray(args) ||\n args.length !== 1\n ) {\n return;\n }\n\n const delay = PreferPromiseSleepRule.delayOf({ callback: args[0] });\n\n if (delay === undefined) {\n return;\n }\n\n // A `setTimeout` the file binds itself (a parameter, a local, an import) is not the timer, and a comma expression is\n // not one delay: neither is a sleep.\n if (\n PreferPromiseSleepRule.isRebound({ context, node }) ||\n AstNode.getType({ node: delay }) === AstNodeTypes.SEQUENCE_EXPRESSION\n ) {\n return;\n }\n\n context.report({\n node,\n messageId: 'sleep',\n data: { source: source ?? PreferPromiseSleepRule.DEFAULT_SOURCE },\n fix: (fixer) => {\n if (\n source === undefined &&\n !ImportSupport.isInScope({ context, name: 'PromiseUtility' })\n ) {\n return null;\n }\n\n const imports = ImportSupport.ensure({\n context,\n fixer,\n name: 'PromiseUtility',\n source: source ?? PreferPromiseSleepRule.DEFAULT_SOURCE,\n isType: false,\n });\n\n return imports\n ? [\n fixer.replaceText(node, PreferPromiseSleepRule.call({ context, delay })),\n ...imports,\n ]\n : null;\n },\n });\n },\n };\n },\n };\n\n static build(opts: { severity?: TRuleSeverity; source?: string | null } = {}): Linter.RuleEntry {\n const { severity = 'error', source } = opts;\n\n return source === undefined ? [severity] : [severity, { source }];\n }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Linter, Rule } from 'eslint';
|
|
2
2
|
import type { TRuleSeverity } from '../rules/common';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* `@venizia/prefer-tnullable`. `X | null | undefined` is `TNullable<X>`, the framework's own name for it
|
|
5
5
|
* (`@venizia/ignis-helpers/common`). A union with only `null` or only `undefined` is not reported: those say
|
|
6
6
|
* something different.
|
|
7
7
|
*
|
|
@@ -9,7 +9,7 @@ import type { TRuleSeverity } from '../rules/common';
|
|
|
9
9
|
* `common` entry); `null` turns the import, and so the fix, off for a project that has no such package.
|
|
10
10
|
*/
|
|
11
11
|
export declare class PreferTNullableRule {
|
|
12
|
-
static readonly NAME = "venizia/prefer-tnullable";
|
|
12
|
+
static readonly NAME = "@venizia/prefer-tnullable";
|
|
13
13
|
static readonly DEFAULT_SOURCE = "@venizia/ignis-helpers/common";
|
|
14
14
|
/**
|
|
15
15
|
* The source of a union member with the parentheses that belong to it: a function, constructor or conditional type is
|
|
@@ -4,7 +4,7 @@ exports.PreferTNullableRule = void 0;
|
|
|
4
4
|
const common_1 = require("./common");
|
|
5
5
|
const import_support_1 = require("./import-support");
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* `@venizia/prefer-tnullable`. `X | null | undefined` is `TNullable<X>`, the framework's own name for it
|
|
8
8
|
* (`@venizia/ignis-helpers/common`). A union with only `null` or only `undefined` is not reported: those say
|
|
9
9
|
* something different.
|
|
10
10
|
*
|
|
@@ -46,7 +46,7 @@ class PreferTNullableRule {
|
|
|
46
46
|
},
|
|
47
47
|
],
|
|
48
48
|
messages: {
|
|
49
|
-
nullable:
|
|
49
|
+
nullable: "Use `TNullable<X>` for `X | null | undefined`: `import type { TNullable } from '{{source}}';`",
|
|
50
50
|
},
|
|
51
51
|
},
|
|
52
52
|
create: (context) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-tnullable.rule.js","sourceRoot":"","sources":["../../../src/linters/house/prefer-tnullable.rule.ts"],"names":[],"mappings":";;;AAEA,qCAAyE;AACzE,qDAAiD;AAEjD;;;;;;;GAOG;AACH,MAAa,mBAAmB;aACd,SAAI,GAAG,GAAG,+BAAsB,mBAAmB,CAAC;aAEpD,mBAAc,GAAG,+BAA+B,CAAC;IA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, HOUSE_PLUGIN_NAMESPACE } from './common';\nimport { ImportSupport } from './import-support';\n\n/**\n * `venizia/prefer-tnullable`. `X | null | undefined` is `TNullable<X>`, the framework's own name for it\n * (`@venizia/ignis-helpers/common`). A union with only `null` or only `undefined` is not reported: those say\n * something different.\n *\n * The fix rewrites the union and adds the import. `source` is where `TNullable` comes from (default the helpers\n * `common` entry); `null` turns the import, and so the fix, off for a project that has no such package.\n */\nexport class PreferTNullableRule {\n static readonly NAME = `${HOUSE_PLUGIN_NAMESPACE}/prefer-tnullable`;\n\n static readonly DEFAULT_SOURCE = '@venizia/ignis-helpers/common';\n\n /**\n * The source of a union member with the parentheses that belong to it: a function, constructor or conditional type is\n * written `(...)` inside a union, and the member text alone (`() => void`) would change the type or not parse.\n */\n private static textOf(opts: { context: Rule.RuleContext; member: Rule.Node }): string {\n const { context, member } = opts;\n const { text } = context.sourceCode;\n const [memberStart, memberEnd] = member.range ?? [0, 0];\n let start = memberStart;\n let end = memberEnd;\n\n for (;;) {\n const before = text.slice(0, start).trimEnd();\n const after = text.slice(end).trimStart();\n\n if (!before.endsWith('(') || !after.startsWith(')')) {\n return text.slice(start, end);\n }\n\n start = before.length - 1;\n end = text.length - after.length + 1;\n }\n }\n\n static readonly RULE: Rule.RuleModule = {\n meta: {\n type: 'suggestion',\n fixable: 'code',\n schema: [\n {\n type: 'object',\n properties: { source: { type: ['string', 'null'] } },\n additionalProperties: false,\n },\n ],\n messages: {\n nullable: 'Use TNullable<X> (from {{source}}) for `X | null | undefined`.',\n },\n },\n create: (context) => {\n const options: unknown = context.options[0];\n const configured = AstNode.readField({ node: options, key: 'source' });\n const source =\n configured === null\n ? undefined\n : typeof configured === 'string'\n ? configured\n : PreferTNullableRule.DEFAULT_SOURCE;\n\n return {\n [AstNodeTypes.UNION_TYPE]: (node: Rule.Node) => {\n const members: unknown = AstNode.readField({ node, key: 'types' });\n\n if (!Array.isArray(members) || members.length < 3) {\n return;\n }\n\n const typeOf = (member: unknown) => AstNode.getType({ node: member });\n const nulls = members.filter((member) => typeOf(member) === AstNodeTypes.NULL_KEYWORD);\n const undefineds = members.filter(\n (member) => typeOf(member) === AstNodeTypes.UNDEFINED_KEYWORD,\n );\n\n if (nulls.length !== 1 || undefineds.length !== 1) {\n return;\n }\n\n // The definition of `TNullable` itself says exactly this.\n const parent: unknown = AstNode.readField({ node, key: 'parent' });\n const parentName = AstNode.readField({\n node: AstNode.readField({ node: parent, key: 'id' }),\n key: 'name',\n });\n\n if (\n AstNode.getType({ node: parent }) === AstNodeTypes.TYPE_ALIAS_DECLARATION &&\n parentName === 'TNullable'\n ) {\n return;\n }\n\n const rest = members.filter(\n (member) =>\n typeOf(member) !== AstNodeTypes.NULL_KEYWORD &&\n typeOf(member) !== AstNodeTypes.UNDEFINED_KEYWORD,\n );\n // A local TNullable is not known to mean what the framework's does, so it is never written toward.\n const local = ImportSupport.importedAs({ context, name: 'TNullable' });\n const isLocal = ImportSupport.isDeclared({ context, name: 'TNullable' });\n // A comment inside the union would be lost by the rewrite: the finding stays, the fix does not.\n const hasComments = context.sourceCode.getCommentsInside(node).length > 0;\n const texts = rest.map((member) => PreferTNullableRule.textOf({ context, member }));\n const ensure = (fixer: Rule.RuleFixer) =>\n ImportSupport.ensure({\n context,\n fixer,\n name: 'TNullable',\n source: source ?? PreferTNullableRule.DEFAULT_SOURCE,\n isType: true,\n });\n\n context.report({\n node,\n messageId: 'nullable',\n data: { source: source ?? PreferTNullableRule.DEFAULT_SOURCE },\n fix: (fixer) => {\n if (hasComments || isLocal || (source === undefined && local === undefined)) {\n return null;\n }\n\n const imports = ensure(fixer);\n\n return imports\n ? [\n fixer.replaceText(node, `${local ?? 'TNullable'}<${texts.join(' | ')}>`),\n ...imports,\n ]\n : null;\n },\n });\n },\n };\n },\n };\n\n static build(opts: { severity?: TRuleSeverity; source?: string | null } = {}): Linter.RuleEntry {\n const { severity = 'error', source } = opts;\n\n return source === undefined ? [severity] : [severity, { source }];\n }\n}\n"]}
|
|
1
|
+
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C,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;IACpE,CAAC;;AAvIH,kDAwIC","sourcesContent":["import type { Linter, Rule } from 'eslint';\nimport type { TRuleSeverity } from '../rules/common';\nimport { AstNode, AstNodeTypes, HOUSE_PLUGIN_NAMESPACE } from './common';\nimport { ImportSupport } from './import-support';\n\n/**\n * `@venizia/prefer-tnullable`. `X | null | undefined` is `TNullable<X>`, the framework's own name for it\n * (`@venizia/ignis-helpers/common`). A union with only `null` or only `undefined` is not reported: those say\n * something different.\n *\n * The fix rewrites the union and adds the import. `source` is where `TNullable` comes from (default the helpers\n * `common` entry); `null` turns the import, and so the fix, off for a project that has no such package.\n */\nexport class PreferTNullableRule {\n static readonly NAME = `${HOUSE_PLUGIN_NAMESPACE}/prefer-tnullable`;\n\n static readonly DEFAULT_SOURCE = '@venizia/ignis-helpers/common';\n\n /**\n * The source of a union member with the parentheses that belong to it: a function, constructor or conditional type is\n * written `(...)` inside a union, and the member text alone (`() => void`) would change the type or not parse.\n */\n private static textOf(opts: { context: Rule.RuleContext; member: Rule.Node }): string {\n const { context, member } = opts;\n const { text } = context.sourceCode;\n const [memberStart, memberEnd] = member.range ?? [0, 0];\n let start = memberStart;\n let end = memberEnd;\n\n for (;;) {\n const before = text.slice(0, start).trimEnd();\n const after = text.slice(end).trimStart();\n\n if (!before.endsWith('(') || !after.startsWith(')')) {\n return text.slice(start, end);\n }\n\n start = before.length - 1;\n end = text.length - after.length + 1;\n }\n }\n\n static readonly RULE: Rule.RuleModule = {\n meta: {\n type: 'suggestion',\n fixable: 'code',\n schema: [\n {\n type: 'object',\n properties: { source: { type: ['string', 'null'] } },\n additionalProperties: false,\n },\n ],\n messages: {\n nullable:\n \"Use `TNullable<X>` for `X | null | undefined`: `import type { TNullable } from '{{source}}';`\",\n },\n },\n create: (context) => {\n const options: unknown = context.options[0];\n const configured = AstNode.readField({ node: options, key: 'source' });\n const source =\n configured === null\n ? undefined\n : typeof configured === 'string'\n ? configured\n : PreferTNullableRule.DEFAULT_SOURCE;\n\n return {\n [AstNodeTypes.UNION_TYPE]: (node: Rule.Node) => {\n const members: unknown = AstNode.readField({ node, key: 'types' });\n\n if (!Array.isArray(members) || members.length < 3) {\n return;\n }\n\n const typeOf = (member: unknown) => AstNode.getType({ node: member });\n const nulls = members.filter((member) => typeOf(member) === AstNodeTypes.NULL_KEYWORD);\n const undefineds = members.filter(\n (member) => typeOf(member) === AstNodeTypes.UNDEFINED_KEYWORD,\n );\n\n if (nulls.length !== 1 || undefineds.length !== 1) {\n return;\n }\n\n // The definition of `TNullable` itself says exactly this.\n const parent: unknown = AstNode.readField({ node, key: 'parent' });\n const parentName = AstNode.readField({\n node: AstNode.readField({ node: parent, key: 'id' }),\n key: 'name',\n });\n\n if (\n AstNode.getType({ node: parent }) === AstNodeTypes.TYPE_ALIAS_DECLARATION &&\n parentName === 'TNullable'\n ) {\n return;\n }\n\n const rest = members.filter(\n (member) =>\n typeOf(member) !== AstNodeTypes.NULL_KEYWORD &&\n typeOf(member) !== AstNodeTypes.UNDEFINED_KEYWORD,\n );\n // A local TNullable is not known to mean what the framework's does, so it is never written toward.\n const local = ImportSupport.importedAs({ context, name: 'TNullable' });\n const isLocal = ImportSupport.isDeclared({ context, name: 'TNullable' });\n // A comment inside the union would be lost by the rewrite: the finding stays, the fix does not.\n const hasComments = context.sourceCode.getCommentsInside(node).length > 0;\n const texts = rest.map((member) => PreferTNullableRule.textOf({ context, member }));\n const ensure = (fixer: Rule.RuleFixer) =>\n ImportSupport.ensure({\n context,\n fixer,\n name: 'TNullable',\n source: source ?? PreferTNullableRule.DEFAULT_SOURCE,\n isType: true,\n });\n\n context.report({\n node,\n messageId: 'nullable',\n data: { source: source ?? PreferTNullableRule.DEFAULT_SOURCE },\n fix: (fixer) => {\n if (hasComments || isLocal || (source === undefined && local === undefined)) {\n return null;\n }\n\n const imports = ensure(fixer);\n\n return imports\n ? [\n fixer.replaceText(node, `${local ?? 'TNullable'}<${texts.join(' | ')}>`),\n ...imports,\n ]\n : null;\n },\n });\n },\n };\n },\n };\n\n static build(opts: { severity?: TRuleSeverity; source?: string | null } = {}): Linter.RuleEntry {\n const { severity = 'error', source } = opts;\n\n return source === undefined ? [severity] : [severity, { source }];\n }\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Linter, Rule } from 'eslint';
|
|
2
|
+
import type { TRuleSeverity } from '../rules/common';
|
|
3
|
+
/**
|
|
4
|
+
* `@venizia/typed-route-context`: reports what compiles and still leaves a route handler unchecked. It reads syntax
|
|
5
|
+
* only, so a context passed on to another file, stored, or passed further than one method is not followed.
|
|
6
|
+
*
|
|
7
|
+
* - a route config object declared as `IAuthRouteConfig` (or in a `Record` of it) instead of `satisfies`;
|
|
8
|
+
* - `valid<T>()` with a type argument on a context typed from its route;
|
|
9
|
+
* - a decorated method whose `TContextOf` names another route than its decorator;
|
|
10
|
+
* - a handler that passes its context straight to a method of the same class typed for another route.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TypedRouteContextRule {
|
|
13
|
+
static readonly NAME = "@venizia/typed-route-context";
|
|
14
|
+
static readonly RULE: Rule.RuleModule;
|
|
15
|
+
static build(opts?: {
|
|
16
|
+
severity?: TRuleSeverity;
|
|
17
|
+
}): Linter.RuleEntry;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=typed-route-context.rule.d.ts.map
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedRouteContextRule = void 0;
|
|
4
|
+
const common_1 = require("./common");
|
|
5
|
+
/** The members of a route's AST this rule reads. */
|
|
6
|
+
class RouteNodes {
|
|
7
|
+
static { this.CONTEXT_TYPE = 'TContextOf'; }
|
|
8
|
+
static { this.CONFIG_TYPE = 'IAuthRouteConfig'; }
|
|
9
|
+
static { this.DECORATORS = new Set([
|
|
10
|
+
'api',
|
|
11
|
+
'get',
|
|
12
|
+
'post',
|
|
13
|
+
'put',
|
|
14
|
+
'patch',
|
|
15
|
+
'del',
|
|
16
|
+
]); }
|
|
17
|
+
static field(node, key) {
|
|
18
|
+
return common_1.AstNode.readField({ node, key });
|
|
19
|
+
}
|
|
20
|
+
static type(node) {
|
|
21
|
+
return common_1.AstNode.getType({ node });
|
|
22
|
+
}
|
|
23
|
+
static list(node, key) {
|
|
24
|
+
const value = RouteNodes.field(node, key);
|
|
25
|
+
return Array.isArray(value) ? value : [];
|
|
26
|
+
}
|
|
27
|
+
static nameOf(node) {
|
|
28
|
+
const value = RouteNodes.field(node, 'name');
|
|
29
|
+
return typeof value === 'string' ? value : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** The type arguments written on a type reference or a call. */
|
|
32
|
+
static typeArguments(node) {
|
|
33
|
+
return RouteNodes.list(RouteNodes.field(node, 'typeArguments') ?? RouteNodes.field(node, 'typeParameters'), 'params');
|
|
34
|
+
}
|
|
35
|
+
/** `a.b.c` of an expression, a qualified type name or an indexed access type; `undefined` for anything computed. */
|
|
36
|
+
static chain(node) {
|
|
37
|
+
const type = RouteNodes.type(node);
|
|
38
|
+
if (type === common_1.AstNodeTypes.IDENTIFIER) {
|
|
39
|
+
return RouteNodes.nameOf(node);
|
|
40
|
+
}
|
|
41
|
+
if (type === common_1.AstNodeTypes.THIS_EXPRESSION) {
|
|
42
|
+
return 'this';
|
|
43
|
+
}
|
|
44
|
+
const join = (left, right) => {
|
|
45
|
+
const head = RouteNodes.chain(left);
|
|
46
|
+
return head === undefined || right === undefined ? undefined : `${head}.${right}`;
|
|
47
|
+
};
|
|
48
|
+
const literal = (value) => (typeof value === 'string' ? value : undefined);
|
|
49
|
+
if (type === common_1.AstNodeTypes.MEMBER_EXPRESSION) {
|
|
50
|
+
const property = RouteNodes.field(node, 'property');
|
|
51
|
+
const isComputed = RouteNodes.field(node, 'computed') === true;
|
|
52
|
+
return join(RouteNodes.field(node, 'object'), isComputed ? literal(RouteNodes.field(property, 'value')) : RouteNodes.nameOf(property));
|
|
53
|
+
}
|
|
54
|
+
if (type === 'TSQualifiedName') {
|
|
55
|
+
return join(RouteNodes.field(node, 'left'), RouteNodes.nameOf(RouteNodes.field(node, 'right')));
|
|
56
|
+
}
|
|
57
|
+
if (type === common_1.AstNodeTypes.TYPE_QUERY) {
|
|
58
|
+
return RouteNodes.chain(RouteNodes.field(node, 'exprName'));
|
|
59
|
+
}
|
|
60
|
+
if (type === 'TSIndexedAccessType') {
|
|
61
|
+
const index = RouteNodes.field(RouteNodes.field(node, 'indexType'), 'literal');
|
|
62
|
+
return join(RouteNodes.field(node, 'objectType'), literal(RouteNodes.field(index, 'value')));
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/** The value of the property `key` of an object expression. */
|
|
67
|
+
static property(node, key) {
|
|
68
|
+
const found = RouteNodes.list(node, 'properties').find((item) => RouteNodes.nameOf(RouteNodes.field(item, 'key')) === key);
|
|
69
|
+
return RouteNodes.field(found, 'value');
|
|
70
|
+
}
|
|
71
|
+
/** The type a pattern is annotated with. */
|
|
72
|
+
static annotation(pattern) {
|
|
73
|
+
return RouteNodes.field(RouteNodes.field(pattern, 'typeAnnotation'), 'typeAnnotation');
|
|
74
|
+
}
|
|
75
|
+
static referenceName(node) {
|
|
76
|
+
return RouteNodes.type(node) === common_1.AstNodeTypes.TYPE_REFERENCE
|
|
77
|
+
? RouteNodes.nameOf(RouteNodes.field(node, 'typeName'))
|
|
78
|
+
: undefined;
|
|
79
|
+
}
|
|
80
|
+
/** The `TContextOf<...>` a parameter carries, directly or as the `context` of its options, and the path it binds. */
|
|
81
|
+
static contextOf(param) {
|
|
82
|
+
const annotation = RouteNodes.annotation(param);
|
|
83
|
+
const read = (type, path) => {
|
|
84
|
+
if (path === undefined || RouteNodes.referenceName(type) !== RouteNodes.CONTEXT_TYPE) {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return { path, config: RouteNodes.chain(RouteNodes.typeArguments(type)[0]) };
|
|
88
|
+
};
|
|
89
|
+
if (RouteNodes.type(annotation) !== 'TSTypeLiteral') {
|
|
90
|
+
return read(annotation, RouteNodes.nameOf(param));
|
|
91
|
+
}
|
|
92
|
+
const member = RouteNodes.list(annotation, 'members').find((item) => RouteNodes.nameOf(RouteNodes.field(item, 'key')) === 'context');
|
|
93
|
+
const bound = RouteNodes.property(param, 'context');
|
|
94
|
+
const holder = RouteNodes.nameOf(param);
|
|
95
|
+
return read(RouteNodes.annotation(member), holder === undefined ? RouteNodes.nameOf(bound) : `${holder}.context`);
|
|
96
|
+
}
|
|
97
|
+
/** The route config of a `defineRoute`, `defineJSXRoute` or `bindRoute().to` call whose options hold the handler. */
|
|
98
|
+
static routeOf(call) {
|
|
99
|
+
const callee = RouteNodes.field(call, 'callee');
|
|
100
|
+
const method = RouteNodes.nameOf(RouteNodes.field(callee, 'property'));
|
|
101
|
+
const [first] = RouteNodes.list(call, 'arguments');
|
|
102
|
+
if (method === 'defineRoute' || method === 'defineJSXRoute') {
|
|
103
|
+
return { config: RouteNodes.chain(RouteNodes.property(first, 'configs')) };
|
|
104
|
+
}
|
|
105
|
+
const bound = RouteNodes.field(callee, 'object');
|
|
106
|
+
const binder = RouteNodes.nameOf(RouteNodes.field(RouteNodes.field(bound, 'callee'), 'property'));
|
|
107
|
+
if (method !== 'to' || binder !== 'bindRoute') {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const [options] = RouteNodes.list(bound, 'arguments');
|
|
111
|
+
return { config: RouteNodes.chain(RouteNodes.property(options, 'configs')) };
|
|
112
|
+
}
|
|
113
|
+
/** The `this.<method>` a handler body passes its context straight to. */
|
|
114
|
+
static forwardOf(handler, context) {
|
|
115
|
+
let body = RouteNodes.field(handler, 'body');
|
|
116
|
+
if (RouteNodes.type(body) === common_1.AstNodeTypes.BLOCK_STATEMENT) {
|
|
117
|
+
const statements = RouteNodes.list(body, 'body');
|
|
118
|
+
body = statements.length === 1 ? RouteNodes.field(statements[0], 'argument') : undefined;
|
|
119
|
+
}
|
|
120
|
+
if (RouteNodes.type(body) === common_1.AstNodeTypes.AWAIT_EXPRESSION) {
|
|
121
|
+
body = RouteNodes.field(body, 'argument');
|
|
122
|
+
}
|
|
123
|
+
const callee = RouteNodes.field(body, 'callee');
|
|
124
|
+
if (RouteNodes.type(RouteNodes.field(callee, 'object')) !== common_1.AstNodeTypes.THIS_EXPRESSION) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const passes = RouteNodes.list(body, 'arguments').some((argument) => RouteNodes.nameOf(argument) === context ||
|
|
128
|
+
RouteNodes.nameOf(RouteNodes.property(argument, 'context')) === context);
|
|
129
|
+
return passes ? RouteNodes.nameOf(RouteNodes.field(callee, 'property')) : undefined;
|
|
130
|
+
}
|
|
131
|
+
/** Whether a declared type is `IAuthRouteConfig`, alone, in an `Omit` or as the value of a `Record`. */
|
|
132
|
+
static isWideConfig(type) {
|
|
133
|
+
const name = RouteNodes.referenceName(type);
|
|
134
|
+
const [first, second] = RouteNodes.typeArguments(type);
|
|
135
|
+
if (name === 'Record') {
|
|
136
|
+
return RouteNodes.isWideConfig(second);
|
|
137
|
+
}
|
|
138
|
+
return name === 'Omit' ? RouteNodes.isWideConfig(first) : name === RouteNodes.CONFIG_TYPE;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* `@venizia/typed-route-context`: reports what compiles and still leaves a route handler unchecked. It reads syntax
|
|
143
|
+
* only, so a context passed on to another file, stored, or passed further than one method is not followed.
|
|
144
|
+
*
|
|
145
|
+
* - a route config object declared as `IAuthRouteConfig` (or in a `Record` of it) instead of `satisfies`;
|
|
146
|
+
* - `valid<T>()` with a type argument on a context typed from its route;
|
|
147
|
+
* - a decorated method whose `TContextOf` names another route than its decorator;
|
|
148
|
+
* - a handler that passes its context straight to a method of the same class typed for another route.
|
|
149
|
+
*/
|
|
150
|
+
class TypedRouteContextRule {
|
|
151
|
+
static { this.NAME = `${common_1.HOUSE_PLUGIN_NAMESPACE}/typed-route-context`; }
|
|
152
|
+
static { this.RULE = {
|
|
153
|
+
meta: {
|
|
154
|
+
type: 'problem',
|
|
155
|
+
schema: [],
|
|
156
|
+
messages: {
|
|
157
|
+
wideConfig: 'Declared as `IAuthRouteConfig`, this config loses its schemas: write `{ ... } satisfies IAuthRouteConfig`.',
|
|
158
|
+
typedValid: 'This context is typed from its route: remove the type argument of `valid()`, which replaces the schema type.',
|
|
159
|
+
otherRoute: 'This route is `{{expected}}`, and the context is typed from `{{actual}}`.',
|
|
160
|
+
forwarded: 'The route `{{expected}}` passes its context to `{{method}}`, which is typed from `{{actual}}`.',
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
create: (context) => {
|
|
164
|
+
const frames = [];
|
|
165
|
+
const classes = [];
|
|
166
|
+
const isTyped = (path) => {
|
|
167
|
+
const [root] = path.split('.');
|
|
168
|
+
const owner = frames.findLast((frame) => frame.declared.has(root ?? ''));
|
|
169
|
+
return owner?.typed.has(path) === true;
|
|
170
|
+
};
|
|
171
|
+
const enterFunction = (node) => {
|
|
172
|
+
const frame = { declared: new Set(), typed: new Set() };
|
|
173
|
+
const params = RouteNodes.list(node, 'params');
|
|
174
|
+
for (const param of params) {
|
|
175
|
+
const own = RouteNodes.nameOf(param);
|
|
176
|
+
if (own !== undefined) {
|
|
177
|
+
frame.declared.add(own);
|
|
178
|
+
}
|
|
179
|
+
for (const property of RouteNodes.list(param, 'properties')) {
|
|
180
|
+
const bound = RouteNodes.nameOf(RouteNodes.field(property, 'value'));
|
|
181
|
+
if (bound !== undefined) {
|
|
182
|
+
frame.declared.add(bound);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const typed = RouteNodes.contextOf(param);
|
|
186
|
+
if (typed !== undefined) {
|
|
187
|
+
frame.typed.add(typed.path);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
frames.push(frame);
|
|
191
|
+
const property = RouteNodes.field(node, 'parent');
|
|
192
|
+
const call = RouteNodes.field(RouteNodes.field(property, 'parent'), 'parent');
|
|
193
|
+
const isHandler = RouteNodes.nameOf(RouteNodes.field(property, 'key')) === 'handler';
|
|
194
|
+
const route = isHandler ? RouteNodes.routeOf(call) : undefined;
|
|
195
|
+
const [first] = params;
|
|
196
|
+
const name = RouteNodes.nameOf(first);
|
|
197
|
+
if (route === undefined || name === undefined || RouteNodes.annotation(first)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
frame.typed.add(name);
|
|
201
|
+
const method = RouteNodes.forwardOf(node, name);
|
|
202
|
+
if (method !== undefined && route.config !== undefined) {
|
|
203
|
+
classes.at(-1)?.forwards.push({ config: route.config, method, node });
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
const leaveFunction = () => {
|
|
207
|
+
frames.pop();
|
|
208
|
+
};
|
|
209
|
+
const checkDeclared = (opts) => {
|
|
210
|
+
// `{ ... } as const` under a wide annotation loses the schemas the same way.
|
|
211
|
+
const value = RouteNodes.type(opts.value) === common_1.AstNodeTypes.AS_EXPRESSION
|
|
212
|
+
? RouteNodes.field(opts.value, 'expression')
|
|
213
|
+
: opts.value;
|
|
214
|
+
// An empty object is a collection filled later, with no literal schema to keep.
|
|
215
|
+
const isObject = RouteNodes.type(value) === common_1.AstNodeTypes.OBJECT_EXPRESSION &&
|
|
216
|
+
RouteNodes.list(value, 'properties').length > 0;
|
|
217
|
+
if (isObject && RouteNodes.isWideConfig(RouteNodes.annotation(opts.pattern))) {
|
|
218
|
+
context.report({ node: opts.node, messageId: 'wideConfig' });
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
return {
|
|
222
|
+
[common_1.AstNodeTypes.FUNCTION_DECLARATION]: enterFunction,
|
|
223
|
+
[common_1.AstNodeTypes.FUNCTION_EXPRESSION]: enterFunction,
|
|
224
|
+
[common_1.AstNodeTypes.ARROW_FUNCTION_EXPRESSION]: enterFunction,
|
|
225
|
+
[`${common_1.AstNodeTypes.FUNCTION_DECLARATION}:exit`]: leaveFunction,
|
|
226
|
+
[`${common_1.AstNodeTypes.FUNCTION_EXPRESSION}:exit`]: leaveFunction,
|
|
227
|
+
[`${common_1.AstNodeTypes.ARROW_FUNCTION_EXPRESSION}:exit`]: leaveFunction,
|
|
228
|
+
[common_1.AstNodeTypes.CLASS_BODY]: () => {
|
|
229
|
+
classes.push({ typed: new Map(), forwards: [] });
|
|
230
|
+
},
|
|
231
|
+
[`${common_1.AstNodeTypes.CLASS_BODY}:exit`]: () => {
|
|
232
|
+
const facts = classes.pop();
|
|
233
|
+
for (const { config, method, node } of facts?.forwards ?? []) {
|
|
234
|
+
const actual = facts?.typed.get(method);
|
|
235
|
+
if (actual !== undefined && actual !== config) {
|
|
236
|
+
context.report({
|
|
237
|
+
node,
|
|
238
|
+
messageId: 'forwarded',
|
|
239
|
+
data: { expected: config, method, actual },
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
[common_1.AstNodeTypes.METHOD_DEFINITION]: (node) => {
|
|
245
|
+
const method = RouteNodes.nameOf(RouteNodes.field(node, 'key'));
|
|
246
|
+
const typed = RouteNodes.list(RouteNodes.field(node, 'value'), 'params')
|
|
247
|
+
.map((param) => RouteNodes.contextOf(param))
|
|
248
|
+
.find((found) => found !== undefined);
|
|
249
|
+
const actual = typed?.config;
|
|
250
|
+
if (method === undefined || actual === undefined) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
classes.at(-1)?.typed.set(method, actual);
|
|
254
|
+
for (const decorator of RouteNodes.list(node, 'decorators')) {
|
|
255
|
+
const call = RouteNodes.field(decorator, 'expression');
|
|
256
|
+
const name = RouteNodes.nameOf(RouteNodes.field(call, 'callee'));
|
|
257
|
+
const [options] = RouteNodes.list(call, 'arguments');
|
|
258
|
+
const expected = RouteNodes.chain(RouteNodes.property(options, 'configs'));
|
|
259
|
+
if (name !== undefined &&
|
|
260
|
+
RouteNodes.DECORATORS.has(name) &&
|
|
261
|
+
expected !== undefined &&
|
|
262
|
+
expected !== actual) {
|
|
263
|
+
context.report({ node, messageId: 'otherRoute', data: { expected, actual } });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
[common_1.AstNodeTypes.VARIABLE_DECLARATOR]: (node) => {
|
|
268
|
+
const pattern = RouteNodes.field(node, 'id');
|
|
269
|
+
const value = RouteNodes.field(node, 'init');
|
|
270
|
+
checkDeclared({ node, pattern, value });
|
|
271
|
+
const frame = frames.at(-1);
|
|
272
|
+
if (!frame) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const own = RouteNodes.nameOf(pattern);
|
|
276
|
+
if (own !== undefined) {
|
|
277
|
+
frame.declared.add(own);
|
|
278
|
+
}
|
|
279
|
+
const source = RouteNodes.chain(value);
|
|
280
|
+
for (const property of RouteNodes.list(pattern, 'properties')) {
|
|
281
|
+
const key = RouteNodes.nameOf(RouteNodes.field(property, 'key'));
|
|
282
|
+
const bound = RouteNodes.nameOf(RouteNodes.field(property, 'value'));
|
|
283
|
+
if (bound === undefined) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const holdsContext = source !== undefined && isTyped(`${source}.${key}`);
|
|
287
|
+
frame.declared.add(bound);
|
|
288
|
+
if (holdsContext) {
|
|
289
|
+
frame.typed.add(bound);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
[common_1.AstNodeTypes.PROPERTY_DEFINITION]: (node) => {
|
|
294
|
+
checkDeclared({
|
|
295
|
+
node,
|
|
296
|
+
pattern: node,
|
|
297
|
+
value: RouteNodes.field(node, 'value'),
|
|
298
|
+
});
|
|
299
|
+
},
|
|
300
|
+
[common_1.AstNodeTypes.CALL_EXPRESSION]: (node) => {
|
|
301
|
+
const callee = RouteNodes.field(node, 'callee');
|
|
302
|
+
const request = RouteNodes.field(callee, 'object');
|
|
303
|
+
if (RouteNodes.nameOf(RouteNodes.field(callee, 'property')) !== 'valid' ||
|
|
304
|
+
RouteNodes.nameOf(RouteNodes.field(request, 'property')) !== 'req' ||
|
|
305
|
+
RouteNodes.typeArguments(node).length === 0) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const holder = RouteNodes.chain(RouteNodes.field(request, 'object'));
|
|
309
|
+
if (holder !== undefined && isTyped(holder)) {
|
|
310
|
+
context.report({ node, messageId: 'typedValid' });
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
},
|
|
315
|
+
}; }
|
|
316
|
+
static build(opts = {}) {
|
|
317
|
+
const { severity = 'error' } = opts;
|
|
318
|
+
return [severity];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
exports.TypedRouteContextRule = TypedRouteContextRule;
|
|
322
|
+
//# sourceMappingURL=typed-route-context.rule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-route-context.rule.js","sourceRoot":"","sources":["../../../src/linters/house/typed-route-context.rule.ts"],"names":[],"mappings":";;;AAEA,qCAAyE;AAqBzE,oDAAoD;AACpD,MAAM,UAAU;aACE,iBAAY,GAAG,YAAY,CAAC;aAC5B,gBAAW,GAAG,kBAAkB,CAAC;aACjC,eAAU,GAAwB,IAAI,GAAG,CAAC;QACxD,KAAK;QACL,KAAK;QACL,MAAM;QACN,KAAK;QACL,OAAO;QACP,KAAK;KACN,CAAC,CAAC;IAEH,MAAM,CAAC,KAAK,CAAC,IAAa,EAAE,GAAW;QACrC,OAAO,gBAAO,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,IAAa;QACvB,OAAO,gBAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,CAAC,I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AC,KAAK,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC,KAAK,KAAK;wBAClE,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAC3C,CAAC;wBACD,OAAO;oBACT,CAAC;oBAED,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;oBAErE,IAAI,MAAM,KAAK,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;wBAC5C,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC,CAAC;oBACpD,CAAC;gBACH,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,MAAM,CAAC,KAAK,CAAC,OAAqC,EAAE;QAClD,MAAM,EAAE,QAAQ,GAAG,OAAO,EAAE,GAAG,IAAI,CAAC;QAEpC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACpB,CAAC;;AAzNH,sDA0NC","sourcesContent":["import type { Linter, Rule } from 'eslint';\nimport type { TRuleSeverity } from '../rules/common';\nimport { AstNode, AstNodeTypes, HOUSE_PLUGIN_NAMESPACE } from './common';\n\n/** A route a handler is written for, and the private method it hands its context to. */\ninterface IForward {\n config: string;\n method: string;\n node: Rule.Node;\n}\n\n/** What one class declares: the route each method's context is typed from, and the handlers that forward to a method. */\ninterface IClassFacts {\n typed: Map<string, string>;\n forwards: IForward[];\n}\n\n/** The bindings of one function: the root names it declares, and the paths among them that hold a typed context. */\ninterface IFrame {\n declared: Set<string>;\n typed: Set<string>;\n}\n\n/** The members of a route's AST this rule reads. */\nclass RouteNodes {\n static readonly CONTEXT_TYPE = 'TContextOf';\n static readonly CONFIG_TYPE = 'IAuthRouteConfig';\n static readonly DECORATORS: ReadonlySet<string> = new Set([\n 'api',\n 'get',\n 'post',\n 'put',\n 'patch',\n 'del',\n ]);\n\n static field(node: unknown, key: string): unknown {\n return AstNode.readField({ node, key });\n }\n\n static type(node: unknown): string | undefined {\n return AstNode.getType({ node });\n }\n\n static list(node: unknown, key: string): unknown[] {\n const value = RouteNodes.field(node, key);\n\n return Array.isArray(value) ? value : [];\n }\n\n static nameOf(node: unknown): string | undefined {\n const value = RouteNodes.field(node, 'name');\n\n return typeof value === 'string' ? value : undefined;\n }\n\n /** The type arguments written on a type reference or a call. */\n static typeArguments(node: unknown): unknown[] {\n return RouteNodes.list(\n RouteNodes.field(node, 'typeArguments') ?? RouteNodes.field(node, 'typeParameters'),\n 'params',\n );\n }\n\n /** `a.b.c` of an expression, a qualified type name or an indexed access type; `undefined` for anything computed. */\n static chain(node: unknown): string | undefined {\n const type = RouteNodes.type(node);\n\n if (type === AstNodeTypes.IDENTIFIER) {\n return RouteNodes.nameOf(node);\n }\n\n if (type === AstNodeTypes.THIS_EXPRESSION) {\n return 'this';\n }\n\n const join = (left: unknown, right: string | undefined) => {\n const head = RouteNodes.chain(left);\n\n return head === undefined || right === undefined ? undefined : `${head}.${right}`;\n };\n\n const literal = (value: unknown) => (typeof value === 'string' ? value : undefined);\n\n if (type === AstNodeTypes.MEMBER_EXPRESSION) {\n const property = RouteNodes.field(node, 'property');\n const isComputed = RouteNodes.field(node, 'computed') === true;\n\n return join(\n RouteNodes.field(node, 'object'),\n isComputed ? literal(RouteNodes.field(property, 'value')) : RouteNodes.nameOf(property),\n );\n }\n\n if (type === 'TSQualifiedName') {\n return join(\n RouteNodes.field(node, 'left'),\n RouteNodes.nameOf(RouteNodes.field(node, 'right')),\n );\n }\n\n if (type === AstNodeTypes.TYPE_QUERY) {\n return RouteNodes.chain(RouteNodes.field(node, 'exprName'));\n }\n\n if (type === 'TSIndexedAccessType') {\n const index = RouteNodes.field(RouteNodes.field(node, 'indexType'), 'literal');\n\n return join(RouteNodes.field(node, 'objectType'), literal(RouteNodes.field(index, 'value')));\n }\n\n return undefined;\n }\n\n /** The value of the property `key` of an object expression. */\n static property(node: unknown, key: string): unknown {\n const found = RouteNodes.list(node, 'properties').find(\n (item) => RouteNodes.nameOf(RouteNodes.field(item, 'key')) === key,\n );\n\n return RouteNodes.field(found, 'value');\n }\n\n /** The type a pattern is annotated with. */\n static annotation(pattern: unknown): unknown {\n return RouteNodes.field(RouteNodes.field(pattern, 'typeAnnotation'), 'typeAnnotation');\n }\n\n static referenceName(node: unknown): string | undefined {\n return RouteNodes.type(node) === AstNodeTypes.TYPE_REFERENCE\n ? RouteNodes.nameOf(RouteNodes.field(node, 'typeName'))\n : undefined;\n }\n\n /** The `TContextOf<...>` a parameter carries, directly or as the `context` of its options, and the path it binds. */\n static contextOf(param: unknown): { path: string; config: string | undefined } | undefined {\n const annotation = RouteNodes.annotation(param);\n\n const read = (type: unknown, path: string | undefined) => {\n if (path === undefined || RouteNodes.referenceName(type) !== RouteNodes.CONTEXT_TYPE) {\n return undefined;\n }\n\n return { path, config: RouteNodes.chain(RouteNodes.typeArguments(type)[0]) };\n };\n\n if (RouteNodes.type(annotation) !== 'TSTypeLiteral') {\n return read(annotation, RouteNodes.nameOf(param));\n }\n\n const member = RouteNodes.list(annotation, 'members').find(\n (item) => RouteNodes.nameOf(RouteNodes.field(item, 'key')) === 'context',\n );\n const bound = RouteNodes.property(param, 'context');\n const holder = RouteNodes.nameOf(param);\n\n return read(\n RouteNodes.annotation(member),\n holder === undefined ? RouteNodes.nameOf(bound) : `${holder}.context`,\n );\n }\n\n /** The route config of a `defineRoute`, `defineJSXRoute` or `bindRoute().to` call whose options hold the handler. */\n static routeOf(call: unknown): { config: string | undefined } | undefined {\n const callee = RouteNodes.field(call, 'callee');\n const method = RouteNodes.nameOf(RouteNodes.field(callee, 'property'));\n const [first] = RouteNodes.list(call, 'arguments');\n\n if (method === 'defineRoute' || method === 'defineJSXRoute') {\n return { config: RouteNodes.chain(RouteNodes.property(first, 'configs')) };\n }\n\n const bound = RouteNodes.field(callee, 'object');\n const binder = RouteNodes.nameOf(\n RouteNodes.field(RouteNodes.field(bound, 'callee'), 'property'),\n );\n\n if (method !== 'to' || binder !== 'bindRoute') {\n return undefined;\n }\n\n const [options] = RouteNodes.list(bound, 'arguments');\n\n return { config: RouteNodes.chain(RouteNodes.property(options, 'configs')) };\n }\n\n /** The `this.<method>` a handler body passes its context straight to. */\n static forwardOf(handler: unknown, context: string): string | undefined {\n let body = RouteNodes.field(handler, 'body');\n\n if (RouteNodes.type(body) === AstNodeTypes.BLOCK_STATEMENT) {\n const statements = RouteNodes.list(body, 'body');\n body = statements.length === 1 ? RouteNodes.field(statements[0], 'argument') : undefined;\n }\n\n if (RouteNodes.type(body) === AstNodeTypes.AWAIT_EXPRESSION) {\n body = RouteNodes.field(body, 'argument');\n }\n\n const callee = RouteNodes.field(body, 'callee');\n\n if (RouteNodes.type(RouteNodes.field(callee, 'object')) !== AstNodeTypes.THIS_EXPRESSION) {\n return undefined;\n }\n\n const passes = RouteNodes.list(body, 'arguments').some(\n (argument) =>\n RouteNodes.nameOf(argument) === context ||\n RouteNodes.nameOf(RouteNodes.property(argument, 'context')) === context,\n );\n\n return passes ? RouteNodes.nameOf(RouteNodes.field(callee, 'property')) : undefined;\n }\n\n /** Whether a declared type is `IAuthRouteConfig`, alone, in an `Omit` or as the value of a `Record`. */\n static isWideConfig(type: unknown): boolean {\n const name = RouteNodes.referenceName(type);\n const [first, second] = RouteNodes.typeArguments(type);\n\n if (name === 'Record') {\n return RouteNodes.isWideConfig(second);\n }\n\n return name === 'Omit' ? RouteNodes.isWideConfig(first) : name === RouteNodes.CONFIG_TYPE;\n }\n}\n\n/**\n * `@venizia/typed-route-context`: reports what compiles and still leaves a route handler unchecked. It reads syntax\n * only, so a context passed on to another file, stored, or passed further than one method is not followed.\n *\n * - a route config object declared as `IAuthRouteConfig` (or in a `Record` of it) instead of `satisfies`;\n * - `valid<T>()` with a type argument on a context typed from its route;\n * - a decorated method whose `TContextOf` names another route than its decorator;\n * - a handler that passes its context straight to a method of the same class typed for another route.\n */\nexport class TypedRouteContextRule {\n static readonly NAME = `${HOUSE_PLUGIN_NAMESPACE}/typed-route-context`;\n\n static readonly RULE: Rule.RuleModule = {\n meta: {\n type: 'problem',\n schema: [],\n messages: {\n wideConfig:\n 'Declared as `IAuthRouteConfig`, this config loses its schemas: write `{ ... } satisfies IAuthRouteConfig`.',\n typedValid:\n 'This context is typed from its route: remove the type argument of `valid()`, which replaces the schema type.',\n otherRoute: 'This route is `{{expected}}`, and the context is typed from `{{actual}}`.',\n forwarded:\n 'The route `{{expected}}` passes its context to `{{method}}`, which is typed from `{{actual}}`.',\n },\n },\n create: (context) => {\n const frames: IFrame[] = [];\n const classes: IClassFacts[] = [];\n\n const isTyped = (path: string): boolean => {\n const [root] = path.split('.');\n const owner = frames.findLast((frame) => frame.declared.has(root ?? ''));\n\n return owner?.typed.has(path) === true;\n };\n\n const enterFunction = (node: Rule.Node) => {\n const frame: IFrame = { declared: new Set(), typed: new Set() };\n const params = RouteNodes.list(node, 'params');\n\n for (const param of params) {\n const own = RouteNodes.nameOf(param);\n\n if (own !== undefined) {\n frame.declared.add(own);\n }\n\n for (const property of RouteNodes.list(param, 'properties')) {\n const bound = RouteNodes.nameOf(RouteNodes.field(property, 'value'));\n\n if (bound !== undefined) {\n frame.declared.add(bound);\n }\n }\n\n const typed = RouteNodes.contextOf(param);\n\n if (typed !== undefined) {\n frame.typed.add(typed.path);\n }\n }\n\n frames.push(frame);\n\n const property = RouteNodes.field(node, 'parent');\n const call = RouteNodes.field(RouteNodes.field(property, 'parent'), 'parent');\n const isHandler = RouteNodes.nameOf(RouteNodes.field(property, 'key')) === 'handler';\n const route = isHandler ? RouteNodes.routeOf(call) : undefined;\n const [first] = params;\n const name = RouteNodes.nameOf(first);\n\n if (route === undefined || name === undefined || RouteNodes.annotation(first)) {\n return;\n }\n\n frame.typed.add(name);\n const method = RouteNodes.forwardOf(node, name);\n\n if (method !== undefined && route.config !== undefined) {\n classes.at(-1)?.forwards.push({ config: route.config, method, node });\n }\n };\n\n const leaveFunction = () => {\n frames.pop();\n };\n\n const checkDeclared = (opts: { node: Rule.Node; pattern: unknown; value: unknown }) => {\n // `{ ... } as const` under a wide annotation loses the schemas the same way.\n const value =\n RouteNodes.type(opts.value) === AstNodeTypes.AS_EXPRESSION\n ? RouteNodes.field(opts.value, 'expression')\n : opts.value;\n // An empty object is a collection filled later, with no literal schema to keep.\n const isObject =\n RouteNodes.type(value) === AstNodeTypes.OBJECT_EXPRESSION &&\n RouteNodes.list(value, 'properties').length > 0;\n\n if (isObject && RouteNodes.isWideConfig(RouteNodes.annotation(opts.pattern))) {\n context.report({ node: opts.node, messageId: 'wideConfig' });\n }\n };\n\n return {\n [AstNodeTypes.FUNCTION_DECLARATION]: enterFunction,\n [AstNodeTypes.FUNCTION_EXPRESSION]: enterFunction,\n [AstNodeTypes.ARROW_FUNCTION_EXPRESSION]: enterFunction,\n [`${AstNodeTypes.FUNCTION_DECLARATION}:exit`]: leaveFunction,\n [`${AstNodeTypes.FUNCTION_EXPRESSION}:exit`]: leaveFunction,\n [`${AstNodeTypes.ARROW_FUNCTION_EXPRESSION}:exit`]: leaveFunction,\n [AstNodeTypes.CLASS_BODY]: () => {\n classes.push({ typed: new Map(), forwards: [] });\n },\n [`${AstNodeTypes.CLASS_BODY}:exit`]: () => {\n const facts = classes.pop();\n\n for (const { config, method, node } of facts?.forwards ?? []) {\n const actual = facts?.typed.get(method);\n\n if (actual !== undefined && actual !== config) {\n context.report({\n node,\n messageId: 'forwarded',\n data: { expected: config, method, actual },\n });\n }\n }\n },\n [AstNodeTypes.METHOD_DEFINITION]: (node: Rule.Node) => {\n const method = RouteNodes.nameOf(RouteNodes.field(node, 'key'));\n const typed = RouteNodes.list(RouteNodes.field(node, 'value'), 'params')\n .map((param) => RouteNodes.contextOf(param))\n .find((found) => found !== undefined);\n const actual = typed?.config;\n\n if (method === undefined || actual === undefined) {\n return;\n }\n\n classes.at(-1)?.typed.set(method, actual);\n\n for (const decorator of RouteNodes.list(node, 'decorators')) {\n const call = RouteNodes.field(decorator, 'expression');\n const name = RouteNodes.nameOf(RouteNodes.field(call, 'callee'));\n const [options] = RouteNodes.list(call, 'arguments');\n const expected = RouteNodes.chain(RouteNodes.property(options, 'configs'));\n\n if (\n name !== undefined &&\n RouteNodes.DECORATORS.has(name) &&\n expected !== undefined &&\n expected !== actual\n ) {\n context.report({ node, messageId: 'otherRoute', data: { expected, actual } });\n }\n }\n },\n [AstNodeTypes.VARIABLE_DECLARATOR]: (node: Rule.Node) => {\n const pattern = RouteNodes.field(node, 'id');\n const value = RouteNodes.field(node, 'init');\n checkDeclared({ node, pattern, value });\n\n const frame = frames.at(-1);\n\n if (!frame) {\n return;\n }\n\n const own = RouteNodes.nameOf(pattern);\n\n if (own !== undefined) {\n frame.declared.add(own);\n }\n\n const source = RouteNodes.chain(value);\n\n for (const property of RouteNodes.list(pattern, 'properties')) {\n const key = RouteNodes.nameOf(RouteNodes.field(property, 'key'));\n const bound = RouteNodes.nameOf(RouteNodes.field(property, 'value'));\n\n if (bound === undefined) {\n continue;\n }\n\n const holdsContext = source !== undefined && isTyped(`${source}.${key}`);\n frame.declared.add(bound);\n\n if (holdsContext) {\n frame.typed.add(bound);\n }\n }\n },\n [AstNodeTypes.PROPERTY_DEFINITION]: (node: Rule.Node) => {\n checkDeclared({\n node,\n pattern: node,\n value: RouteNodes.field(node, 'value'),\n });\n },\n [AstNodeTypes.CALL_EXPRESSION]: (node: Rule.Node) => {\n const callee = RouteNodes.field(node, 'callee');\n const request = RouteNodes.field(callee, 'object');\n\n if (\n RouteNodes.nameOf(RouteNodes.field(callee, 'property')) !== 'valid' ||\n RouteNodes.nameOf(RouteNodes.field(request, 'property')) !== 'req' ||\n RouteNodes.typeArguments(node).length === 0\n ) {\n return;\n }\n\n const holder = RouteNodes.chain(RouteNodes.field(request, 'object'));\n\n if (holder !== undefined && isTyped(holder)) {\n context.report({ node, messageId: 'typedValid' });\n }\n },\n };\n },\n };\n\n static build(opts: { severity?: TRuleSeverity } = {}): Linter.RuleEntry {\n const { severity = 'error' } = opts;\n\n return [severity];\n }\n}\n"]}
|