gazelle

package
v1.202449.18 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 34 Imported by: 0

README

JavaScript/TypeScript BUILD file generation

This package automates the creation and maintenance of BUILD files for JavaScript and TypeScript, using rules_js and rules_ts. It is a Gazelle Language implementation.

Usage

This feature is included in the Aspect CLI, accessed with the configure command. It's also possible to build into your own Gazelle binary.

Rules

Generated targets include:

  • ts_project targets for source, tests, and custom targets and their ts/js/npm dependencies
  • npm_package targets for projects within a pnpm workspace
  • npm_link_all_packages for linking npm packages
Directives

See Aspect CLI Directives for a list of supported directives.

Documentation

Index

Constants

View Source
const (
	// Directive_TypeScriptExtension represents the directive that controls whether
	// this TypeScript generation is enabled or not. Sub-packages inherit this value.
	// Can be either "enabled" or "disabled". Defaults to "enabled".
	Directive_TypeScriptExtension = "js"
	// En/disable ts_proto_library generation
	Directive_TypeScriptProtoExtension = "js_proto"
	// En/disable ts_config generation
	Directive_TypeScriptConfigExtension = "js_tsconfig"
	// En/disable npm_package generation and when generate
	Directive_NpmPackageExtension = "js_npm_package"
	// Visibility of the primary library targets
	Directive_Visibility = "js_visibility"
	// The pnpm-lock.yaml file.
	Directive_Lockfile = "js_pnpm_lockfile"
	// The tsconfig.json file.
	Directive_TsconfigFile = "js_tsconfig_file"
	// Directive_IgnoreImports represents the directive that controls the
	// ignored dependencies from the generated targets.
	// Sub-packages extend this value.
	// Ignored imports may be file path globs.
	Directive_IgnoreImports = "js_ignore_imports"
	// Directive_Resolve represents a gazelle:resolve state which supports globs.
	Directive_Resolve = "js_resolve"
	// Directive_ValidateImportStatements represents the directive that controls
	// whether the TypeScript import statements should be validated.
	Directive_ValidateImportStatements = "js_validate_import_statements"
	// Directive_LibraryNamingConvention represents the directive that controls the
	// ts_project naming convention. It interpolates {dirname} with the
	// Bazel package name. E.g. if the Bazel package name is `foo`, setting this
	// to `{dirname}_my_lib` would render to `foo_my_lib`.
	Directive_LibraryNamingConvention = "js_project_naming_convention"
	// The target name for npm_package() rules. See npm_translate_lock(npm_package_target_name)
	Directive_NpmPackageNameConvention = "js_npm_package_target_name"
	// Directive_TestsNamingConvention represents the directive that controls the ts_project test
	// naming convention. See js_project_naming_convention for more info on
	// the package name interpolation.
	Directive_TestsNamingConvention = "js_tests_naming_convention"
	// Use of npm_package() vs js_library() for the linked npm package target
	Directive_PackageRuleKind = "js_package_rule_kind"
	// The glob for the main library files, excludes files matching Directive_TestFiles.
	Directive_LibraryFiles = "js_files"
	// The glob for test files.
	Directive_TestFiles = "js_test_files"

	// TODO(deprecated): remove - replaced with js_files [group]
	Directive_CustomTargetFiles = "js_custom_files"
	// TODO(deprecated): remove - replaced with js_test_files [group]
	Directive_CustomTargetTestFiles = "js_custom_test_files"
	// TODO(deprecated): remove - replaced with common generation_mode
	Directive_GenerationMode = "js_generation_mode"
)

Directives. Keep in sync with documentation in /docs/cli/help/directives.md

View Source
const (
	DefaultNpmLinkAllTargetName = "node_modules"
	TargetNameDirectoryVar      = "{dirname}"
	DefaultLibraryName          = TargetNameDirectoryVar
	DefaultTestsName            = TargetNameDirectoryVar + "_tests"

	// The default name for ts_proto_library rules, where {proto_library} is the name of the proto target
	ProtoNameVar            = "{proto_library}"
	DefaultProtoLibraryName = ProtoNameVar + "_ts"

	// The suffix added to the end of a target being wrapped in a package.
	PackageSrcSuffix = "_lib"

	// The default should align with the rules_js default npm_translate_lock(npm_package_target_name)
	DefaultNpmPackageTargetName = TargetNameDirectoryVar
)
View Source
const (
	// The filename (with any of the TS extensions) imported when importing a directory.
	IndexFileName      = "index"
	SlashIndexFileName = "/" + IndexFileName

	NpmPackageFilename = "package.json"

	DefaultRootTargetName = "root"

	MaxWorkerCount = 12
)
View Source
const (
	TsProjectKind         = "ts_project"
	TsProtoLibraryKind    = "ts_proto_library"
	JsLibraryKind         = "js_library"
	TsConfigKind          = "ts_config"
	NpmPackageKind        = "npm_package"
	NpmLinkAllKind        = "npm_link_all_packages"
	RulesJsRepositoryName = "aspect_rules_js"
	RulesTsRepositoryName = "aspect_rules_ts"
	NpmRepositoryName     = "npm"
)
View Source
const (
	Resolution_Error      = -1
	Resolution_None       = 0
	Resolution_NotFound   = 1
	Resolution_Package    = 2
	Resolution_Label      = 3
	Resolution_NativeNode = 4
	Resolution_Override   = 5
)
View Source
const LanguageName = "js"

