Skip to content

Commit 82623ec

Browse files
committed
chore: update dependencies and improve code documentation
1 parent bb8d5a9 commit 82623ec

File tree

21 files changed

+178
-203
lines changed

21 files changed

+178
-203
lines changed

.github/workflows/backend-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
go mod tidy -go=1.21
2727
git diff --exit-code
2828
- name: golangci-lint
29-
uses: golangci/golangci-lint-action@v6
29+
uses: golangci/golangci-lint-action@v8
3030
with:
31-
version: v1.56.1
31+
version: v2.4.0
3232
args: --verbose --timeout=3m
3333
skip-cache: true
3434

.golangci.yaml

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
version: "2"
2+
13
run:
24
timeout: 10m
5+
36
linters:
47
enable:
58
- errcheck
6-
- goimports
79
- revive
810
- govet
911
- staticcheck
@@ -16,69 +18,73 @@ linters:
1618
- forbidigo
1719
- mirror
1820
- bodyclose
21+
settings:
22+
revive:
23+
# Run entire rule set and selectively disable the ones that are too strict for now.
24+
enable-all-rules: true
25+
rules:
26+
- name: file-header
27+
disabled: true
28+
- name: line-length-limit
29+
disabled: true
30+
- name: function-length
31+
disabled: true
32+
- name: max-public-structs
33+
disabled: true
34+
- name: function-result-limit
35+
disabled: true
36+
- name: banned-characters
37+
disabled: true
38+
- name: argument-limit
39+
disabled: true
40+
- name: cognitive-complexity
41+
disabled: true
42+
- name: cyclomatic
43+
disabled: true
44+
- name: confusing-results
45+
disabled: true
46+
- name: add-constant
47+
disabled: true
48+
- name: flag-parameter
49+
disabled: true
50+
- name: nested-structs
51+
disabled: true
52+
- name: import-shadowing
53+
disabled: true
54+
- name: early-return
55+
disabled: true
56+
- name: use-any
57+
disabled: true
58+
- name: exported
59+
disabled: true
60+
- name: unhandled-error
61+
disabled: true
62+
- name: if-return
63+
disabled: true
64+
- name: max-control-nesting
65+
disabled: true
66+
gocritic:
67+
disabled-checks:
68+
- ifElseChain
69+
govet:
70+
enable-all: true
71+
disable:
72+
- fieldalignment
73+
- shadow
74+
settings:
75+
printf:
76+
funcs:
77+
- common.Errorf
78+
forbidigo:
79+
forbid:
80+
- pattern: 'fmt\\.Errorf(# Please use errors\\.Wrap\\|Wrapf\\|Errorf instead)?'
81+
- pattern: 'ioutil\\.ReadDir(# Please use os\\.ReadDir)?'
1982

20-
linters-settings:
21-
goimports:
22-
# Put imports beginning with prefix after 3rd-party packages.
23-
local-prefixes: github.com/usememos/memos
24-
revive:
25-
# Default to run all linters so that new rules in the future could automatically be added to the static check.
26-
enable-all-rules: true
27-
rules:
28-
# The following rules are too strict and make coding harder. We do not enable them for now.
29-
- name: file-header
30-
disabled: true
31-
- name: line-length-limit
32-
disabled: true
33-
- name: function-length
34-
disabled: true
35-
- name: max-public-structs
36-
disabled: true
37-
- name: function-result-limit
38-
disabled: true
39-
- name: banned-characters
40-
disabled: true
41-
- name: argument-limit
42-
disabled: true
43-
- name: cognitive-complexity
44-
disabled: true
45-
- name: cyclomatic
46-
disabled: true
47-
- name: confusing-results
48-
disabled: true
49-
- name: add-constant
50-
disabled: true
51-
- name: flag-parameter
52-
disabled: true
53-
- name: nested-structs
54-
disabled: true
55-
- name: import-shadowing
56-
disabled: true
57-
- name: early-return
58-
disabled: true
59-
- name: use-any
60-
disabled: true
61-
- name: exported
62-
disabled: true
63-
- name: unhandled-error
64-
disabled: true
65-
- name: if-return
66-
disabled: true
67-
- name: max-control-nesting
68-
disabled: true
69-
gocritic:
70-
disabled-checks:
71-
- ifElseChain
72-
govet:
73-
settings:
74-
printf: # The name of the analyzer, run `go tool vet help` to see the list of all analyzers
75-
funcs: # Run `go tool vet help printf` to see the full configuration of `printf`.
76-
- common.Errorf
77-
enable-all: true
78-
disable:
79-
- fieldalignment
80-
- shadow
81-
forbidigo:
82-
forbid:
83-
- 'fmt\.Errorf(# Please use errors\.Wrap\|Wrapf\|Errorf instead)?'
84-
- 'ioutil\.ReadDir(# Please use os\.ReadDir)?'
83+
formatters:
84+
enable:
85+
- goimports
86+
settings:
87+
goimports:
88+
# Keep local modules grouped after 3rd-party imports.
89+
local-prefixes:
90+
- github.com/usememos/memos

ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package ast defines the abstract syntax tree nodes used by gomark.
12
package ast
23

