codetext

package
v0.96.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package codetext separates comments from code in Go and JavaScript source for the contracts text rules. It is a lexer, not a parser: it walks the text left to right and tracks string, raw-string, rune and template literals, so a "*/*" Accept value or a "locales/*.json" glob stays code instead of opening a comment that blanks the rest of the file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Strip

func Strip(src string, script bool) string

Strip returns src with every // and /* */ comment removed. Newlines inside a comment are kept, so line numbers still match the original.

A string literal is code, but the code it holds may carry its own comments: a generator emits "// fontFaceCSS holds the @font-face rules", and a stylesheet in a raw string has /* knob notes */. Those are prose too, so they go, with two limits that keep the "*/*" class out. A /* inside a literal opens a comment only when its */ closes inside the same literal ("image/*" never does). A // inside a literal opens one only at the literal's start or after whitespace or an escaped newline, with whitespace after it, and runs to the end of the line or the literal, whichever comes first ("https://cdn" and '//cdn.example/x.js' stay).

script selects JavaScript literal rules: a backslash escapes inside a template literal, which it never does inside a Go raw string. A regular-expression literal is not tracked; a quote inside one at worst keeps the rest of that line as code.

Types

type Comment

type Comment struct {
	Line int
	Text string
}

Comment is one comment and the 1-indexed line it starts on. Text keeps its delimiters ("// …", "/* … */").

func Comments

func Comments(src string, script bool) []Comment

Comments returns every comment in src, in order. A comment inside a string literal is not one: it belongs to the emitted code, and a directive spelled there is documentation, never a live directive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL