diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b83602b..a4b55cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,6 +40,18 @@ jobs: name: thrift-ls-${{ matrix.goos }}-${{ matrix.goarch }} path: dist/ + # Test every package: unit, golden (cli_test.go + tests/e2e fixtures), + # corpus (check_test.go + tests/made-in-abyss), and fuzz regression. + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - name: test + run: go test ./... + vsix: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index 29a2452..4f4485c 100644 --- a/README.md +++ b/README.md @@ -384,15 +384,17 @@ git tag v0.1.0 && git push origin v0.1.0 ## Development ```bash -go test ./... # unit and fuzz regression tests -bash tests/e2e/run-e2e.sh # end-to-end formatter tests +go test ./... # unit, golden, and fuzz regression tests ``` -The formatter is fuzz-tested end to end: `FuzzFormat` checks that any clean -document formats without errors, keeps every comment, is idempotent and -deterministic across the whole option space. The lexer, parser, doc -printer, LSP offset mapper, and range formatting each have their own fuzz -targets; the corpus entries under `testdata/fuzz` are permanent regression +The golden CLI tests under `cli_test.go` pin the formatter flag combinations +(and the `check` lint corpus under `tests/made-in-abyss`) against committed +outputs in `tests/e2e`. The formatter is fuzz-tested end to end: +`FuzzFormat` checks that any clean document formats without errors, keeps +every comment, is idempotent and deterministic across the whole option +space. The lexer, parser, doc printer, LSP offset mapper, and range +formatting each have their own fuzz targets; the corpus entries under +`testdata/fuzz` are permanent regression tests. `thrift-ls dump` (see above) is the debugging companion: it shows the parse diff --git a/check_test.go b/check_test.go new file mode 100644 index 0000000..3c11dbf --- /dev/null +++ b/check_test.go @@ -0,0 +1,154 @@ +package main + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.lsp.dev/protocol" +) + +// Test_CheckMadeInAbyss pins the lint corpus: tests/made-in-abyss is the +// living spec of the diagnostics, one intentional mistake per section in +// lints.thrift, an include cycle on the cycle_a/b pair, and no +// diagnostics on the clean files. +func Test_CheckMadeInAbyss(t *testing.T) { + ctx := t.Context() + + files, err := collectThriftFiles("tests/made-in-abyss") + require.NoError(t, err) + + rootAbs, err := filepath.Abs("tests/made-in-abyss") + require.NoError(t, err) + + diags, err := checkFiles(ctx, files, rootAbs, nil) + require.NoError(t, err) + + // The clean corpus files report nothing. + for _, name := range []string{"abyss.thrift", "delvers.thrift", "orth.thrift"} { + assert.Empty(t, diags[corpusAbs(t, name)], "%s must be clean", name) + } + + // The mutual include cycle: one warning on cycle_a (its include of + // cycle_b closes the cycle), two on cycle_b. + cycleA := diags[corpusAbs(t, "cycle_a.thrift")] + require.Len(t, cycleA, 1) + assert.Contains(t, cycleA[0].Message, "cycle dependency") + + cycleB := diags[corpusAbs(t, "cycle_b.thrift")] + require.Len(t, cycleB, 2) + assert.Contains(t, cycleB[0].Message, "cycle dependency") + assert.Contains(t, cycleB[1].Message, "cycle dependency") + + // The mistake showcase: 18 errors and 5 warnings. + lints := diags[corpusAbs(t, "lints.thrift")] + require.Len(t, lints, 23) + + errs, warns := 0, 0 + for _, d := range lints { + switch d.Severity { + case protocol.DiagnosticSeverityError: + errs++ + case protocol.DiagnosticSeverityWarning: + warns++ + } + } + assert.Equal(t, 18, errs) + assert.Equal(t, 5, warns) + + // Every intentional mistake is reported. + for _, msg := range []string{ + "field id conflict", + "field id should be a positive integer in [1, 32767]", + "duplicate enum Reg", + "duplicate field same_name", + "duplicate field repeat", + "duplicate member RIKO", + "enum value 1 duplicates OZEN", + "duplicate function descend", + "duplicate argument depth", + `duplicate map key "zone1"`, + "duplicate set value 4", + "field type doesn't exist", + "STAR_COMPASS has no explicit value (implicitly 0)", + "UNHEARD_BELL has no explicit value (implicitly 3)", + "CROSSED_STILLS has no explicit value (implicitly 5)", + } { + assert.True(t, hasMessage(lints, msg), "missing diagnostic %q", msg) + } + + // Two checks on one line: the second `repeat` field conflicts on both + // its id (FieldIDCheck) and its name (DuplicateCheck). + var repeatLine uint32 + for _, d := range lints { + if strings.Contains(string(d.Message.(protocol.String)), "duplicate field repeat") { + repeatLine = d.Range.Start.Line + } + } + + onLine := diagsOnLine(lints, repeatLine) + require.Len(t, onLine, 2, "line %d must carry both diagnostics", repeatLine+1) + assert.Contains(t, onLine[0].Message, "field id conflict") + assert.Contains(t, onLine[1].Message, "duplicate field repeat") +} + +func Test_CollectThriftFiles(t *testing.T) { + // A single file. + files, err := collectThriftFiles("tests/made-in-abyss/lints.thrift") + require.NoError(t, err) + require.Len(t, files, 1) + assert.True(t, filepath.IsAbs(files[0])) + + // A folder, recursively, in lexical order. + files, err = collectThriftFiles("tests/made-in-abyss") + require.NoError(t, err) + + var names []string + for _, f := range files { + names = append(names, filepath.Base(f)) + } + assert.Equal(t, []string{ + "abyss.thrift", "cycle_a.thrift", "cycle_b.thrift", + "delvers.thrift", "lints.thrift", "orth.thrift", + }, names) + + // A missing path is an error. + _, err = collectThriftFiles("tests/does-not-exist") + require.Error(t, err) +} + +// corpusAbs is the absolute path of a made-in-abyss corpus file. +func corpusAbs(t *testing.T, name string) string { + t.Helper() + + p, err := filepath.Abs(filepath.Join("tests", "made-in-abyss", name)) + require.NoError(t, err) + + return p +} + +// hasMessage reports whether any diagnostic carries msg. +func hasMessage(diags []protocol.Diagnostic, msg string) bool { + for _, d := range diags { + if strings.Contains(string(d.Message.(protocol.String)), msg) { + return true + } + } + + return false +} + +// diagsOnLine returns the diagnostics starting on the 0-based line. +func diagsOnLine(diags []protocol.Diagnostic, line uint32) []protocol.Diagnostic { + var out []protocol.Diagnostic + + for _, d := range diags { + if d.Range.Start.Line == line { + out = append(out, d) + } + } + + return out +} diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 0000000..8e45bcd --- /dev/null +++ b/cli_test.go @@ -0,0 +1,125 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// runCLI executes the root command with args, capturing its writers. +func runCLI(t *testing.T, args ...string) (string, string, error) { + t.Helper() + + cmd := rootCommand() + var out, errOut bytes.Buffer + cmd.Writer = &out + cmd.ErrWriter = &errOut + + err := cmd.Run(t.Context(), append([]string{"thrift-ls"}, args...)) + + return out.String(), errOut.String(), err +} + +// Test_FormatCli_GoldenFields pins the indent and align combinations on a +// struct body against the golden outputs in tests/e2e/fields. +func Test_FormatCli_GoldenFields(t *testing.T) { + cases := []struct { + name, indent, align string + }{ + {"2spaces.assign", " ", "assign"}, + {"2spaces.disable", " ", "disable"}, + {"4spaces.field", " ", "field"}, + {"tab.assign", "\t", "assign"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + stdout, stderr, err := runCLI(t, "format", "-indent", c.indent, "-align", c.align, "tests/e2e/fields/fields.thrift") + require.NoError(t, err) + assert.Empty(t, stderr) + assert.Equal(t, readGolden(t, "tests/e2e/fields/"+c.name+".expect"), stdout) + }) + } +} + +// Test_FormatCli_GoldenFieldSeparators runs the struct separator modes on +// the comma-less fields fixture. The golden names keep the flag values of +// the CLI they were recorded against: add, remove, disable. +func Test_FormatCli_GoldenFieldSeparators(t *testing.T) { + cases := []struct { + name, sep string + }{ + {"add", "comma"}, + {"remove", "none"}, + {"disable", "preserve"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + stdout, stderr, err := runCLI(t, "format", "-struct-separator", c.sep, "tests/e2e/field_line_comma/fields.thrift") + require.NoError(t, err) + assert.Empty(t, stderr) + assert.Equal(t, readGolden(t, "tests/e2e/field_line_comma/"+c.name+".expect"), stdout) + }) + } +} + +// Test_FormatCli_GoldenEnums covers indent, align, and enum separator +// combinations on tests/e2e/enums. +func Test_FormatCli_GoldenEnums(t *testing.T) { + cases := []struct { + name, indent, align, sep string + }{ + {"2spaces.assign.add", " ", "assign", "comma"}, + {"2spaces.disable.remove", " ", "disable", "none"}, + {"4spaces.field.disable", " ", "field", "preserve"}, + {"tab.assign.disable", "\t", "assign", "preserve"}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + stdout, stderr, err := runCLI(t, "format", + "-indent", c.indent, "-align", c.align, "-enum-separator", c.sep, + "tests/e2e/enums/enums.thrift") + require.NoError(t, err) + assert.Empty(t, stderr) + assert.Equal(t, readGolden(t, "tests/e2e/enums/"+c.name+".expect"), stdout) + }) + } +} + +// Test_CheckCLI_MadeInAbyss runs the check command over the corpus and +// pins the diagnostic counts, including the per-file breakdown and the +// exit code gating on error-severity diagnostics. +func Test_CheckCLI_MadeInAbyss(t *testing.T) { + stdout, stderr, err := runCLI(t, "check", "tests/made-in-abyss") + require.Error(t, err) + assert.Equal(t, "", stderr) + + assert.Contains(t, stdout, "lints.thrift:") + assert.Contains(t, stdout, "cycle_a.thrift:") + assert.Contains(t, stdout, "cycle_b.thrift:") + + errCount := strings.Count(stdout, " error ") + warnCount := strings.Count(stdout, " warning ") + assert.Equal(t, 18, errCount, "error diagnostics") + assert.Equal(t, 8, warnCount, "warning diagnostics (5 lints + 3 cycles)") + + assert.Contains(t, err.Error(), "18 error(s)") + assert.Contains(t, err.Error(), "8 warning(s)") +} + +// readGolden returns the recorded output of a CLI test case. +func readGolden(t *testing.T, path string) string { + t.Helper() + + b, err := os.ReadFile(filepath.FromSlash(path)) + require.NoError(t, err) + + return string(b) +} diff --git a/formatter/field.go b/formatter/field.go index a6c49d5..daf5186 100644 --- a/formatter/field.go +++ b/formatter/field.go @@ -275,9 +275,9 @@ func computeFieldAlign(fields []*syntax.Field) *columnAlign { a.idWidth = maxInt(a.idWidth, len(field.FieldID.Text)+1) // "N:" } - if field.Req != 0 { + if field.Req != nil { a.hasReq = true - a.reqWidth = maxInt(a.reqWidth, len(field.Req.String())) + a.reqWidth = maxInt(a.reqWidth, len(field.Req.Kind.String())) } a.typeWidth = maxInt(a.typeWidth, len(typeText(field.Type))) @@ -421,8 +421,8 @@ func (f *formatter) fieldPads(v *syntax.Field, a *columnAlign) ([]padEntry, stri pads = append(pads, padEntry{v.TokStart() + 1, padRight("", a.idWidth-len(v.FieldID.Text)-1)}) } - if v.Req != 0 { - pads = append(pads, padEntry{v.Type.TokStart() - 1, padRight("", a.reqWidth-len(v.Req.String()))}) + if v.Req != nil { + pads = append(pads, padEntry{v.Type.TokStart() - 1, padRight("", a.reqWidth-len(v.Req.Kind.String()))}) } else if a.hasReq { // The empty requiredness column: extend the id pad by one column // plus the missing req width, or lead the field with it when there diff --git a/lsp/codeaction.go b/lsp/codeaction.go index 1c9fc53..293f9d3 100644 --- a/lsp/codeaction.go +++ b/lsp/codeaction.go @@ -33,6 +33,12 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara actions = append(actions, *enum) } + fieldActions, err := source.MakeFieldQualifierAction(ctx, ss, fh, params.Range) + if err != nil { + return nil, err + } + actions = append(actions, fieldActions...) + actions = preferQuickFixes(filterCodeActions(actions, params.Context.Only)) out := make([]protocol.CommandOrCodeAction, 0, len(actions)) diff --git a/lsp/source/diagnostic.go b/lsp/source/diagnostic.go index 1686bdc..4317d8a 100644 --- a/lsp/source/diagnostic.go +++ b/lsp/source/diagnostic.go @@ -20,6 +20,7 @@ func init() { &CycleCheck{}, &Parse{}, &FieldIDCheck{}, + &DuplicateCheck{}, &EnumValueCheck{}, &SemanticAnalysis{}, } diff --git a/lsp/source/duplicate_check.go b/lsp/source/duplicate_check.go new file mode 100644 index 0000000..2ca551e --- /dev/null +++ b/lsp/source/duplicate_check.go @@ -0,0 +1,321 @@ +package source + +import ( + "context" + "errors" + "fmt" + "log/slog" + "strconv" + + "go.lsp.dev/protocol" + "go.lsp.dev/uri" + + "github.com/karitham/thrift-ls/lsp/cache" + "github.com/karitham/thrift-ls/syntax" +) + +// DuplicateCheck reports the duplicates the compiler rejects: a name +// defined twice in one scope — structs, unions, exceptions, enums, members, +// fields, function arguments, service functions, typedefs, constants — enum +// members whose resolved value collides, and map keys or set values that +// repeat in a constant. Duplicates are ambiguous on the wire and in +// generated code, so they are errors. +type DuplicateCheck struct{} + +func (c *DuplicateCheck) Name() string { + return "DuplicateCheck" +} + +func (c *DuplicateCheck) Diagnostic(ctx context.Context, ss *cache.Snapshot, changeFiles []uri.URI) (DiagnosticResult, error) { + res := make(DiagnosticResult) + + for _, file := range changeFiles { + items, err := c.diagnostic(ctx, ss, file) + if err != nil { + return nil, err + } + + res[file] = items + } + + return res, nil +} + +func (c *DuplicateCheck) diagnostic(ctx context.Context, ss *cache.Snapshot, file uri.URI) ([]protocol.Diagnostic, error) { + pf, err := ss.Parse(ctx, file) + if err != nil { + return nil, err + } + + if pf.AST() == nil { + return nil, errors.New("parse ast failed") + } + + for _, err := range pf.Errors() { + slog.Debug("parse failed", "err", err) + } + + var ret []protocol.Diagnostic + + // Top-level definitions share one scope. + var defs []named + + for _, st := range pf.AST().Structs() { + defs = append(defs, named{"struct", st.Name}) + } + + for _, u := range pf.AST().Unions() { + defs = append(defs, named{"union", u.Name}) + } + + for _, ex := range pf.AST().Exceptions() { + defs = append(defs, named{"exception", ex.Name}) + } + + for _, en := range pf.AST().Enums() { + defs = append(defs, named{"enum", en.Name}) + } + + for _, td := range pf.AST().Typedefs() { + defs = append(defs, named{"typedef", td.Name}) + } + + for _, cs := range pf.AST().Consts() { + defs = append(defs, named{"const", cs.Name}) + } + + for _, sv := range pf.AST().Services() { + defs = append(defs, named{"service", sv.Name}) + } + + ret = append(ret, checkNames(pf, defs)...) + + for _, enum := range pf.AST().Enums() { + var members []named + + for _, v := range enum.Values { + members = append(members, named{"member", v.Name}) + } + + ret = append(ret, checkNames(pf, members)...) + ret = append(ret, checkEnumValues(pf, enum)...) + } + + structLikes := [][]*syntax.Field{} + for _, st := range pf.AST().Structs() { + structLikes = append(structLikes, st.Fields) + } + + for _, u := range pf.AST().Unions() { + structLikes = append(structLikes, u.Fields) + } + + for _, ex := range pf.AST().Exceptions() { + structLikes = append(structLikes, ex.Fields) + } + + for _, fields := range structLikes { + ret = append(ret, checkNames(pf, fieldNames(fields, "field"))...) + } + + for _, sv := range pf.AST().Services() { + var fns []named + for _, fn := range sv.Functions { + fns = append(fns, named{"function", fn.Name}) + } + + ret = append(ret, checkNames(pf, fns)...) + + for _, fn := range sv.Functions { + ret = append(ret, checkNames(pf, fieldNames(fn.Args, "argument"))...) + + if fn.Throws != nil { + ret = append(ret, checkNames(pf, fieldNames(fn.Throws.Fields, "field"))...) + } + } + } + + for _, cs := range pf.AST().Consts() { + ret = append(ret, checkValueDuplicates(pf, cs.Value, cs.Type)...) + } + + pf.AST().WalkFieldLists(func(fields []*syntax.Field, _ syntax.FieldListKind) { + for _, field := range fields { + if field.Value != nil { + ret = append(ret, checkValueDuplicates(pf, field.Value, field.Type)...) + } + } + }) + + return ret, nil +} + +// named is a definition with the label its messages use. +type named struct { + kind string + id *syntax.Identifier +} + +// fieldNames labels every field of a scope with the same kind. +func fieldNames(fields []*syntax.Field, kind string) []named { + out := make([]named, 0, len(fields)) + + for _, f := range fields { + out = append(out, named{kind, f.Name}) + } + + return out +} + +// checkNames reports a diagnostic on every name in a scope that repeats an +// earlier one. +func checkNames(pf *cache.ParsedFile, defs []named) []protocol.Diagnostic { + seen := make(map[string]bool) + + var ret []protocol.Diagnostic + + for _, d := range defs { + if d.id == nil || d.id.Text == "" { + continue + } + + if seen[d.id.Text] { + ret = append(ret, protocol.Diagnostic{ + Range: tokenRange(pf, nameToken(pf, d.id)), + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String(fmt.Sprintf("duplicate %s %s", d.kind, d.id.Text)), + }) + continue + } + + seen[d.id.Text] = true + } + + return ret +} + +// nameToken returns the token of an identifier. +func nameToken(pf *cache.ParsedFile, id *syntax.Identifier) *syntax.Token { + return &pf.AST().Tokens[id.TokStart()] +} + +// checkEnumValues reports enum members whose resolved value — an explicit +// constant or the compiler's auto-increment — collides with an earlier +// member's. +func checkEnumValues(pf *cache.ParsedFile, enum *syntax.Enum) []protocol.Diagnostic { + seen := make(map[int64]string) // value -> first member with it + + var ret []protocol.Diagnostic + + for _, mv := range enumValues(enum) { + if !mv.known { + continue + } + + if first, ok := seen[mv.value]; ok { + ret = append(ret, protocol.Diagnostic{ + Range: tokenRange(pf, enumValueNameToken(pf, mv.member)), + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String(fmt.Sprintf("enum value %d duplicates %s", mv.value, first)), + }) + continue + } + + seen[mv.value] = mv.member.Name.Text + } + + return ret +} + +// checkValueDuplicates reports repeated keys in map literals and repeated +// values in set literals, recursing into nested containers. The declared +// type decides whether a list literal is a set: lists keep duplicates, +// sets reject them. +func checkValueDuplicates(pf *cache.ParsedFile, value *syntax.ConstValue, typ *syntax.FieldType) []protocol.Diagnostic { + var ret []protocol.Diagnostic + + switch value.Kind { + case syntax.ValueMap: + seen := make(map[string]bool) + + for _, entry := range value.Map { + if entry.Key != nil { + if seen[valueKey(entry.Key)] { + ret = append(ret, duplicateValueDiagnostic(pf, entry.Key, "map key")) + } + seen[valueKey(entry.Key)] = true + } + + if entry.Value != nil { + ret = append(ret, checkValueDuplicates(pf, entry.Value, containerValueType(typ))...) + } + } + case syntax.ValueList: + if typ != nil && typ.Kind == syntax.TypeSet { + seen := make(map[string]bool) + + for _, item := range value.List { + if seen[valueKey(item)] { + ret = append(ret, duplicateValueDiagnostic(pf, item, "set value")) + } + seen[valueKey(item)] = true + } + } + + for _, item := range value.List { + ret = append(ret, checkValueDuplicates(pf, item, containerValueType(typ))...) + } + } + + return ret +} + +// containerValueType returns the element type of a container type — the +// value type of maps, the value type of lists and sets — or nil when typ +// is not a container. +func containerValueType(typ *syntax.FieldType) *syntax.FieldType { + if typ == nil { + return nil + } + + switch typ.Kind { + case syntax.TypeMap, syntax.TypeList, syntax.TypeSet: + return typ.ValueType + } + + return nil +} + +// valueKey is the canonical identity of a constant value for duplicate +// detection: strings by their content, integers by their numeric value, +// everything else by source text. +func valueKey(v *syntax.ConstValue) string { + switch v.Kind { + case syntax.ValueString: + if len(v.Text) >= 2 && v.Text[0] == v.Text[len(v.Text)-1] && + (v.Text[0] == '"' || v.Text[0] == '\'') { + return v.Text[1 : len(v.Text)-1] + } + + return v.Text + case syntax.ValueInt: + if n, err := strconv.ParseInt(v.Text, 0, 64); err == nil { + return "int:" + strconv.FormatInt(n, 10) + } + } + + return v.Text +} + +// duplicateValueDiagnostic is the diagnostic for a repeated map key or set +// value. +func duplicateValueDiagnostic(pf *cache.ParsedFile, v *syntax.ConstValue, kind string) protocol.Diagnostic { + return protocol.Diagnostic{ + Range: tokenRange(pf, &pf.AST().Tokens[v.TokStart()]), + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String(fmt.Sprintf("duplicate %s %s", kind, v.Text)), + } +} diff --git a/lsp/source/duplicate_check_test.go b/lsp/source/duplicate_check_test.go new file mode 100644 index 0000000..9def168 --- /dev/null +++ b/lsp/source/duplicate_check_test.go @@ -0,0 +1,352 @@ +package source + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "go.lsp.dev/protocol" + "go.lsp.dev/uri" + + "github.com/karitham/thrift-ls/lsp/cache" +) + +func Test_DuplicateCheck_Diagnostic(t *testing.T) { + tests := []struct { + name string + content string + want []protocol.Diagnostic + }{ + { + name: "distinct names and values are clean", + content: `struct User {} +enum Color { + RED = 0, + GREEN = 2, +} +service S { + void f(1: i32 x), +} +`, + want: nil, + }, + { + name: "duplicate top-level definitions", + content: `struct A {} +struct A {} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 7}, + End: protocol.Position{Line: 1, Character: 8}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate struct A"), + }, + }, + }, + { + name: "duplicate enum member names", + content: `enum E { + A, + A, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate member A"), + }, + }, + }, + { + name: "duplicate field names", + content: `struct S { + 1: i32 a, + 2: i32 a, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 9}, + End: protocol.Position{Line: 2, Character: 10}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate field a"), + }, + }, + }, + { + name: "duplicate function names", + content: `service S { + void f(), + void f(), +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 7}, + End: protocol.Position{Line: 2, Character: 8}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate function f"), + }, + }, + }, + { + name: "duplicate argument names", + content: `service S { + void f(1: i32 x, 2: i32 x), +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 26}, + End: protocol.Position{Line: 1, Character: 27}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate argument x"), + }, + }, + }, + { + name: "definitions of different kinds share one scope", + content: `struct User {} +enum User {} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 5}, + End: protocol.Position{Line: 1, Character: 9}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate enum User"), + }, + }, + }, + { + name: "explicit duplicate enum values", + content: `enum E { + A = 1, + B = 1, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("enum value 1 duplicates A"), + }, + }, + }, + { + name: "implicit member collides with explicit value", + content: `enum E { + A, + B = 0, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("enum value 0 duplicates A"), + }, + }, + }, + { + name: "unparseable constant skips the value chain", + content: `enum E { + A = 08, + B, + C = 0, +} +`, + want: nil, + }, + { + name: "multiple duplicate enum values", + content: `enum E { + A = 0, + B = 0, + C = 0, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("enum value 0 duplicates A"), + }, + { + Range: protocol.Range{ + Start: protocol.Position{Line: 3, Character: 2}, + End: protocol.Position{Line: 3, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("enum value 0 duplicates A"), + }, + }, + }, + { + name: "hex and decimal enum values collide", + content: `enum E { + A = 0x10, + B = 16, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 3}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("enum value 16 duplicates A"), + }, + }, + }, + { + name: "duplicate map string keys", + content: `const map M = { + "a": 1, + "a": 2, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 5}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String(`duplicate map key "a"`), + }, + }, + }, + { + name: "map keys collide by numeric value", + content: `const map M = { + 1: 1, + 0x1: 2, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 2}, + End: protocol.Position{Line: 2, Character: 5}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate map key 0x1"), + }, + }, + }, + { + name: "duplicate set values", + content: `const set S = [1, 2, 1] +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 0, Character: 26}, + End: protocol.Position{Line: 0, Character: 27}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate set value 1"), + }, + }, + }, + { + name: "list keeps duplicate values", + content: `const list L = [1, 1] +`, + want: nil, + }, + { + name: "nested set in map value", + content: `const map> M = { + "a": [1, 2, 1], +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 14}, + End: protocol.Position{Line: 1, Character: 15}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String("duplicate set value 1"), + }, + }, + }, + { + name: "field default with duplicate map key", + content: `struct Foo { + 1: map m = {"a": 1, "a": 2}, +} +`, + want: []protocol.Diagnostic{ + { + Range: protocol.Range{ + Start: protocol.Position{Line: 1, Character: 35}, + End: protocol.Position{Line: 1, Character: 38}, + }, + Severity: protocol.DiagnosticSeverityError, + Source: protocol.NewOptional("thrift-ls"), + Message: protocol.String(`duplicate map key "a"`), + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ss := buildSnapshotForTest(t, []*cache.FileChange{ + { + URI: "file:///tmp/user.thrift", + Version: 0, + Content: []byte(tt.content), + From: cache.FileChangeTypeDidOpen, + }, + }) + + got, err := (&DuplicateCheck{}).Diagnostic(t.Context(), ss, []uri.URI{"file:///tmp/user.thrift"}) + assert.NoError(t, err) + + assert.Equal(t, DiagnosticResult{"file:///tmp/user.thrift": tt.want}, got) + }) + } +} diff --git a/lsp/source/enum_value_check.go b/lsp/source/enum_value_check.go index cc5ecc0..32e2979 100644 --- a/lsp/source/enum_value_check.go +++ b/lsp/source/enum_value_check.go @@ -58,14 +58,29 @@ func (c *EnumValueCheck) diagnostic(ctx context.Context, ss *cache.Snapshot, fil var ret []protocol.Diagnostic for _, enum := range pf.AST().Enums() { - for _, im := range enumImplicitValues(enum) { - msg := fmt.Sprintf("%s has no explicit value", im.member.Name.Text) - if im.known { - msg = fmt.Sprintf("%s has no explicit value (implicitly %d)", im.member.Name.Text, im.value) + // A member whose name already appeared earlier in the enum is a + // duplicate; DuplicateCheck reports it as an error. Skip the + // implicit value warning for it: making a duplicate explicit + // does not fix it, and its auto-incremented value is incidental. + firstIdx := make(map[string]int, len(enum.Values)) + for i, member := range enum.Values { + if _, ok := firstIdx[member.Name.Text]; !ok { + firstIdx[member.Name.Text] = i + } + } + + for i, mv := range enumValues(enum) { + if mv.member.Value != nil || firstIdx[mv.member.Name.Text] != i { + continue + } + + msg := fmt.Sprintf("%s has no explicit value", mv.member.Name.Text) + if mv.known { + msg = fmt.Sprintf("%s has no explicit value (implicitly %d)", mv.member.Name.Text, mv.value) } ret = append(ret, protocol.Diagnostic{ - Range: tokenRange(pf, enumValueNameToken(pf, im.member)), + Range: tokenRange(pf, enumValueNameToken(pf, mv.member)), Severity: protocol.DiagnosticSeverityWarning, Source: protocol.NewOptional("thrift-ls"), Message: protocol.String(msg), @@ -97,27 +112,56 @@ type enumImplicitValue struct { func enumImplicitValues(enum *syntax.Enum) []enumImplicitValue { var out []enumImplicitValue + for _, mv := range enumValues(enum) { + if mv.member.Value != nil { + continue + } + + out = append(out, enumImplicitValue{member: mv.member, value: mv.value, known: mv.known}) + } + + return out +} + +// enumMemberValue is an enum member with the value the compiler resolves +// for it. +type enumMemberValue struct { + member *syntax.EnumValue + value int64 + known bool // false when the preceding value is broken, so value is unknowable +} + +// enumValues resolves every member of an enum to its on-wire value: +// explicit constants parse as written (base-0), implicit members +// auto-increment: 0 for the first member, one greater than the preceding +// member's value otherwise. Members after an unparseable explicit constant +// report known=false until the next parseable constant settles the chain. +func enumValues(enum *syntax.Enum) []enumMemberValue { + var out []enumMemberValue + // A virtual value of -1 precedes the first member so the first // implicit member auto-increments to 0, mirroring the compiler. val, known := int64(-1), true for _, member := range enum.Values { if member.Value == nil { - im := enumImplicitValue{member: member, known: known} if known { val++ - im.value = val } - out = append(out, im) + + out = append(out, enumMemberValue{member: member, value: val, known: known}) continue } v, err := strconv.ParseInt(member.Value.Text, 0, 64) if err != nil { + out = append(out, enumMemberValue{member: member, known: false}) known = false continue } + val, known = v, true + out = append(out, enumMemberValue{member: member, value: val, known: true}) } return out diff --git a/lsp/source/field_qualifier_action.go b/lsp/source/field_qualifier_action.go new file mode 100644 index 0000000..297d4b7 --- /dev/null +++ b/lsp/source/field_qualifier_action.go @@ -0,0 +1,117 @@ +package source + +import ( + "context" + "fmt" + "sort" + + "go.lsp.dev/protocol" + "go.lsp.dev/uri" + + "github.com/karitham/thrift-ls/lsp/cache" + "github.com/karitham/thrift-ls/syntax" +) + +// MakeFieldQualifierAction returns the "Make field required" and "Make +// field optional" code actions for the fields whose declaration line the +// selection covers. Every covered field yields one action per qualifier +// it does not already carry: an unqualified field gets both, a required +// field gets "Make field optional", and vice versa. Union fields never +// offer "Make field required": unions have no required members. +func MakeFieldQualifierAction(ctx context.Context, ss *cache.Snapshot, fh cache.FileHandle, rng protocol.Range) ([]protocol.CodeAction, error) { + pf, err := ss.Parse(ctx, fh.URI()) + if err != nil { + return nil, err + } + + if pf.AST() == nil || len(pf.Errors()) > 0 { + return nil, nil + } + + // Each picked action is kept beside the declaration offset of its + // field, so the final list can be sorted into document order. + var picked []struct { + offset int + code protocol.CodeAction + } + + pf.AST().WalkFieldLists(func(fields []*syntax.Field, kind syntax.FieldListKind) { + for _, field := range fields { + // TokenPosition is 1-based; selection lines are 0-based. + line := pf.AST().TokenPosition(field.TokStart()).Line - 1 + if line < int(rng.Start.Line) || line > int(rng.End.Line) { + continue + } + + unionField := kind == syntax.UnionFields + for _, qualifier := range fieldQualifiers(field, unionField) { + picked = append(picked, struct { + offset int + code protocol.CodeAction + }{ + offset: pf.AST().TokenPosition(field.TokStart()).Offset, + code: fieldQualifierAction(pf, fh, field, qualifier), + }) + } + } + }) + + sort.Slice(picked, func(i, j int) bool { + return picked[i].offset < picked[j].offset + }) + + actions := make([]protocol.CodeAction, len(picked)) + for i, p := range picked { + actions[i] = p.code + } + + return actions, nil +} + +// fieldQualifiers returns the qualifiers the field does not yet carry, in +// the order "required" before "optional". Union fields never offer +// "required": unions have no required members. +func fieldQualifiers(field *syntax.Field, unionField bool) []syntax.TokenKind { + switch { + case field.Req == nil: + if unionField { + return []syntax.TokenKind{syntax.TokenOptional} + } + + return []syntax.TokenKind{syntax.TokenRequired, syntax.TokenOptional} + case field.Req.Kind == syntax.TokenRequired: + return []syntax.TokenKind{syntax.TokenOptional} + default: // field.Req.Kind == syntax.TokenOptional + if unionField { + return nil + } + + return []syntax.TokenKind{syntax.TokenRequired} + } +} + +// fieldQualifierAction builds the action switching the field to qualifier: +// the edit replaces the span from the current qualifier keyword, or the +// type start when the field is unqualified, up to the type start. +func fieldQualifierAction(pf *cache.ParsedFile, fh cache.FileHandle, field *syntax.Field, qualifier syntax.TokenKind) protocol.CodeAction { + keyword := qualifier.String() + + start := pf.AST().TokenPosition(field.Type.TokStart()) + if field.Req != nil { + start = pf.AST().TokenPosition(pf.AST().TokenIndex(field.Req)) + } + end := pf.AST().TokenPosition(field.Type.TokStart()) + + return protocol.CodeAction{ + Title: fmt.Sprintf("Make field %s (%s)", keyword, field.Name.Text), + Kind: new(protocol.CodeActionKindRefactorRewrite), + Edit: &protocol.WorkspaceEdit{ + Changes: map[uri.URI][]protocol.TextEdit{ + fh.URI(): {{ + Range: toLSPRange(pf, start, end), + NewText: keyword + " ", + }}, + }, + }, + } +} diff --git a/lsp/source/fieldid_check.go b/lsp/source/fieldid_check.go index e1862e2..638df15 100644 --- a/lsp/source/fieldid_check.go +++ b/lsp/source/fieldid_check.go @@ -52,7 +52,7 @@ func (c *FieldIDCheck) diagnostic(ctx context.Context, ss *cache.Snapshot, file var ret []protocol.Diagnostic - processStructLike := func(fields []*syntax.Field) { + pf.AST().WalkFieldLists(func(fields []*syntax.Field, _ syntax.FieldListKind) { fieldIDSet := make(map[int][]*syntax.Field) for i := range fields { @@ -94,29 +94,7 @@ func (c *FieldIDCheck) diagnostic(ctx context.Context, ss *cache.Snapshot, file }) } } - } - - for _, st := range pf.AST().Structs() { - processStructLike(st.Fields) - } - - for _, union := range pf.AST().Unions() { - processStructLike(union.Fields) - } - - for _, excep := range pf.AST().Exceptions() { - processStructLike(excep.Fields) - } - - for _, svc := range pf.AST().Services() { - for _, fn := range svc.Functions { - processStructLike(fn.Args) - - if fn.Throws != nil { - processStructLike(fn.Throws.Fields) - } - } - } + }) return ret, nil } diff --git a/lsp/source/reference.go b/lsp/source/reference.go index d68c756..1cb8b04 100644 --- a/lsp/source/reference.go +++ b/lsp/source/reference.go @@ -576,37 +576,15 @@ func searchEnumQualifiedValueReference(ctx context.Context, ss *cache.Snapshot, // field defaults, const values, and service argument and throws defaults. // Positions without a default are skipped. func walkValueIdentifiers(doc *syntax.Document, fn func(v *syntax.ConstValue)) { - process := func(fields []*syntax.Field) { + doc.WalkFieldLists(func(fields []*syntax.Field, _ syntax.FieldListKind) { for _, field := range fields { if field.Value != nil { fn(field.Value) } } - } - - for _, st := range doc.Structs() { - process(st.Fields) - } - - for _, st := range doc.Unions() { - process(st.Fields) - } - - for _, st := range doc.Exceptions() { - process(st.Fields) - } + }) for _, cst := range doc.Consts() { fn(cst.Value) } - - for _, svc := range doc.Services() { - for _, fnx := range svc.Functions { - process(fnx.Args) - - if fnx.Throws != nil { - process(fnx.Throws.Fields) - } - } - } } diff --git a/lsp/source/semantic_analysis.go b/lsp/source/semantic_analysis.go index ae8d07e..e41d3dc 100644 --- a/lsp/source/semantic_analysis.go +++ b/lsp/source/semantic_analysis.go @@ -48,104 +48,11 @@ func (s *SemanticAnalysis) diagnostic(ctx context.Context, ss *cache.Snapshot, c slog.Debug("parse failed", "err", err) } - res := s.checkDefineConflict(ctx, pf) - items := s.checkDefinitionExist(ctx, ss, changeFile, pf) - res = append(res, items...) + res := s.checkDefinitionExist(ctx, ss, changeFile, pf) return res, nil } -// checkDefineConflict reports duplicate field names, function names, and -// top-level definition names. -func (s *SemanticAnalysis) checkDefineConflict(ctx context.Context, pf *cache.ParsedFile) []protocol.Diagnostic { - var ret []protocol.Diagnostic - - processStructLike := func(fields []*syntax.Field) { - fieldMap := make(map[string]struct{}) - - for i := range fields { - field := fields[i] - if _, exist := fieldMap[field.Name.Text]; exist { - ret = append(ret, protocol.Diagnostic{ - Range: nodeRange(pf, field.Name), - Severity: protocol.DiagnosticSeverityError, - Source: protocol.NewOptional("thrift-ls"), - Message: protocol.String("field name conflict with other field"), - }) - } - - fieldMap[field.Name.Text] = struct{}{} - } - } - - definitionNameMap := make(map[string]string) - - processDefinition := func(name string, node syntax.Node, kind string) { - if previous, exist := definitionNameMap[name]; exist { - ret = append(ret, protocol.Diagnostic{ - Range: nodeRange(pf, node), - Severity: protocol.DiagnosticSeverityError, - Source: protocol.NewOptional("thrift-ls"), - Message: protocol.String(fmt.Sprintf("%s name conflict with other %s", kind, previous)), - }) - } - - definitionNameMap[name] = kind - } - - for _, st := range pf.AST().Structs() { - processDefinition(st.Name.Text, st.Name, "struct") - processStructLike(st.Fields) - } - - for _, union := range pf.AST().Unions() { - processDefinition(union.Name.Text, union.Name, "union") - processStructLike(union.Fields) - } - - for _, excep := range pf.AST().Exceptions() { - processDefinition(excep.Name.Text, excep.Name, "exception") - processStructLike(excep.Fields) - } - - for _, enum := range pf.AST().Enums() { - processDefinition(enum.Name.Text, enum.Name, "enum") - } - - for _, cst := range pf.AST().Consts() { - processDefinition(cst.Name.Text, cst.Name, "const") - } - - for _, td := range pf.AST().Typedefs() { - processDefinition(td.Name.Text, td.Name, "typedef") - } - - for _, svc := range pf.AST().Services() { - processDefinition(svc.Name.Text, svc.Name, "service") - - fnMap := make(map[string]struct{}) - for _, fn := range svc.Functions { - if _, exist := fnMap[fn.Name.Text]; exist { - ret = append(ret, protocol.Diagnostic{ - Range: nodeRange(pf, fn.Name), - Severity: protocol.DiagnosticSeverityWarning, - Source: protocol.NewOptional("thrift-ls"), - Message: protocol.String("function name conflict with other function"), - }) - } - - fnMap[fn.Name.Text] = struct{}{} - processStructLike(fn.Args) - - if fn.Throws != nil { - processStructLike(fn.Throws.Fields) - } - } - } - - return ret -} - // checkDefinitionExist reports field types, const values, and return types // that reference undefined definitions. func (s *SemanticAnalysis) checkDefinitionExist(ctx context.Context, ss *cache.Snapshot, file uri.URI, pf *cache.ParsedFile) []protocol.Diagnostic { @@ -169,17 +76,9 @@ func (s *SemanticAnalysis) checkDefinitionExist(ctx context.Context, ss *cache.S } } - for _, st := range pf.AST().Structs() { - processStructLike(st.Fields) - } - - for _, union := range pf.AST().Unions() { - processStructLike(union.Fields) - } - - for _, excep := range pf.AST().Exceptions() { - processStructLike(excep.Fields) - } + pf.AST().WalkFieldLists(func(fields []*syntax.Field, _ syntax.FieldListKind) { + processStructLike(fields) + }) for _, cst := range pf.AST().Consts() { items := s.checkConstValueExist(ctx, ss, file, pf, cst.Value) @@ -190,12 +89,6 @@ func (s *SemanticAnalysis) checkDefinitionExist(ctx context.Context, ss *cache.S for _, fn := range svc.Functions { items := s.checkTypeExist(ctx, ss, file, pf, fn.Type) ret = append(ret, items...) - - processStructLike(fn.Args) - - if fn.Throws != nil { - processStructLike(fn.Throws.Fields) - } } } diff --git a/lsp/source/semantic_analysis_test.go b/lsp/source/semantic_analysis_test.go index 385503d..267eee3 100644 --- a/lsp/source/semantic_analysis_test.go +++ b/lsp/source/semantic_analysis_test.go @@ -100,21 +100,6 @@ struct TestUUID { Source: protocol.NewOptional("thrift-ls"), Message: protocol.String("field type doesn't exist"), }, - { - Range: protocol.Range{ - Start: protocol.Position{ - Line: 6, - Character: 8, - }, - End: protocol.Position{ - Line: 6, - Character: 15, - }, - }, - Severity: protocol.DiagnosticSeverityError, - Source: protocol.NewOptional("thrift-ls"), - Message: protocol.String("struct name conflict with other struct"), - }, { Range: protocol.Range{ Start: protocol.Position{ diff --git a/main.go b/main.go index 84f179d..7a4168c 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" + "sort" "strings" "github.com/urfave/cli/v3" @@ -15,15 +17,30 @@ import ( "github.com/karitham/thrift-ls/formatter" tlog "github.com/karitham/thrift-ls/log" "github.com/karitham/thrift-ls/lsp" + "github.com/karitham/thrift-ls/lsp/cache" + "github.com/karitham/thrift-ls/lsp/source" "github.com/karitham/thrift-ls/options" "github.com/karitham/thrift-ls/syntax" "go.lsp.dev/jsonrpc2" "go.lsp.dev/pkg/fakenet" + "go.lsp.dev/protocol" + "go.lsp.dev/uri" ) func main() { - cmd := &cli.Command{ + cmd := rootCommand() + + if err := cmd.Run(context.Background(), os.Args); err != nil { + fmt.Fprintln(os.Stderr, "thrift-ls:", err) + os.Exit(1) + } +} + +// rootCommand is the CLI: the default (no subcommand) is the language +// server, with format, dump, and check as subcommands. +func rootCommand() *cli.Command { + return &cli.Command{ Name: "thrift-ls", Usage: "Thrift language server and formatter", Version: lsp.ServerVersion, @@ -65,13 +82,15 @@ func main() { }, Action: dumpAction, }, + { + Name: "check", + Usage: "report parse, semantic, and lint diagnostics on thrift files", + ArgsUsage: "", + Flags: lspFlags(), + Action: checkAction, + }, }, } - - if err := cmd.Run(context.Background(), os.Args); err != nil { - fmt.Fprintln(os.Stderr, "thrift-ls:", err) - os.Exit(1) - } } // lspFlags are the flags shared by the default and lsp subcommands. @@ -206,7 +225,7 @@ func formatAction(ctx context.Context, cmd *cli.Command) error { return err } - return formatFile(file, cmd.Bool("w"), cmd.Bool("d"), cmd.String("config"), cli) + return formatFile(file, cmd.Writer, cmd.Bool("w"), cmd.Bool("d"), cmd.String("config"), cli) } // dumpAction prints the parse tree, and optionally the formatted document @@ -261,6 +280,181 @@ func dumpAction(ctx context.Context, cmd *cli.Command) error { return nil } +// checkAction reports the diagnostics the language server computes — parse +// errors, semantic analysis, and lints — for a thrift file or folder, +// through the same cache and checker pipeline the LSP uses. Diagnostics +// print to stdout; the command exits 1 when any error-severity diagnostic +// is found, so it can gate CI. Warnings do not fail the check. +func checkAction(ctx context.Context, cmd *cli.Command) error { + path := cmd.Args().First() + if path == "" { + return errors.New("must specify a thrift file or folder to check, e.g. thrift-ls check file.thrift") + } + + files, err := collectThriftFiles(path) + if err != nil { + return err + } + + if len(files) == 0 { + return fmt.Errorf("no thrift files found in %s", path) + } + + cfg := loadConfig(cmd.String("config"), ".") + patch := options.Effective(cfg) + + cliPatch, err := lspPatch(cmd) + if err != nil { + return err + } + + patch = cliPatch.Apply(patch) + + root := path + if info, err := os.Stat(path); err != nil { + return err + } else if !info.IsDir() { + root = filepath.Dir(path) + } + + rootAbs, err := filepath.Abs(root) + if err != nil { + return err + } + + diags, err := checkFiles(ctx, files, rootAbs, derefStrings(patch.IncludePaths)) + if err != nil { + return err + } + + w := cmd.Writer + errCount, warnCount := 0, 0 + + for _, file := range files { + for _, d := range diags[file] { + sev := "warning" + if d.Severity == protocol.DiagnosticSeverityError { + sev = "error" + errCount++ + } else { + warnCount++ + } + + fmt.Fprintf(w, "%s:%d:%d %s %s\n", relPath(file), d.Range.Start.Line+1, d.Range.Start.Character+1, sev, d.Message) + } + } + + if errCount > 0 { + return fmt.Errorf("found %d error(s), %d warning(s)", errCount, warnCount) + } + + return nil +} + +// checkFiles runs the language server's diagnostic pipeline — parse, +// semantic analysis, and lints — over files opened in a session rooted at +// folder, and returns the diagnostics per file, keyed by absolute path. +func checkFiles(ctx context.Context, files []string, folder string, includePaths []string) (map[string][]protocol.Diagnostic, error) { + c := cache.New(includePaths) + sess := cache.NewSession(c) + sess.AddView(uri.File(folder)) + + changes := make([]*cache.FileChange, 0, len(files)) + uris := make([]uri.URI, 0, len(files)) + + for _, file := range files { + content, err := os.ReadFile(file) + if err != nil { + return nil, err + } + + u := uri.File(file) + uris = append(uris, u) + changes = append(changes, &cache.FileChange{URI: u, Version: 0, Content: content, From: cache.FileChangeTypeDidOpen}) + } + + if err := sess.UpdateOverlayFS(ctx, changes); err != nil { + return nil, err + } + + out := make(map[string][]protocol.Diagnostic, len(files)) + + for i := range files { + v, err := sess.ViewOf(uris[i]) + if err != nil { + return nil, err + } + + ss, release := v.Snapshot() + + res, err := source.NewDiagnostic().Diagnostic(ctx, ss, uris) + release() + if err != nil { + return nil, err + } + + out[files[i]] = res[uris[i]] + } + + return out, nil +} + +// collectThriftFiles returns the absolute paths of the thrift files under +// path: the file itself, or every *.thrift in the folder, recursively, in +// lexical order. +func collectThriftFiles(path string) ([]string, error) { + abs, err := filepath.Abs(path) + if err != nil { + return nil, err + } + + info, err := os.Stat(abs) + if err != nil { + return nil, err + } + + if !info.IsDir() { + return []string{abs}, nil + } + + var files []string + + err = filepath.WalkDir(abs, func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if !d.IsDir() && strings.HasSuffix(d.Name(), ".thrift") { + files = append(files, p) + } + + return nil + }) + if err != nil { + return nil, err + } + + sort.Strings(files) + + return files, nil +} + +// relPath renders p relative to the working directory when it lies under +// it, and absolute otherwise. +func relPath(p string) string { + base, err := os.Getwd() + if err != nil { + return p + } + + rel, err := filepath.Rel(base, p) + if err != nil || strings.HasPrefix(rel, "..") { + return p + } + + return rel +} + // lspPatch builds an options patch from the explicitly set lsp flags. func lspPatch(cmd *cli.Command) (options.Patch, error) { p := options.Patch{} @@ -359,8 +553,8 @@ func fatal(err error) { } // formatFile formats a single file: read, parse, resolve options, format, -// self-validate, and write, diff, or print. -func formatFile(file string, write, diffOut bool, configPath string, cli options.Patch) error { +// self-validate, and write, diff, or print to w. +func formatFile(file string, w io.Writer, write, diffOut bool, configPath string, cli options.Patch) error { if file == "" { return errors.New("must specify a thrift file to format, e.g. thrift-ls format file.thrift") } @@ -408,11 +602,11 @@ func formatFile(file string, write, diffOut bool, configPath string, cli options return os.WriteFile(file, []byte(out), perms) case diffOut: - fmt.Print(string(Diff("old", src, "new", []byte(out)))) + fmt.Fprint(w, string(Diff("old", src, "new", []byte(out)))) return nil default: - fmt.Print(out) + fmt.Fprint(w, out) return nil } diff --git a/syntax/accessors.go b/syntax/accessors.go index acfdcce..76cb1ca 100644 --- a/syntax/accessors.go +++ b/syntax/accessors.go @@ -133,3 +133,41 @@ func (d *Document) Typedefs() []*Typedef { return out } + +// FieldListKind identifies the declaration a field list belongs to. +type FieldListKind uint8 + +const ( + StructFields FieldListKind = iota + UnionFields + ExceptionFields + FunctionArgs // service function arguments + ThrowsFields // a service function's throws clause +) + +// WalkFieldLists visits the field lists of every struct, union, +// exception, service function argument, and throws clause in document +// order. +func (d *Document) WalkFieldLists(fn func(fields []*Field, kind FieldListKind)) { + for _, st := range d.Structs() { + fn(st.Fields, StructFields) + } + + for _, union := range d.Unions() { + fn(union.Fields, UnionFields) + } + + for _, excep := range d.Exceptions() { + fn(excep.Fields, ExceptionFields) + } + + for _, svc := range d.Services() { + for _, fnx := range svc.Functions { + fn(fnx.Args, FunctionArgs) + + if fnx.Throws != nil { + fn(fnx.Throws.Fields, ThrowsFields) + } + } + } +} diff --git a/syntax/ast.go b/syntax/ast.go index adb67c3..27c7972 100644 --- a/syntax/ast.go +++ b/syntax/ast.go @@ -213,7 +213,7 @@ type Service struct { type Field struct { nodeBase FieldID *Token // the optional id int token; nil means implicit - Req TokenKind + Req *Token // the required/optional keyword token; nil means unqualified Type *FieldType Reference bool // & prefix (field reference) Name *Identifier diff --git a/syntax/parser.go b/syntax/parser.go index b94eb30..228c08c 100644 --- a/syntax/parser.go +++ b/syntax/parser.go @@ -562,7 +562,7 @@ func (p *parser) parseField() (*Field, bool) { switch p.cur().Kind { case TokenRequired, TokenOptional: - f.Req = p.advance().Kind + f.Req = p.advance() } f.Type = p.parseFieldType() diff --git a/syntax/parser_test.go b/syntax/parser_test.go index c2298bc..fbc7ffe 100644 --- a/syntax/parser_test.go +++ b/syntax/parser_test.go @@ -193,7 +193,7 @@ func TestParseStructs(t *testing.T) { t.Errorf("field 0 id = %v", f.FieldID) } - if f.Req != TokenRequired { + if f.Req == nil || f.Req.Kind != TokenRequired { t.Errorf("field 0 req = %v", f.Req) } diff --git a/tests/e2e/enums/test.sh b/tests/e2e/enums/test.sh deleted file mode 100755 index f652be8..0000000 --- a/tests/e2e/enums/test.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -echo "run e2e test case: enums" - -IFS="." -for f in ./tests/e2e/enums/*.expect -do - if test -f "$f"; then - echo "======================================================" - substr=${f##*/} - read -ra options <<<"$substr" - indent=${options[0]} - align=${options[1]} - field_line_comma=${options[2]} - echo "indent: ${indent}, align: ${align}, field_line_comma: ${field_line_comma}" - got=$("$THRIFT_LS_BIN" format -indent "${indent}" -align "${align}" -field-separator "${field_line_comma}" tests/e2e/enums/enums.thrift) - expected=$(cat "$f") - if [ "$got" == "$expected" ];then - echo "pass" - else : - echo "failed" - printf 'got: \n%s\n' "${got}" - printf 'expected: \n%s\n' "${expected}" - diff <(echo "$got") <(echo "$expected") - fi - echo "======================================================" - fi - -done diff --git a/tests/e2e/field_line_comma/test.sh b/tests/e2e/field_line_comma/test.sh deleted file mode 100755 index 0bfcf4a..0000000 --- a/tests/e2e/field_line_comma/test.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -echo "run e2e test case: field line comma" - -IFS="." -for f in ./tests/e2e/field_line_comma/*.expect -do - if test -f "$f"; then - echo "======================================================" - substr=${f##*/} - read -ra options <<<"$substr" - field_line_comma=${options[0]} - echo "fieldLineComma: ${field_line_comma}" - got=$("$THRIFT_LS_BIN" format -field-separator "${field_line_comma}" tests/e2e/field_line_comma/fields.thrift) - expected=$(cat "$f") - if [ "$got" == "$expected" ];then - echo "pass" - else : - echo "failed" - printf 'got: \n%s\n' "${got}" - printf 'expected: \n%s\n' "${expected}" - diff <(echo "$got") <(echo "$expected") - fi - echo "======================================================" - fi - -done diff --git a/tests/e2e/fields/2spaces.assign.expect b/tests/e2e/fields/2spaces.assign.expect index 4d72bf3..84faa9a 100644 --- a/tests/e2e/fields/2spaces.assign.expect +++ b/tests/e2e/fields/2spaces.assign.expect @@ -2,4 +2,4 @@ struct User { 1: required string Name, 2: required i32 Age = 15, 3: optional string longlonglonglonglonglonglong_info = "", -} \ No newline at end of file +} diff --git a/tests/e2e/fields/tab.assign.expect b/tests/e2e/fields/tab.assign.expect index 885e833..95f9860 100644 --- a/tests/e2e/fields/tab.assign.expect +++ b/tests/e2e/fields/tab.assign.expect @@ -2,4 +2,4 @@ struct User { 1: required string Name, 2: required i32 Age = 15, 3: optional string longlonglonglonglonglonglong_info = "", -} \ No newline at end of file +} diff --git a/tests/e2e/fields/test.sh b/tests/e2e/fields/test.sh deleted file mode 100644 index 8a101b9..0000000 --- a/tests/e2e/fields/test.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash - -echo "run e2e test case: fields" - -IFS="." -for f in ./tests/e2e/fields/*.expect -do - if test -f "$f"; then - echo "======================================================" - substr=${f##*/} - read -ra options <<<"$substr" - indent=${options[0]} - align=${options[1]} - echo "indent: ${indent}, align: ${align}" - got=$("$THRIFT_LS_BIN" format -indent "${indent}" -align "${align}" tests/e2e/fields/fields.thrift) - expected=$(cat "$f") - if [ "$got" == "$expected" ];then - echo "pass" - else : - echo "failed" - printf 'got: \n%s\n' "${got}" - printf 'expected: \n%s\n' "${expected}" - diff <(echo "$got") <(echo "$expected") - fi - echo "======================================================" - fi - -done diff --git a/tests/e2e/run-e2e.sh b/tests/e2e/run-e2e.sh deleted file mode 100755 index ad9f1a0..0000000 --- a/tests/e2e/run-e2e.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -BIN_DIR=$(mktemp -d) -trap 'rm -rf "$BIN_DIR"' EXIT -go build -o "$BIN_DIR/thrift-ls" . -export THRIFT_LS_BIN="$BIN_DIR/thrift-ls" - -for f in ./tests/e2e/* -do - if test -d "$f"; then - bash "$f"/test.sh - fi -done diff --git a/tests/made-in-abyss/abyss.thrift b/tests/made-in-abyss/abyss.thrift new file mode 100644 index 0000000..7f05607 --- /dev/null +++ b/tests/made-in-abyss/abyss.thrift @@ -0,0 +1,59 @@ +// Core types of the made-in-abyss corpus. This file has no dependencies: +// it exists so the other files can exercise include resolution and +// cross-file references against it, with every name and value deliberate +// and no diagnostics expected here. +namespace go abyss + +// Enum with explicit values: no EnumValueCheck warnings, no duplicate +// values. Keep it that way; the implicit-value demo lives in lints.thrift. +enum Layer { + EDGE = 1, + FOREST_OF_TEMPTATION = 2, + THE_GREAT_FAULT = 3, + GOBLETS_OF_GIANTS = 4, + SEA_OF_CORPSES = 5, + CAPITAL_OF_THE_UNRETURNED = 6, + FINAL_MAELSTROM = 7, +} + +enum WhistleRank { + RED = 1, + BLUE = 2, + MOON = 3, + BLACK = 4, + WHITE = 5, +} + +enum CurseSeverity { + LIGHT = 1, + MODERATE = 2, + SEVERE = 3, + FATAL = 4, +} + +// Field ids 1-2, explicit qualifiers on 2: exercises required/optional +// parsing without triggering any lint. +struct Delver { + 1: string name, + 2: required WhistleRank whistle, + 3: i32 deepest_layer, + 4: optional list relic_names, +} + +struct Relic { + 1: string name, + 2: optional string curse_note, + 3: optional i32 lucerium_value, +} + +struct Hollow { + 1: string name, + 2: bool is_hollowed, +} + +// Exception referenced by orth.thrift's throws clause: cross-file +// resolution of named types. +exception DiveTooDeep { + 1: i32 requested_layer, + 2: string message, +} diff --git a/tests/made-in-abyss/cycle_a.thrift b/tests/made-in-abyss/cycle_a.thrift new file mode 100644 index 0000000..4053c21 --- /dev/null +++ b/tests/made-in-abyss/cycle_a.thrift @@ -0,0 +1,7 @@ +// CycleCheck — cycle_a.thrift and cycle_b.thrift include each other. +// Expect the cycle diagnostic on this include while the folder is open. +include "cycle_b.thrift" + +namespace go made + +struct FromCycleA {} \ No newline at end of file diff --git a/tests/made-in-abyss/cycle_b.thrift b/tests/made-in-abyss/cycle_b.thrift new file mode 100644 index 0000000..11756dd --- /dev/null +++ b/tests/made-in-abyss/cycle_b.thrift @@ -0,0 +1,6 @@ +// The back half of the include cycle with cycle_a.thrift. +include "cycle_a.thrift" + +namespace go made + +struct FromCycleB {} \ No newline at end of file diff --git a/tests/made-in-abyss/delvers.thrift b/tests/made-in-abyss/delvers.thrift new file mode 100644 index 0000000..8632c1f --- /dev/null +++ b/tests/made-in-abyss/delvers.thrift @@ -0,0 +1,32 @@ +// Second consumer of abyss.thrift: same include, different set of +// cross-file references. Together with orth.thrift this exercises two +// files resolving the same include once the folder is open. +include "abyss.thrift" + +namespace ts abyss + +// Typedef and consts: trust the parser round-trip, no cross-file names. +typedef i32 lucerium_capacity + +const i32 RICO_WHISTLE = 1 + +// Const struct with cross-file enum-qualified member references: the +// WhistleRank.RED / WhistleRank.WHITE dots resolve into abyss.thrift. +const Delver RIKO = { + "name": "Riko", + "whistle": WhistleRank.RED, + "deepest_layer": 4, +} + +const Delver REG = { + "name": "Reg", + "whistle": WhistleRank.WHITE, + "deepest_layer": 5, +} + +// Service overload-style: two functions over included types; the second +// throws into an included exception. +service DelverExpedition { + void descend(1: Delver team, 2: optional i32 max_depth), + Relic salvage(1: string layer, 2: map loot) throws (1: DiveTooDeep too_deep), +} \ No newline at end of file diff --git a/tests/made-in-abyss/lints.thrift b/tests/made-in-abyss/lints.thrift new file mode 100644 index 0000000..a4dd010 --- /dev/null +++ b/tests/made-in-abyss/lints.thrift @@ -0,0 +1,119 @@ +// Every intentional mistake in the corpus, in one file. The comment +// above each section names the check that fires and the message to +// expect: +// +// struct DuplicateFieldID FieldIDCheck — "field id conflict" +// struct BadVault FieldIDCheck — "field id should be a +// positive integer in [1, 32767]" +// struct Reg + enum Reg DuplicateCheck — "duplicate enum Reg" +// struct DuplicateFieldName DuplicateCheck — "duplicate field +// same_name" +// struct DoubleTrouble FieldIDCheck + DuplicateCheck — two +// diagnostics on one line: "field id +// conflict" and "duplicate field repeat" +// enum RelicKind EnumValueCheck (warning) + the +// "Make enum values explicit" code action +// enum CurseBreaker DuplicateCheck — "enum value 1 +// duplicates OZEN" +// enum DuplicateMember DuplicateCheck — "duplicate member +// RIKO" +// service DescendUnion DuplicateCheck — "duplicate argument +// depth", "duplicate function descend" +// struct Without SemanticAnalysis — "field type doesn't +// exist" +// const CURSED_LOCATIONS DuplicateCheck — "duplicate map key +// \"zone1\"", "duplicate set value 4" +// +// struct Clean at the bottom is fully valid: no diagnostics expected. + +// FieldIDCheck — both `1:` tokens fire "field id conflict". +struct DuplicateFieldID { + 1: i32 weight, + 1: i32 range, + 2: string material, +} + +// FieldIDCheck — ids must be in [1, 32767]: 0 and -1 are just as bad as +// 40000. +struct BadVault { + 40000: i32 too_high, + 0: i32 zero, + -1: i32 negative, +} + +// DuplicateCheck — the second Reg collides with the first: names share +// one top-level scope regardless of kind. +struct Reg { + 1: i32 range, +} + +enum Reg { + IDLE = 1, +} + +// DuplicateCheck — both fields are named same_name. +struct DuplicateFieldName { + 1: i32 same_name, + 2: i32 same_name, +} + +// Two checks on one line: the second field repeats both the id and the +// name of the first, so FieldIDCheck ("field id conflict") and +// DuplicateCheck ("duplicate field repeat") fire on the same line. +struct DoubleTrouble { + 1: i32 repeat, + 1: i32 repeat, +} + +// EnumValueCheck — STAR_COMPASS, UNHEARD_BELL and CROSSED_STILLS have +// implicit values 0, 3 and 5. Putting the cursor anywhere on the enum +// offers "Make enum values explicit" as a refactor, and as a quickfix +// on the warnings. +enum RelicKind { + STAR_COMPASS, + BLAZE_REAP = 2, + UNHEARD_BELL, + GAVEL_OF_THE_ABYSS = 4, + CROSSED_STILLS, +} + +// DuplicateCheck — OZEN and MITTY both resolve to enum value 1. +enum CurseBreaker { + OZEN = 1, + MITTY = 1, +} + +// DuplicateCheck — the third member repeats RIKO's name. +enum DuplicateMember { + RIKO, + NANACHI, + RIKO, +} + +// DuplicateCheck — argument depth twice in the first function, and the +// function name descend twice in the service. +service DescendUnion { + void descend(1: i32 depth, 2: i32 depth), + void descend(1: i32 far), +} + +// SemanticAnalysis — MissingRelic and MissingLayer are not defined +// anywhere in the folder. +struct Without { + 1: MissingRelic missing, + 2: list xs, +} + +// DuplicateCheck — the duplicated "zone1" key, and a set literal that +// contains 4 twice. +const map> CURSED_LOCATIONS = { + "zone1": [1, 2, 3], + "zone3": [3, 4, 4], + "zone1": [7, 8, 9], +} + +// Fully valid — no diagnostics expected here. +struct Clean { + 1: required i32 id, + 2: optional string name, +} diff --git a/tests/made-in-abyss/orth.thrift b/tests/made-in-abyss/orth.thrift new file mode 100644 index 0000000..09017ae --- /dev/null +++ b/tests/made-in-abyss/orth.thrift @@ -0,0 +1,22 @@ +// Include resolution: every type used below — Delver, Relic, WhistleRank, +// DiveTooDeep — must resolve through `include "abyss.thrift"`. Resolve any +// name and expect the definition in abyss.thrift; reference `delvers.thrift` +// and expect the same types while the file is open. +include "abyss.thrift" + +namespace py orth + +// Const referencing a builtin type: no resolution needed, just an int. +const i32 ORTH_DELIVER_COUNT = 64 + +// Typedef of a builtin: exercises the typedef node without pulling in +// another file. +typedef i32 relic_rarity + +// Service using only included types, including a throws reference to the +// exception defined in the include. +service DelverUnion { + void register_delver(1: Delver delver), + void dive_relay(1: i32 depth) throws (1: DiveTooDeep too_deep), + void ring_whistle(1: WhistleRank rank), +} \ No newline at end of file