Skip to content
Open
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
22 changes: 22 additions & 0 deletions .changeset/add-prefer-schema-union.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"@effect/tsgo": minor
---

Add `preferSchemaUnion` diagnostic (`TS377130`) and code fix for Effect v4 to suggest composing Schema values with `Schema.Union` instead of unioning their extracted `Type` properties.

### Example

```ts
// Before
export const Circle = Schema.Struct({ kind: Schema.Literal("circle"), radius: Schema.Number })
export const Square = Schema.Struct({ kind: Schema.Literal("square"), side: Schema.Number })

export type Shape = typeof Circle.Type | typeof Square.Type

// After applying code fix
export const Circle = Schema.Struct({ kind: Schema.Literal("circle"), radius: Schema.Number })
export const Square = Schema.Struct({ kind: Schema.Literal("square"), side: Schema.Number })

export const Shape = Schema.Union([Circle, Square])
export type Shape = typeof Shape.Type
```
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Some diagnostics are off by default or have a default severity of suggestion, bu
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/new-schema-class.md"><code>newSchemaClass</code></a></td><td>Suggests using Schema make instead of new for Schema classes</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/option-match-to-from-option.md"><code>optionMatchToFromOption</code></a></td><td>Suggests Effect.fromOption when Option.match or an Option tag conditional only converts Some to Effect.succeed and None to Effect.fail</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/prefer-schema-type-property.md"><code>preferSchemaTypeProperty</code></a></td><td>Disallows Schema.Schema.Type&lt;typeof X&gt; in favor of typeof X.Type</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/prefer-schema-union.md"><code>preferSchemaUnion</code></a></td><td>Suggests composing Schema values with Schema.Union instead of unioning their extracted Type properties</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/prefer-succeed-some-or-none.md"><code>preferSucceedSomeOrNone</code></a></td><td>Suggests using Effect.succeedNone or Effect.succeedSome instead of wrapping Option.none or Option.some with Effect.succeed</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/prefer-typed-schema-decoder.md"><code>preferTypedSchemaDecoder</code></a></td><td>Suggests typed Schema decoders when the input is assignable to the schema&#39;s Encoded type</td></tr>
<tr><td><a href="https://github.com/Effect-TS/tsgo/blob/main/docs/rules/provide-layer-succeed-to-provide-service.md"><code>provideLayerSucceedToProvideService</code></a></td><td>Suggests providing inline Layer.succeed and Layer.effect services directly</td></tr>
Expand Down
23 changes: 23 additions & 0 deletions _packages/tsgo/src/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,29 @@
]
}
},
{
"name": "preferSchemaUnion",
"group": "style",
"description": "Suggests composing Schema values with Schema.Union instead of unioning their extracted Type properties",
"defaultSeverity": "off",
"fixable": true,
"supportedEffect": [
"v4"
],
"codes": [
377130
],
"preview": {
"sourceText": "import { Schema } from \"effect\"\n\nexport const Circle = Schema.Struct({ kind: Schema.Literal(\"circle\"), radius: Schema.Number })\nexport const Square = Schema.Struct({ kind: Schema.Literal(\"square\"), side: Schema.Number })\n\nexport type Shape = typeof Circle.Type | typeof Square.Type\n",
"diagnostics": [
{
"start": 242,
"end": 281,
"text": "This type alias unions decoded Effect Schema types. Prefer Schema.Union([...]) and derive the type from the resulting schema. effect(preferSchemaUnion)"
}
]
}
},
{
"name": "preferSucceedSomeOrNone",
"group": "style",
Expand Down
66 changes: 66 additions & 0 deletions docs/rules/prefer-schema-union.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!-- This file is generated by internal/rules/rules_json_test.go. Do not edit it manually. -->

# `preferSchemaUnion`

Suggests composing Schema values with Schema.Union instead of unioning their extracted Type properties

| Property | Value |
| --- | --- |
| Category | Style |
| Default severity | `off` |
| Fixable | Yes |
| Effect versions | v4 |
| Diagnostic codes | `TS377130` |
| Language Service name | `preferSchemaUnion` |
| Oxlint name | `effecttsgo/prefer-schema-union` |

## Preview

```ts
import { Schema } from "effect"

export const Circle = Schema.Struct({ kind: Schema.Literal("circle"), radius: Schema.Number })
export const Square = Schema.Struct({ kind: Schema.Literal("square"), side: Schema.Number })

export type Shape = typeof Circle.Type | typeof Square.Type
/**
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ effecttsgo(prefer-schema-union): This type alias unions decoded Effect Schema types. Prefer Schema.Union([...]) and derive the type from the resulting schema.
*/
```

## Language Service Configuration

See the [Language Service setup guide](../../README.md#installation) for installation instructions.

```jsonc
{
"$schema": "./node_modules/@effect/tsgo/schema.json",
"compilerOptions": {
"plugins": [
{
"name": "@effect/language-service",
"diagnosticSeverity": {
"preferSchemaUnion": "warning"
}
}
]
}
}
```

## Oxlint Configuration

See the [Oxlint setup guide](../README.md#oxlint-setup) for installation and patching instructions.

```json
{
"$schema": "./node_modules/@effect/tsgo/oxlint-schema.json",
"options": {
"typeAware": true
},
"plugins": ["effecttsgo"],
"rules": {
"effecttsgo/prefer-schema-union": "warn"
}
}
```
4 changes: 4 additions & 0 deletions internal/diagnostics/effectDiagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -506,5 +506,9 @@
"This module reference imports `{0}`, which is obsolete in Effect v4. In Effect v4, Schema is provided directly by `Schema` from `effect` (or `effect/Schema`). effect(obsoleteSchemaImport)": {
"category": "Warning",
"code": 377128
},
"This type alias unions decoded Effect Schema types. Prefer Schema.Union([...]) and derive the type from the resulting schema. effect(preferSchemaUnion)": {
"category": "Suggestion",
"code": 377130
}
}
1 change: 1 addition & 0 deletions internal/fixables/fixables.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var All = []fixable.Fixable{
UnnecessaryArrowBlockFix,
UnnecessaryTypeofTypeFix,
PreferSchemaTypePropertyFix,
PreferSchemaUnionFix,
EffectMapVoidFix,
UnnecessaryFailYieldableErrorFix,
ClassSelfMismatchFix,
Expand Down
Loading