Variables

View Source
var DefaultSourceGlobs = []*TargetGroup{
	&TargetGroup{
		name:           DefaultLibraryName,
		customSources:  []string{},
		defaultSources: []string{fmt.Sprintf("%s/**/*.{%s}", rootDirVar, strings.Join(defaultTypescriptFileExtensionsArray, ","))},
		testonly:       false,
	},
	&TargetGroup{
		name:           DefaultTestsName,
		customSources:  []string{},
		defaultSources: []string{fmt.Sprintf("%s/**/*.{spec,test}.{%s}", rootDirVar, strings.Join(defaultTypescriptFileExtensionsArray, ","))},
		testonly:       true,
	},
}

Functions

func NewLanguage

func NewLanguage() language.Language

NewLanguage initializes a new TypeScript that satisfies the language.Language interface. This is the entrypoint for the extension initialization.

Types

type ImportStatement

type ImportStatement struct {
	resolve.ImportSpec

	// The path of the file containing the import
	SourcePath string

	// The path as written in the import statement
	ImportPath string

	// If the import is optional and failure to resolve should not be an error
	Optional bool

	// If the import is explicitly for types, in which case prefer @types package
	// dependencies when types are shipped separately
	TypesOnly bool
}

ImportStatement represents an ImportSpec imported from a source file. Imports can be of any form (es6, cjs, amd, ...). Imports may be relative ot the source, absolute, workspace, named modules etc.

type JsGazelleConfig

type JsGazelleConfig struct {
	// contains filtered or unexported fields
}

JsGazelleConfig represents a config extension for a specific Bazel package.

func (*JsGazelleConfig) AddIgnoredImport

func (c *JsGazelleConfig) AddIgnoredImport(impGlob string)

Adds a dependency to the list of ignored dependencies for a given package. Adding an ignored dependency to a package also makes it ignored on a subpackage.

func (*JsGazelleConfig) AddResolve

func (c *JsGazelleConfig) AddResolve(imprt string, label *label.Label)

func (*JsGazelleConfig) GenerationEnabled

func (c *JsGazelleConfig) GenerationEnabled() bool

GenerationEnabled returns whether the extension is enabled or not.

func (*JsGazelleConfig) GetFileSourceTarget

func (c *JsGazelleConfig) GetFileSourceTarget(filePath, rootDir string) *TargetGroup

Determine if and which target the passed file belongs in.

func (*JsGazelleConfig) GetNpmPackageGenerationMode

func (c *JsGazelleConfig) GetNpmPackageGenerationMode() NpmPackageMode

func (*JsGazelleConfig) GetResolution

func (c *JsGazelleConfig) GetResolution(imprt string) *label.Label

func (*JsGazelleConfig) GetSourceTarget

func (c *JsGazelleConfig) GetSourceTarget(targetName string) *TargetGroup

func (*JsGazelleConfig) GetSourceTargets

func (c *JsGazelleConfig) GetSourceTargets() []*TargetGroup

Return a list of all source groups for this config, including primary library + tests. The list is the source of truth for the order of groups

func (*JsGazelleConfig) GetTsConfigGenerationEnabled

func (c *JsGazelleConfig) GetTsConfigGenerationEnabled() bool

If ts_config extension is enabled.

func (*JsGazelleConfig) GetTsconfigFile

func (c *JsGazelleConfig) GetTsconfigFile() string

func (*JsGazelleConfig) IsImportIgnored

func (c *JsGazelleConfig) IsImportIgnored(impt string) bool

Checks if a import is ignored in the given package or in one of the parent packages up to the workspace root.

func (*JsGazelleConfig) MapTargetName

func (c *JsGazelleConfig) MapTargetName(name string) string

func (*JsGazelleConfig) NewChild

func (c *JsGazelleConfig) NewChild(childPath string) *JsGazelleConfig

NewChild creates a new child JsGazelleConfig. It inherits desired values from the current JsGazelleConfig and sets itself as the parent to the child.

func (*JsGazelleConfig) PnpmLockfile

func (c *JsGazelleConfig) PnpmLockfile() string

func (*JsGazelleConfig) ProtoGenerationEnabled

func (c *JsGazelleConfig) ProtoGenerationEnabled() bool

If ts_proto_library extension is enabled.

func (*JsGazelleConfig) RenderNpmPackageTargetName

