Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/lazy-promise-in-effect-sync-0af7c0f.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/tsgo": minor
---

Add the `lazyPromiseInEffectSync` diagnostic for `Effect.sync` thunks that return the global `Promise<T>` type.

This ports the upstream language-service behavior to the Go implementation, including v3/v4 examples, baselines, and exact Promise detection via TypeScriptGo checker shims.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Some diagnostics are off by default or have a default severity of suggestion, bu
<tr><td><code>globalErrorInEffectCatch</code></td><td>⚠️</td><td></td><td>Warns when catch callbacks return global Error type instead of typed errors</td><td>✓</td><td>✓</td></tr>
<tr><td><code>globalErrorInEffectFailure</code></td><td>⚠️</td><td></td><td>Warns when the global Error type is used in an Effect failure channel</td><td>✓</td><td>✓</td></tr>
<tr><td><code>layerMergeAllWithDependencies</code></td><td>⚠️</td><td>🔧</td><td>Detects interdependencies in Layer.mergeAll calls where one layer provides a service that another layer requires</td><td>✓</td><td>✓</td></tr>
<tr><td><code>lazyPromiseInEffectSync</code></td><td>⚠️</td><td></td><td>Warns when Effect.sync lazily returns a Promise instead of using an async Effect constructor</td><td>✓</td><td>✓</td></tr>
<tr><td><code>leakingRequirements</code></td><td>💡</td><td></td><td>Detects implementation services leaked in service methods</td><td>✓</td><td>✓</td></tr>
<tr><td><code>multipleEffectProvide</code></td><td>⚠️</td><td>🔧</td><td>Warns against chaining Effect.provide calls which can cause service lifecycle issues</td><td>✓</td><td>✓</td></tr>
<tr><td><code>returnEffectInGen</code></td><td>💡</td><td>🔧</td><td>Warns when returning an Effect in a generator causes nested Effect&lt;Effect&lt;...&gt;&gt;</td><td>✓</td><td>✓</td></tr>
Expand Down
24 changes: 24 additions & 0 deletions _packages/tsgo/src/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,30 @@
]
}
},
{
"name": "lazyPromiseInEffectSync",
"group": "antipattern",
"description": "Warns when Effect.sync lazily returns a Promise instead of using an async Effect constructor",
"defaultSeverity": "warning",
"fixable": false,
"supportedEffect": [
"v3",
"v4"
],
"codes": [
377082
],
"preview": {
"sourceText": "import { Effect } from \"effect\"\n\nexport const preview = Effect.sync(() =\u003e Promise.resolve(1))\n",
"diagnostics": [
{
"start": 68,
"end": 92,
"text": "This `Effect.sync` thunk returns a Promise. Use `Effect.promise` or `Effect.tryPromise` to represent async work. effect(lazyPromiseInEffectSync)"
}
]
}
},
{
"name": "leakingRequirements",
"group": "antipattern",
Expand Down
4 changes: 4 additions & 0 deletions internal/diagnostics/effectDiagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@
"category": "Warning",
"code": 377080
},
"This `Effect.sync` thunk returns a Promise. Use `Effect.promise` or `Effect.tryPromise` to represent async work. effect(lazyPromiseInEffectSync)": {
"category": "Warning",
"code": 377082
},
"This code declares an async function, consider representing this async control flow with Effect values and `Effect.gen`. effect(asyncFunction)": {
"category": "Warning",
"code": 377081
Expand Down
65 changes: 65 additions & 0 deletions internal/rules/lazy_promise_in_effect_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package rules

import (
"github.com/effect-ts/tsgo/etscore"
"github.com/effect-ts/tsgo/internal/rule"
"github.com/effect-ts/tsgo/internal/typeparser"
"github.com/microsoft/typescript-go/shim/ast"
"github.com/microsoft/typescript-go/shim/checker"
tsdiag "github.com/microsoft/typescript-go/shim/diagnostics"
"github.com/microsoft/typescript-go/shim/scanner"
)

var LazyPromiseInEffectSync = rule.Rule{
Name: "lazyPromiseInEffectSync",
Group: "antipattern",
Description: "Warns when Effect.sync lazily returns a Promise instead of using an async Effect constructor",
DefaultSeverity: etscore.SeverityWarning,
SupportedEffect: []string{"v3", "v4"},
Codes: []int32{
tsdiag.This_Effect_sync_thunk_returns_a_Promise_Use_Effect_promise_or_Effect_tryPromise_to_represent_async_work_effect_lazyPromiseInEffectSync.Code(),
},
Run: func(ctx *rule.Context) []*ast.Diagnostic {
var diags []*ast.Diagnostic
var walk ast.Visitor
walk = func(node *ast.Node) bool {
if node == nil {
return false
}

if node.Kind == ast.KindCallExpression {
call := node.AsCallExpression()
if call != nil && ctx.TypeParser.IsNodeReferenceToEffectModuleApi(call.Expression, "sync") && call.Arguments != nil && len(call.Arguments.Nodes) > 0 {
lazyArg := call.Arguments.Nodes[0]
lazyArgType := ctx.TypeParser.GetTypeAtLocation(lazyArg)
if lazyArgType != nil && thunkReturnsPromise(ctx.Checker, ctx.TypeParser, lazyArgType) {
diags = append(diags, ctx.NewDiagnostic(
ctx.SourceFile,
scanner.GetErrorRangeForNode(ctx.SourceFile, lazyArg),
tsdiag.This_Effect_sync_thunk_returns_a_Promise_Use_Effect_promise_or_Effect_tryPromise_to_represent_async_work_effect_lazyPromiseInEffectSync,
nil,
))
}
}
}

node.ForEachChild(walk)
return false
}

walk(ctx.SourceFile.AsNode())
return diags
},
}

func thunkReturnsPromise(c *checker.Checker, tp *typeparser.TypeParser, lazyArgType *checker.Type) bool {
for _, member := range tp.UnrollUnionMembers(lazyArgType) {
for _, signature := range c.GetSignaturesOfType(member, checker.SignatureKindCall) {
returnType := c.GetReturnTypeOfSignature(signature)
if tp.PromiseType(returnType) != nil {
return true
}
}
}
return false
}
1 change: 1 addition & 0 deletions internal/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var All = []rule.Rule{
SchemaUnionOfLiterals,
MissingEffectServiceDependency,
LeakingRequirements,
LazyPromiseInEffectSync,
InstanceOfSchema,
GenericEffectServices,
OverriddenSchemaConstructor,
Expand Down
31 changes: 31 additions & 0 deletions internal/typeparser/promise_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package typeparser

import (
"github.com/microsoft/typescript-go/shim/checker"
)

// PromiseType returns t when it is the global Promise type or a reference to it.
// It intentionally does not match arbitrary thenables or PromiseLike values.
func (tp *TypeParser) PromiseType(t *checker.Type) *checker.Type {
if tp == nil || tp.checker == nil || t == nil {
return nil
}

return Cached(&tp.links.PromiseType, t, func() *checker.Type {
getGlobalPromiseTypeChecked := checker.Checker_getGlobalPromiseTypeChecked(tp.checker)
if getGlobalPromiseTypeChecked == nil {
return nil
}

globalPromiseType := getGlobalPromiseTypeChecked()
if globalPromiseType == nil || globalPromiseType == checker.Checker_emptyGenericType(tp.checker) {
return nil
}

if checker.Checker_isReferenceToType(tp.checker, t, globalPromiseType) || t == globalPromiseType {
return t
}

return nil
})
}
1 change: 1 addition & 0 deletions internal/typeparser/type_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type EffectLinks struct {
EffectSchemaTypes core.LinkStore[*checker.Type, *SchemaTypes]
IsScopeType core.LinkStore[*checker.Type, bool]
IsPipeableType core.LinkStore[*checker.Type, bool]
PromiseType core.LinkStore[*checker.Type, *checker.Type]
IsGlobalErrorType core.LinkStore[*checker.Type, bool]
IsYieldableErrorType core.LinkStore[*checker.Type, bool]

Expand Down
5 changes: 5 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,11 @@
"default": "warning",
"description": "Detects interdependencies in Layer.mergeAll calls where one layer provides a service that another layer requires"
},
"lazyPromiseInEffectSync": {
"$ref": "#/definitions/effectLanguageServicePluginSeverityDefinition",
"default": "warning",
"description": "Warns when Effect.sync lazily returns a Promise instead of using an async Effect constructor"
},
"leakingRequirements": {
"$ref": "#/definitions/effectLanguageServicePluginSeverityDefinition",
"default": "suggestion",
Expand Down
3 changes: 2 additions & 1 deletion shim/checker/extra-shim.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"ExtraMethods": {
"Checker": ["isTypeAssignableTo", "isArrayType", "isReadonlyArrayType", "getIndexInfosOfType", "getTypeArguments", "getLiteralTypeFromProperty", "getSymbolIfSameReference", "getSymbolOfDeclaration", "isSignatureAssignableTo", "newFunctionType", "newCallSignature"]
"Checker": ["isTypeAssignableTo", "isArrayType", "isReadonlyArrayType", "getIndexInfosOfType", "getTypeArguments", "getLiteralTypeFromProperty", "getSymbolIfSameReference", "getSymbolOfDeclaration", "isSignatureAssignableTo", "isReferenceToType", "newFunctionType", "newCallSignature"]
},
"ExtraFields": {
"Checker": ["getGlobalPromiseTypeChecked", "emptyGenericType"],
"IndexInfo": ["keyType", "valueType"]
}
}
Loading
Loading