From 84e116d070264a7b7fc1738226a0e8abb7bf86d5 Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Tue, 11 Nov 2025 21:40:16 +0000 Subject: [PATCH] feat: add options schema for `no-duplicate-type-constituents` (#397) --- .../no_duplicate_type_constituents.go | 13 +------ .../no_duplicate_type_constituents_test.go | 4 +-- .../no_duplicate_type_constituents/options.go | 34 +++++++++++++++++++ .../schema.json | 18 ++++++++++ 4 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 internal/rules/no_duplicate_type_constituents/options.go create mode 100644 internal/rules/no_duplicate_type_constituents/schema.json diff --git a/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents.go b/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents.go index c2e83fb0..3ff3cf2b 100644 --- a/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents.go +++ b/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents.go @@ -37,21 +37,10 @@ const ( unionOrIntersection_Intersection ) -type NoDuplicateTypeConstituentsOptions struct { - IgnoreIntersections bool - IgnoreUnions bool -} - var NoDuplicateTypeConstituentsRule = rule.Rule{ Name: "no-duplicate-type-constituents", Run: func(ctx rule.RuleContext, options any) rule.RuleListeners { - opts, ok := options.(NoDuplicateTypeConstituentsOptions) - if !ok { - opts = NoDuplicateTypeConstituentsOptions{ - IgnoreIntersections: false, - IgnoreUnions: false, - } - } + opts := utils.UnmarshalOptions[NoDuplicateTypeConstituentsOptions](options, "no-duplicate-type-constituents") unwindedParentType := func(node *ast.Node, kind ast.Kind) *ast.Node { for { diff --git a/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents_test.go b/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents_test.go index 43f3f81b..bb0ec131 100644 --- a/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents_test.go +++ b/internal/rules/no_duplicate_type_constituents/no_duplicate_type_constituents_test.go @@ -133,11 +133,11 @@ type T = Record; }, { Code: "type T = A | A;", - Options: NoDuplicateTypeConstituentsOptions{IgnoreUnions: true}, + Options: rule_tester.OptionsFromJSON[NoDuplicateTypeConstituentsOptions](`{"ignoreUnions": true}`), }, { Code: "type T = A & A;", - Options: NoDuplicateTypeConstituentsOptions{IgnoreIntersections: true}, + Options: rule_tester.OptionsFromJSON[NoDuplicateTypeConstituentsOptions](`{"ignoreIntersections": true}`), }, { Code: "type T = Class | Class;", diff --git a/internal/rules/no_duplicate_type_constituents/options.go b/internal/rules/no_duplicate_type_constituents/options.go new file mode 100644 index 00000000..8cc44a58 --- /dev/null +++ b/internal/rules/no_duplicate_type_constituents/options.go @@ -0,0 +1,34 @@ +// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT. + +package no_duplicate_type_constituents + +import "encoding/json" + +type NoDuplicateTypeConstituentsOptions struct { + // IgnoreIntersections corresponds to the JSON schema field "ignoreIntersections". + IgnoreIntersections bool `json:"ignoreIntersections,omitempty"` + + // IgnoreUnions corresponds to the JSON schema field "ignoreUnions". + IgnoreUnions bool `json:"ignoreUnions,omitempty"` +} + +// UnmarshalJSON implements json.Unmarshaler. +func (j *NoDuplicateTypeConstituentsOptions) UnmarshalJSON(value []byte) error { + var raw map[string]interface{} + if err := json.Unmarshal(value, &raw); err != nil { + return err + } + type Plain NoDuplicateTypeConstituentsOptions + var plain Plain + if err := json.Unmarshal(value, &plain); err != nil { + return err + } + if v, ok := raw["ignoreIntersections"]; !ok || v == nil { + plain.IgnoreIntersections = false + } + if v, ok := raw["ignoreUnions"]; !ok || v == nil { + plain.IgnoreUnions = false + } + *j = NoDuplicateTypeConstituentsOptions(plain) + return nil +} diff --git a/internal/rules/no_duplicate_type_constituents/schema.json b/internal/rules/no_duplicate_type_constituents/schema.json new file mode 100644 index 00000000..d0f8dfd1 --- /dev/null +++ b/internal/rules/no_duplicate_type_constituents/schema.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://json-schema.org/draft-07/schema#", + "definitions": { + "no_duplicate_type_constituents_options": { + "type": "object", + "properties": { + "ignoreIntersections": { + "type": "boolean", + "default": false + }, + "ignoreUnions": { + "type": "boolean", + "default": false + } + } + } + } +}