diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-05 17:18:23 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-05 18:00:34 +0700 |
| commit | 2a51183943b29f9011d2ffa439a1cce502ce36cb (patch) | |
| tree | d66c54b6aff94431f51c03089bca15a302ca9d49 | |
| parent | 346500242a3541bf9f7d8ce008dcf2ef6950e9a0 (diff) | |
| download | asciidoctor-go-2a51183943b29f9011d2ffa439a1cce502ce36cb.tar.xz | |
all: comply with linter recommendations
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | attribute_entry.go | 7 | ||||
| -rw-r--r-- | cell_format.go | 9 | ||||
| -rw-r--r-- | element.go | 71 | ||||
| -rw-r--r-- | element_table.go | 3 | ||||
| -rw-r--r-- | html_backend.go | 28 | ||||
| -rw-r--r-- | inline_parser.go | 12 | ||||
| -rw-r--r-- | parser.go | 68 | ||||
| -rw-r--r-- | section_counters.go | 7 | ||||
| -rw-r--r-- | table_parser.go | 5 |
10 files changed, 130 insertions, 84 deletions
@@ -6,9 +6,11 @@ all: test lint lint: - -revive ./... -fieldalignment ./... -shadow ./... + -golangci-lint run \ + --presets bugs,metalinter,performance,unused \ + ./... test: go test -coverprofile=cover.out ./... diff --git a/attribute_entry.go b/attribute_entry.go index c1d329f..98db699 100644 --- a/attribute_entry.go +++ b/attribute_entry.go @@ -22,11 +22,12 @@ func newAttributeEntry() AttributeEntry { } func (entry *AttributeEntry) apply(key, val string) { - if key[0] == '!' { + switch { + case key[0] == '!': delete(*entry, strings.TrimSpace(key[1:])) - } else if key[len(key)-1] == '!' { + case key[len(key)-1] == '!': delete(*entry, strings.TrimSpace(key[:len(key)-1])) - } else { + default: (*entry)[key] = val } } diff --git a/cell_format.go b/cell_format.go index a37ec88..054ac08 100644 --- a/cell_format.go +++ b/cell_format.go @@ -37,13 +37,14 @@ func parseCellFormat(raw string) (cf *cellFormat) { if len(raw) == 0 { return nil } - if raw[0] == '*' { + switch raw[0] { + case '*': cf.ndupCol = n x = 1 - } else if raw[0] == '+' { + case '+': cf.nspanCol = n x = 1 - } else if raw[0] == '.' { + case '.': cf.nspanCol = n n, raw = parseCellFormatDigits(raw[1:]) if n == 0 { @@ -56,7 +57,7 @@ func parseCellFormat(raw string) (cf *cellFormat) { } x = 1 cf.nspanRow = n - } else { + default: return nil } } @@ -101,7 +101,8 @@ func (el *element) getVideoSource() string { isVimeo = true } - if isYoutube { + switch { + case isYoutube: u.Scheme = `https` u.Host = `www.youtube.com` u.Path = `/embed/` + src @@ -143,7 +144,7 @@ func (el *element) getVideoSource() string { q = append(q, attrNameYoutubeLang+`=`+vstr) } - } else if isVimeo { + case isVimeo: u.Scheme = `https` u.Host = `player.vimeo.com` u.Path = `/video/` + src @@ -159,7 +160,7 @@ func (el *element) getVideoSource() string { fragment = `at=` + vstr } - } else { + default: for _, vstr = range el.options { switch vstr { case optNameAutoplay, optNameLoop: @@ -729,15 +730,16 @@ func (el *element) postParseParagraphAsQuote(lines [][]byte) bool { secondLastIdx = len(lines) - 2 for x, line = range lines[:len(lines)-1] { - if x == 0 { + switch x { + case 0: if x == secondLastIdx { el.Write(line[1 : len(line)-1]) } else { el.Write(line[1:]) } - } else if x == secondLastIdx { + case secondLastIdx: el.Write(line[:len(line)-1]) - } else { + default: el.Write(line) } el.WriteByte('\n') @@ -828,10 +830,8 @@ func (el *element) toHTML(doc *Document, w io.Writer) { label = href } } - } else { - if len(label) == 0 { - label = anchor.label - } + } else if len(label) == 0 { + label = anchor.label } fmt.Fprintf(w, `<a href="#%s">%s</a>`, href, label) @@ -851,13 +851,14 @@ func (el *element) toHTML(doc *Document, w io.Writer) { htmlWriteSection(doc, el, w) case elKindParagraph: - if el.isStyleAdmonition() { + switch { + case el.isStyleAdmonition(): htmlWriteBlockAdmonition(el, w) - } else if el.isStyleQuote() { + case el.isStyleQuote(): htmlWriteBlockQuote(el, w) - } else if el.isStyleVerse() { + case el.isStyleVerse(): htmlWriteBlockVerse(el, w) - } else { + default: htmlWriteParagraphBegin(el, w) } @@ -893,11 +894,12 @@ func (el *element) toHTML(doc *Document, w io.Writer) { label.Write(el.rawLabel.Bytes()) } - if el.isStyleQandA() { + switch { + case el.isStyleQandA(): format = _htmlListDescriptionItemQandABegin - } else if el.isStyleHorizontal() { + case el.isStyleHorizontal(): format = _htmlListDescriptionItemHorizontalBegin - } else { + default: format = _htmlListDescriptionItemBegin } fmt.Fprintf(w, format, label.String()) @@ -919,13 +921,14 @@ func (el *element) toHTML(doc *Document, w io.Writer) { htmlWriteBlockImage(doc, el, w) case elKindBlockOpen: - if el.isStyleAdmonition() { + switch { + case el.isStyleAdmonition(): htmlWriteBlockAdmonition(el, w) - } else if el.isStyleQuote() { + case el.isStyleQuote(): htmlWriteBlockQuote(el, w) - } else if el.isStyleVerse() { + case el.isStyleVerse(): htmlWriteBlockVerse(el, w) - } else { + default: htmlWriteBlockOpenBegin(el, w) } @@ -1054,13 +1057,14 @@ func (el *element) toHTML(doc *Document, w io.Writer) { fmt.Fprint(w, "\n</div>") case elKindParagraph: - if el.isStyleAdmonition() { + switch { + case el.isStyleAdmonition(): fmt.Fprint(w, _htmlAdmonitionEnd) - } else if el.isStyleQuote() { + case el.isStyleQuote(): htmlWriteBlockQuoteEnd(el, w) - } else if el.isStyleVerse() { + case el.isStyleVerse(): htmlWriteBlockVerseEnd(el, w) - } else { + default: fmt.Fprint(w, "</p>\n</div>") } @@ -1069,11 +1073,13 @@ func (el *element) toHTML(doc *Document, w io.Writer) { case elKindListDescriptionItem: var format string - if el.isStyleQandA() { + + switch { + case el.isStyleQandA(): format = "\n</li>" - } else if el.isStyleHorizontal() { + case el.isStyleHorizontal(): format = "\n</td>\n</tr>" - } else { + default: format = "\n</dd>" } fmt.Fprint(w, format) @@ -1093,13 +1099,14 @@ func (el *element) toHTML(doc *Document, w io.Writer) { } case elKindBlockOpen: - if el.isStyleAdmonition() { + switch { + case el.isStyleAdmonition(): fmt.Fprint(w, _htmlAdmonitionEnd) - } else if el.isStyleQuote() { + case el.isStyleQuote(): htmlWriteBlockQuoteEnd(el, w) - } else if el.isStyleVerse() { + case el.isStyleVerse(): htmlWriteBlockVerseEnd(el, w) - } else { + default: fmt.Fprint(w, "\n</div>\n</div>") } case elKindBlockExcerpts: diff --git a/element_table.go b/element_table.go index 3e9c07d..ad94e2a 100644 --- a/element_table.go +++ b/element_table.go @@ -174,8 +174,7 @@ func (table *elementTable) initializeClassAndStyles(ea *elementAttribute) { } } for _, k = range ea.options { - switch k { - case optNameAutowidth: + if k == optNameAutowidth { withWidth = true table.classes.add(classNameFitContent) diff --git a/html_backend.go b/html_backend.go index d2fafea..be17c2f 100644 --- a/html_backend.go +++ b/html_backend.go @@ -819,7 +819,8 @@ func htmlWriteBlockVideo(el *element, out io.Writer) { fmt.Fprintf(out, "\n<div class=%q>", attrValueContent) - if isYoutube { + switch { + case isYoutube: var ( optFullscreen string noFullscreen bool @@ -830,9 +831,9 @@ func htmlWriteBlockVideo(el *element, out io.Writer) { optFullscreen = ` allowfullscreen` } fmt.Fprintf(out, _htmlBlockVideoYoutube, width, height, src, optFullscreen) - } else if isVimeo { + case isVimeo: fmt.Fprintf(out, _htmlBlockVideoVimeo, width, height, src) - } else { + default: var ( optControls = ` controls` @@ -932,16 +933,17 @@ func htmlWriteFooter(doc *Document, out io.Writer) { // htmlWriteFootnote generate HTML content for footnote. // Each unique footnote will have its id, so it can be referenced at footer. func htmlWriteFootnote(el *element, out io.Writer) { - if len(el.ID) != 0 { + switch { + case len(el.ID) != 0: // The first footnote with explicit ID. fmt.Fprintf(out, `<sup class="footnote" id="_footnote_%s">[<a id="_footnoteref_%d" class="footnote" href="#_footnotedef_%d" title="View footnote.">%d</a>]</sup>`, el.ID, el.level, el.level, el.level) - } else if len(el.key) != 0 { + case len(el.key) != 0: // The first footnote without ID. fmt.Fprintf(out, `<sup class="footnote">[<a id="_footnoteref_%d" class="footnote" href="#_footnotedef_%d" title="View footnote.">%d</a>]</sup>`, el.level, el.level, el.level) - } else { + default: // The next footnote with same ID. fmt.Fprintf(out, `<sup class="footnoteref">[<a class="footnote" href="#_footnotedef_%d" title="View footnote.">%d</a>]</sup>`, el.level, el.level) @@ -1110,13 +1112,14 @@ func htmlWriteInlinePass(doc *Document, el *element, out io.Writer) { func htmlWriteListDescription(el *element, out io.Writer) { var openTag string - if el.isStyleQandA() { + switch { + case el.isStyleQandA() == true: htmlWriteBlockBegin(el, out, `qlist qanda`) openTag = "\n<ol>" - } else if el.isStyleHorizontal() { + case el.isStyleHorizontal() == true: htmlWriteBlockBegin(el, out, `hdlist`) openTag = "\n<table>" - } else { + default: htmlWriteBlockBegin(el, out, `dlist`) openTag = "\n<dl>" } @@ -1125,11 +1128,12 @@ func htmlWriteListDescription(el *element, out io.Writer) { } func htmlWriteListDescriptionEnd(el *element, out io.Writer) { - if el.isStyleQandA() { + switch { + case el.isStyleQandA() == true: fmt.Fprintf(out, "\n</ol>\n</div>") - } else if el.isStyleHorizontal() { + case el.isStyleHorizontal() == true: fmt.Fprintf(out, "\n</table>\n</div>") - } else { + default: fmt.Fprintf(out, "\n</dl>\n</div>") } } diff --git a/inline_parser.go b/inline_parser.go index 09aa5a1..2f18aaf 100644 --- a/inline_parser.go +++ b/inline_parser.go @@ -73,6 +73,12 @@ func (pi *inlineParser) do() { pi.prev = pi.c continue } + + // We use if-condition with "continue" to break and continue + // the for-loop, so it is not possible to use switch-case + // here. + // + //nolint:gocritic if pi.c == '+' { if pi.isEscaped { pi.escape() @@ -250,6 +256,12 @@ func (pi *inlineParser) do() { pi.escape() continue } + + // We use if-condition with "continue" to break and + // continue the for-loop, so it is not possible + // to use switch-case here. + // + //nolint:gocritic if pi.nextc == '<' { if pi.parseCrossRef() { continue @@ -375,6 +375,12 @@ func applySubstitutions(doc *Document, content []byte) []byte { ) for x < len(raw) { c = raw[x] + + // We use if-condition with "continue" to break and continue + // the for-loop, so it is not possible to use switch-case + // here. + // + //nolint:gocritic if c == '{' { newRaw, ok = parseAttrRef(doc, raw, x) if ok { @@ -462,7 +468,7 @@ func generateID(doc *Document, str string) string { bout = append(bout, '_') } else if !ascii.IsAlpha(bout[0]) && bout[0] != '_' { bout = append(bout, '_') - copy(bout[1:], bout[:]) + copy(bout[1:], bout) bout[0] = '_' } @@ -471,17 +477,18 @@ func generateID(doc *Document, str string) string { func isAdmonition(line []byte) bool { var x int - if bytes.HasPrefix(line, []byte(admonitionCaution)) { + switch { + case bytes.HasPrefix(line, []byte(admonitionCaution)): x = len(admonitionCaution) - } else if bytes.HasPrefix(line, []byte(admonitionImportant)) { + case bytes.HasPrefix(line, []byte(admonitionImportant)): x = len(admonitionImportant) - } else if bytes.HasPrefix(line, []byte(admonitionNote)) { + case bytes.HasPrefix(line, []byte(admonitionNote)): x = len(admonitionNote) - } else if bytes.HasPrefix(line, []byte(admonitionTip)) { + case bytes.HasPrefix(line, []byte(admonitionTip)): x = len(admonitionTip) - } else if bytes.HasPrefix(line, []byte(admonitionWarning)) { + case bytes.HasPrefix(line, []byte(admonitionWarning)): x = len(admonitionWarning) - } else { + default: return false } if x >= len(line) { @@ -810,9 +817,10 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { } } - if line[0] == ':' { + switch line[0] { + case ':': kind = lineKindAttribute - } else if line[0] == '[' { + case '[': var ( newline = bytes.TrimRight(line, " \t") l = len(newline) @@ -838,26 +846,28 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { } } return lineKindAttributeElement, spaces, line - } else if line[0] == '=' { + case '=': var subs = bytes.Fields(line) - if bytes.Equal(subs[0], []byte(`==`)) { + switch string(subs[0]) { + case `==`: kind = elKindSectionL1 - } else if bytes.Equal(subs[0], []byte(`===`)) { + case `===`: kind = elKindSectionL2 - } else if bytes.Equal(subs[0], []byte(`====`)) { + case `====`: kind = elKindSectionL3 - } else if bytes.Equal(subs[0], []byte(`=====`)) { + case `=====`: kind = elKindSectionL4 - } else if bytes.Equal(subs[0], []byte(`======`)) { + case `======`: kind = elKindSectionL5 } - } else if line[0] == '.' { - if len(line) <= 1 { + case '.': + switch { + case len(line) <= 1: kind = lineKindText - } else if ascii.IsAlnum(line[1]) { + case ascii.IsAlnum(line[1]): kind = lineKindBlockTitle - } else { + default: x = 0 for ; x < len(line); x++ { if line[x] == '.' { @@ -869,7 +879,7 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { } } } - } else if line[0] == '*' || line[0] == '-' { + case '*', '-': if len(line) <= 1 { kind = lineKindText return kind, spaces, line @@ -904,13 +914,17 @@ func whatKindOfLine(line []byte) (kind int, spaces, got []byte) { kind = lineKindText } return kind, spaces, line - - } else if bytes.Equal(line, []byte(`+`)) { - kind = lineKindListContinue - } else if bytes.Equal(line, []byte(`----`)) { - kind = elKindBlockListing - } else if isLineDescriptionItem(line) { - kind = elKindListDescriptionItem + default: + switch string(line) { + case `+`: + kind = lineKindListContinue + case `----`: + kind = elKindBlockListing + default: + if isLineDescriptionItem(line) { + kind = elKindListDescriptionItem + } + } } return kind, spaces, line } diff --git a/section_counters.go b/section_counters.go index 0a5ba86..f51bd7a 100644 --- a/section_counters.go +++ b/section_counters.go @@ -16,16 +16,17 @@ type sectionCounters struct { } func (sec *sectionCounters) set(level int) *sectionCounters { - if level == sec.curr { + switch { + case level == sec.curr: sec.nums[level]++ - } else if level > sec.curr { + case level > sec.curr: // Check if the section level out of sequence. if level > sec.curr+1 { level = sec.curr + 1 } sec.nums[level] = 1 sec.curr = level - } else { + default: var x int for x = sec.curr; x > level; x-- { sec.nums[x] = 0 diff --git a/table_parser.go b/table_parser.go index 4f9c7f8..3a5f56e 100644 --- a/table_parser.go +++ b/table_parser.go @@ -79,6 +79,11 @@ func (pt *tableParser) toCells() { tokenTrim = strings.TrimSpace(token) l = len(tokenTrim) for { + // We use if-condition with "continue" to break and continue + // the for-loop, so it is not possible to use switch-case + // here. + // + //nolint:gocritic if c == '\n' { if l > 0 { cell.writeString(token) |