func (c *JsGazelleConfig) RenderNpmPackageTargetName(packageName string) string

func (*JsGazelleConfig) RenderSourceTargetName

func (c *JsGazelleConfig) RenderSourceTargetName(groupName, packageName string, hasPackageTarget bool) string

Returns the ts_project target name by performing all substitutions.

func (*JsGazelleConfig) RenderTsConfigName

func (c *JsGazelleConfig) RenderTsConfigName(tsconfigName string) string

func (*JsGazelleConfig) RenderTsProtoLibraryName

func (c *JsGazelleConfig) RenderTsProtoLibraryName(protoLibraryName string) string

func (*JsGazelleConfig) SetGenerationEnabled

func (c *JsGazelleConfig) SetGenerationEnabled(enabled bool)

SetGenerationEnabled sets whether the extension is enabled or not.

func (*JsGazelleConfig) SetLibraryNamingConvention

func (c *JsGazelleConfig) SetLibraryNamingConvention(libraryNamingConvention string)

SetLibraryNamingConvention sets the ts_project target naming convention.

func (*JsGazelleConfig) SetNpmPackageGenerationMode

func (c *JsGazelleConfig) SetNpmPackageGenerationMode(mode NpmPackageMode)

func (*JsGazelleConfig) SetNpmPackageNamingConvention

func (c *JsGazelleConfig) SetNpmPackageNamingConvention(testsNamingConvention string)

func (*JsGazelleConfig) SetPnpmLockfile

func (c *JsGazelleConfig) SetPnpmLockfile(pnpmLockPath string)

Set the pnpm-workspace.yaml file path.

func (*JsGazelleConfig) SetProtoGenerationEnabled

func (c *JsGazelleConfig) SetProtoGenerationEnabled(enabled bool)

func (*JsGazelleConfig) SetTestsNamingLibraryConvention

func (c *JsGazelleConfig) SetTestsNamingLibraryConvention(testsNamingConvention string)

SetTestsNamingLibraryConvention sets the ts_project test target naming convention.

func (*JsGazelleConfig) SetTsConfigGenerationEnabled

func (c *JsGazelleConfig) SetTsConfigGenerationEnabled(enabled bool)

func (*JsGazelleConfig) SetTsProtoLibraryNamingConvention

func (c *JsGazelleConfig) SetTsProtoLibraryNamingConvention(tsProtoLibraryName string)

func (*JsGazelleConfig) SetTsconfigFile

func (c *JsGazelleConfig) SetTsconfigFile(tsconfigName string)

Set the tsconfig.json file name

func (*JsGazelleConfig) SetValidateImportStatements

func (c *JsGazelleConfig) SetValidateImportStatements(mode ValidationMode)

SetValidateImportStatements sets the ValidationMode for TypeScript import statements. It throws an error if this is set multiple times, i.e. if the directive is specified multiple times in the Bazel workspace.

func (*JsGazelleConfig) SetVisibility

func (c *JsGazelleConfig) SetVisibility(groupName string, visLabels []string)

func (*JsGazelleConfig) ValidateImportStatements

func (c *JsGazelleConfig) ValidateImportStatements() ValidationMode

ValidateImportStatements returns whether the TypeScript import statements should be validated or not. If this option was not explicitly specified by the user, it defaults to true.

type LinkAllPackagesImports

type LinkAllPackagesImports struct{}

Npm link-all rule import data

type NpmPackageMode

type NpmPackageMode string
const (
	NpmPackageEnabledMode    NpmPackageMode = "enabled"
	NpmPackageDisabledMode   NpmPackageMode = "disabled"
	NpmPackageReferencedMode NpmPackageMode = "referenced"
)

type PackageTargetKind

type PackageTargetKind string
const (
	PackageTargetKind_Package PackageTargetKind = NpmPackageKind
	PackageTargetKind_Library PackageTargetKind = JsLibraryKind
)

type ResolutionType

type ResolutionType = int

type TargetGroup

type TargetGroup struct {
	// contains filtered or unexported fields
}

type TsPackageInfo

type TsPackageInfo struct {
	TsProjectInfo
	// contains filtered or unexported fields
}

type TsProjectInfo

type TsProjectInfo struct {
	// contains filtered or unexported fields
}

TsProject rule import data

func (*TsProjectInfo) AddImport

func (i *TsProjectInfo) AddImport(impt ImportStatement)

func (*TsProjectInfo) HasTsx

func (i *TsProjectInfo) HasTsx() bool

type ValidationMode

type ValidationMode int

ValidationMode represents what should happen when validation errors are found.

const (
	// ValidationError has gazelle produce an error when validation errors are found.
	ValidationError ValidationMode = iota
	// ValidationWarn has gazelle print warnings when validation errors are found.
	ValidationWarn
	// ValidationOff has gazelle swallow validation errors silently.
	ValidationOff
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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