srcscan

package
v0.8.92 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package srcscan prepares source text for guards that search it.

A source-scanning guard asserts that some token is present or absent in a file. Every one of them has the same hole: a comment EXPLAINING the token contains the token. Delete the real thing, leave the prose describing it, and the guard still passes — on the sentence about the code rather than the code.

That has now happened four times in one week, across DEJIMA_ROLE (three times) and a checksum step (once), the last written by someone who had read the write-up of the first three that same afternoon. Documenting it did not prevent it, which is the useful finding: the intervention has to be mechanical. Strip the comments once, here, and every guard that uses this stops being able to match prose.

The stripping errs toward removing too little, deliberately

The two mistakes are not symmetric. Strip too little and a guard matches a comment: a false positive, noisy, immediately obvious to whoever is looking at it. Strip too much and a guard stops seeing real code: it passes, silently, for the same reason the code is broken — which is the exact failure this package exists to end, reintroduced one layer down.

So StripLineComments removes only whole-line comments, never trailing ones, because deciding whether a mid-line marker is a comment or a literal needs a parser for the language in question. A trailing comment left in can only cost a false positive. Guessing wrong about a quoted '#' could cost a real match.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StripGoComments

func StripGoComments(src string) (out string, ok bool)

StripGoComments blanks every comment in Go source, preserving the length and line structure of the input so that offsets, line numbers and counts of anything else in the file are unchanged.

It is exact rather than heuristic: the Go scanner decides what a comment is, so a "//" inside a string literal survives and a comment containing code does not. On a lexical error the input is returned unchanged, with ok false — a guard should scan the raw file rather than a partially processed one, since a half-stripped file is the one state that could hide a real match.

func StripLineComments

func StripLineComments(src, marker string) string

StripLineComments blanks whole-line comments — lines whose first non-space character is marker — in text that is not Go: an embedded shell script, a YAML fixture, a Dockerfile. Pass "#" for those.

Whole-line only. A trailing comment after code stays, and that is the conservative choice: see the package comment for why the two errors are not symmetric. Line count and line lengths are preserved so a guard reporting a line number still points at the right place in the original.

Types

This section is empty.

Jump to

Keyboard shortcuts

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