34
type NodeType string

ast/document.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func (d *Document) CloneMetadata() map[string]any {
2727
if len(d.Metadata) == 0 {
2828
return nil
2929
}
30-
copy := make(map[string]any, len(d.Metadata))
30+
metadataCopy := make(map[string]any, len(d.Metadata))
3131
for k, v := range d.Metadata {
32-
copy[k] = v
32+
metadataCopy[k] = v
3333
}
34-
return copy
34+
return metadataCopy
3535
}

config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package config provides configuration structures for the gomark parser.
12
package config
23

34
// ParserConfig holds configuration options for the parser.
@@ -188,6 +189,8 @@ func (c *ParserConfig) WithExtension(name string, enabled bool) *ParserConfig {
188189
config.EnableExtensions.ReferencedContent = enabled
189190
case "tags":
190191
config.EnableExtensions.Tags = enabled
192+
default:
193+
return config
191194
}
192195
return config
193196
}
@@ -225,4 +228,4 @@ func (c *ParserConfig) WithMaxFileSize(size int64) *ParserConfig {
225228
config := c.Clone()
226229
config.MaxFileSize = size
227230
return config
228-
}
231+
}

gomark.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package gomark exposes high-level parsing and rendering helpers.
12
package gomark
23

34
import (
@@ -73,7 +74,7 @@ func (e *Engine) Parse(markdown string) (*ast.Document, error) {
7374
}
7475

7576
// Restore renders the AST document to markdown.
76-
func (e *Engine) Restore(doc *ast.Document) string {
77+
func (*Engine) Restore(doc *ast.Document) string {
7778
if doc == nil {
7879
return ""
7980
}

parser/errors.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parser
22

33
import (
44
"fmt"
5+
56
"github.com/usememos/gomark/parser/tokenizer"
67
)
78

@@ -104,8 +105,8 @@ func NewIncompleteElementError(element string, position int) *ParseError {
104105

105106
// ParseResult wraps parsing results with error information.
106107
type ParseResult struct {
107-
Success bool
108-
Errors []*ParseError
108+
Success bool
109+
Errors []*ParseError
109110
Warnings []*ParseError
110111
}
111112

@@ -145,4 +146,4 @@ func NewParseResult() *ParseResult {
145146
Errors: []*ParseError{},
146147
Warnings: []*ParseError{},
147148
}
148-
}
149+
}

parser/internal/code_block.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,22 @@ func (*CodeBlockParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
2727
return nil, 0
2828
}
2929

30-
// Check for new-style triple backtick token or legacy three separate tokens
31-
if firstRow[0].Type == tokenizer.TripleBacktick {
32-
// New tokenizer style
33-
} else if len(firstRow) >= 3 && firstRow[0].Type == tokenizer.Backtick && firstRow[1].Type == tokenizer.Backtick && firstRow[2].Type == tokenizer.Backtick {
34-
// Legacy tokenizer style
35-
} else {
30+
// Check for new-style triple backtick token or legacy three separate tokens.
31+
isNewStyle := firstRow[0].Type == tokenizer.TripleBacktick
32+
isLegacyStyle := len(firstRow) >= 3 && firstRow[0].Type == tokenizer.Backtick && firstRow[1].Type == tokenizer.Backtick && firstRow[2].Type == tokenizer.Backtick
33+
if !isNewStyle && !isLegacyStyle {
3634
return nil, 0
3735
}
38-
languageTokens := []*tokenizer.Token{}
39-
if firstRow[0].Type == tokenizer.TripleBacktick {
40-
// New tokenizer style - language tokens start at index 1
36+
37+
var languageTokens []*tokenizer.Token
38+
if isNewStyle {
39+
// New tokenizer style - language tokens start at index 1.
4140
if len(firstRow) > 1 {
4241
languageTokens = firstRow[1:]
4342
}
44-
} else {
45-
// Legacy tokenizer style - language tokens start at index 3
46-
if len(firstRow) > 3 {
47-
languageTokens = firstRow[3:]
48-
}
43+
} else if len(firstRow) > 3 {
44+
// Legacy tokenizer style - language tokens start at index 3.
45+
languageTokens = firstRow[3:]
4946
}
5047

5148
// Check if language is valid.

parser/internal/ordered_list_item.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ func (*OrderedListItemParser) Match(tokens []*tokenizer.Token) (ast.Node, int) {
1616
indent := 0
1717
consumedTokens := 0
1818

19-
// Handle both new-style consolidated spaces and legacy individual spaces
19+
// Handle both new-style consolidated spaces and legacy individual spaces.
20+
spaceScan:
2021
for i, token := range matchedTokens {
21-
if token.Type == tokenizer.Space {
22+
switch token.Type {
23+
case tokenizer.Space:
2224
indent++
2325
consumedTokens = i + 1
24-
} else if token.Type == tokenizer.MultipleSpaces {
25-
indent = len(token.Value) // Count the actual spaces in the consolidated token
26+
case tokenizer.MultipleSpaces:
27+
indent = len(token.Value) // Count the actual spaces in the consolidated token.
2628
consumedTokens = i + 1
27-
} else {
28-
break
29+
default:
30+
break spaceScan
2931
}
3032
}
3133

parser/internal/parser.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
// Package internal contains parser implementations used by gomark.
12
package internal
23

34
import (
45
"github.com/usememos/gomark/ast"
56
"github.com/usememos/gomark/parser/tokenizer"
67
)
78

8-
// ParseInline parses inline tokens into AST nodes
9+
// ParseInline parses inline tokens into AST nodes.
910
func ParseInline(tokens []*tokenizer.Token) ([]ast.Node, error) {
1011
return ParseInlineWithParsers(tokens, getDefaultInlineParsers())
1112
}
1213

13-
// ParseInlineWithParsers parses inline tokens using the provided parsers
14+
// ParseInlineWithParsers parses inline tokens using the provided parsers.
1415
func ParseInlineWithParsers(tokens []*tokenizer.Token, inlineParsers []InlineParser) ([]ast.Node, error) {
1516
nodes := []ast.Node{}
1617
for len(tokens) > 0 {
@@ -33,12 +34,12 @@ func ParseInlineWithParsers(tokens []*tokenizer.Token, inlineParsers []InlinePar
3334
return mergeTextNodes(nodes), nil
3435
}
3536

36-
// ParseBlock parses block tokens into AST nodes using default parsers
37+
// ParseBlock parses block tokens into AST nodes using default parsers.
3738
func ParseBlock(tokens []*tokenizer.Token) ([]ast.Node, error) {
3839
return ParseBlockWithParsers(tokens, getDefaultBlockParsers())
3940
}
4041

41-
// ParseBlockWithParsers parses block tokens using the provided parsers
42+
// ParseBlockWithParsers parses block tokens using the provided parsers.
4243
func ParseBlockWithParsers(tokens []*tokenizer.Token, blockParsers []BlockParser) ([]ast.Node, error) {
4344
// Set lookahead parsers for any paragraph parsers
4445
for _, parser := range blockParsers {
@@ -106,7 +107,7 @@ func ParseBlockWithParsers(tokens []*tokenizer.Token, blockParsers []BlockParser
106107
return nodes, nil
107108
}
108109

109-
// getDefaultInlineParsers returns the default set of inline parsers
110+
// getDefaultInlineParsers returns the default set of inline parsers.
110111
func getDefaultInlineParsers() []InlineParser {
111112
return []InlineParser{
112113
NewEscapingCharacterParser(),
@@ -131,7 +132,7 @@ func getDefaultInlineParsers() []InlineParser {
131132
}
132133
}
133134

134-
// getDefaultBlockParsers returns the default set of block parsers
135+
// getDefaultBlockParsers returns the default set of block parsers.
135136
func getDefaultBlockParsers() []BlockParser {
136137
paragraph := NewParagraphParser()
137138
parsers := []BlockParser{
@@ -151,7 +152,7 @@ func getDefaultBlockParsers() []BlockParser {
151152
return parsers
152153
}
153154

154-
// mergeTextNodes merges consecutive text nodes
155+
// mergeTextNodes merges consecutive text nodes.
155156
func mergeTextNodes(nodes []ast.Node) []ast.Node {
156157
if len(nodes) == 0 {
157158
return nodes
@@ -167,7 +168,7 @@ func mergeTextNodes(nodes []ast.Node) []ast.Node {
167168
return result
168169
}
169170

170-
// mergeListItemNodes merges list item nodes into list structures
171+
// mergeListItemNodes merges list item nodes into list structures.
171172
func mergeListItemNodes(nodes []ast.Node) []ast.Node {
172173
var result []ast.Node
173174
var stack []*ast.List
@@ -218,4 +219,4 @@ func mergeListItemNodes(nodes []ast.Node) []ast.Node {
218219
}
219220

220221
return result
221-
}
222+
}

0 commit comments

Comments
 (0)