package jscode

import "golazy.dev/lazycode/jscode"

Package jscode provides deliberately bounded JavaScript text edits as lazycode operations.

Exact imports and explicitly owned blocks can be planned together:

workspace, err := lazycode.Load(".", "app/js/app.js")
if err != nil {
	return err
}

_, err = workspace.Plan(
	jscode.Import(
		"app/js/app.js",
		`import "@hotwired/turbo";`,
	),
	jscode.ManagedBlock(
		"app/js/app.js",
		"seo",
		`initializeSEO();`,
	),
)
if err != nil {
	return err
}
if result.Changed() {
	fmt.Println("app/js/app.js would change")
}

Import manages exact single-line ES module imports. ManagedBlock owns only text between matching GoLazy markers. The package is intentionally not a general JavaScript parser or rewriter.

Functions

func EnsureImport

EnsureImport inserts an exact, single-line ES module import. Callers retain control over named/default import syntax by supplying the complete statement.

func EnsureImport(source []byte, statement string) ([]byte, bool, error)

func EnsureManagedBlock

EnsureManagedBlock creates or replaces body between matching GoLazy markers.

func EnsureManagedBlock(source []byte, id, body string) ([]byte, bool, error)

func EnsureSideEffectImport

EnsureSideEffectImport inserts a quoted side-effect import for module.

func EnsureSideEffectImport(source []byte, module string) ([]byte, bool, error)

func RemoveImport

RemoveImport removes an exact single-line import statement and reports whether it existed.

func RemoveImport(source []byte, statement string) ([]byte, bool, error)

func RemoveManagedBlock

RemoveManagedBlock removes a marked block and reports whether it existed.

func RemoveManagedBlock(source []byte, id string) ([]byte, bool, error)

Types

type EditFunc

EditFunc transforms JavaScript source and reports whether it changed.

type EditFunc func([]byte) ([]byte, bool, error)