Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/web-infra-dev/rslint
go 1.25.0

replace (
github.com/microsoft/typescript-go/shim/api => ./shim/api
github.com/microsoft/typescript-go/shim/api/encoder => ./shim/api/encoder
github.com/microsoft/typescript-go/shim/ast => ./shim/ast
github.com/microsoft/typescript-go/shim/bundled => ./shim/bundled
github.com/microsoft/typescript-go/shim/checker => ./shim/checker
Expand All @@ -18,13 +20,12 @@ replace (
github.com/microsoft/typescript-go/shim/vfs => ./shim/vfs
github.com/microsoft/typescript-go/shim/vfs/cachedvfs => ./shim/vfs/cachedvfs
github.com/microsoft/typescript-go/shim/vfs/osvfs => ./shim/vfs/osvfs
github.com/microsoft/typescript-go/shim/api => ./shim/api
github.com/microsoft/typescript-go/shim/api/encoder => ./shim/api/encoder
)

require (
github.com/bmatcuk/doublestar/v4 v4.9.1
github.com/fatih/color v1.18.0
github.com/microsoft/typescript-go/shim/api/encoder v0.0.0
github.com/microsoft/typescript-go/shim/ast v0.0.0
github.com/microsoft/typescript-go/shim/bundled v0.0.0
github.com/microsoft/typescript-go/shim/checker v0.0.0
Expand All @@ -40,8 +41,6 @@ require (
github.com/microsoft/typescript-go/shim/vfs v0.0.0
github.com/microsoft/typescript-go/shim/vfs/cachedvfs v0.0.0
github.com/microsoft/typescript-go/shim/vfs/osvfs v0.0.0
github.com/microsoft/typescript-go/shim/api v0.0.0
github.com/microsoft/typescript-go/shim/api/encoder v0.0.0
github.com/tailscale/hujson v0.0.0-20250605163823-992244df8c5a
golang.org/x/sync v0.16.0
golang.org/x/sys v0.35.0
Expand Down
1 change: 0 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ toolchain go1.25.0
use (
.
./internal/typescript-estree
./typescript-go
)
9 changes: 9 additions & 0 deletions internal/typescript-estree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal/typescript-estree/
├── converter/ # AST conversion logic - transforms TypeScript AST to ESTree
├── types/ # Type definitions for ESTree nodes
├── utils/ # Utility functions for AST manipulation
├── version/ # TypeScript version detection and compatibility checking
├── testutils/ # Testing utilities and helpers
└── README.md # This file
```
Expand Down Expand Up @@ -152,6 +153,14 @@ The `utils` package provides:
- Position and location utilities
- Common operations on nodes

### Version Package

The `version` package handles:
- TypeScript version detection and parsing
- Semantic version comparison
- Compatibility checking and warnings
- Version-specific feature flags

### Testutils Package

The `testutils` package contains:
Expand Down
5 changes: 4 additions & 1 deletion internal/typescript-estree/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/web-infra-dev/rslint/internal/typescript-estree

go 1.25.0

require github.com/microsoft/typescript-go/shim/ast v0.0.0
require (
github.com/Masterminds/semver/v3 v3.3.1
github.com/microsoft/typescript-go/shim/ast v0.0.0
)

require (
github.com/go-json-experiment/json v0.0.0-20250811204210-4789234c3ea1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/typescript-estree/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/go-json-experiment/json v0.0.0-20250811204210-4789234c3ea1 h1:KaJSg7Eaxs50Tw1m9QeR4Ua/If27AltydVhyZ184HEI=
github.com/go-json-experiment/json v0.0.0-20250811204210-4789234c3ea1/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
Expand Down
200 changes: 200 additions & 0 deletions internal/typescript-estree/version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# TypeScript Version Checking

This package provides TypeScript version detection and compatibility checking functionality, ported from [@typescript-eslint/typescript-estree](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-estree/src/version-check.ts).

## Overview

The version checking system ensures that the TypeScript ESTree parser is used with compatible TypeScript versions. It:

- Detects the current TypeScript version
- Compares against supported version ranges
- Issues warnings for unsupported versions
- Provides version feature flags for conditional functionality

## Usage

### Basic Version Checking

```go
import "github.com/web-infra-dev/rslint/internal/typescript-estree/version"

// Set the TypeScript version (typically called during parser initialization)
err := version.SetTypeScriptVersion("5.4.0")
if err != nil {
// Handle invalid version
}

// Check if a specific version is available
if version.IsVersionAtLeast("5.4") {
// Use 5.4+ features
}

// Get the current version
currentVersion := version.GetCurrentVersion()
fmt.Println("TypeScript version:", currentVersion)
```

### Using the Version Map

The package maintains a `TypeScriptVersionIsAtLeast` map for quick lookups:

```go
// Check from the pre-computed map
if version.TypeScriptVersionIsAtLeast["5.4"] {
// TypeScript 5.4 features are available
}

if version.TypeScriptVersionIsAtLeast["5.0"] {
// TypeScript 5.0 features are available
}
```

### Supported Versions

Get the list of explicitly supported versions:

```go
supportedVersions := version.GetSupportedVersions()
fmt.Println("Supported versions:", supportedVersions)
// Output: [4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7]
```

## Version Comparison Logic

The package uses semantic versioning with special handling for pre-release versions:

- **Stable versions**: Compared using standard semver (e.g., `5.4.0`, `5.4.5`)
- **RC versions**: Release candidates are checked (e.g., `5.4.1-rc` satisfies `>= 5.4`)
- **Beta versions**: Beta releases are checked (e.g., `5.4.0-beta` satisfies `>= 5.4`)

For a version check like `IsVersionAtLeast("5.4")`, the following constraint is used:
```
>= 5.4.0 || >= 5.4.1-rc || >= 5.4.0-beta
```

This ensures that stable, RC, and beta versions of the same minor version are all considered compatible.

## Warning System

When an unsupported TypeScript version is detected, the package automatically issues a warning:

```
WARNING: You are using TypeScript version 6.0.0 which is not explicitly supported.
The supported TypeScript versions are: [4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7]
Please consider upgrading to a supported version for the best experience.
```

Warnings are issued only once per program execution.

## Thread Safety

The version checking system is thread-safe and uses `sync.Once` to ensure initialization happens only once, even when called from multiple goroutines concurrently.

## Testing

The package includes comprehensive tests covering:

- Version setting and retrieval
- Version comparison logic
- Supported version detection
- Pre-release version handling
- Concurrent access
- Reset functionality (for testing)

Run tests with:

```bash
go test ./internal/typescript-estree/version/...
```

## Implementation Details

### Semantic Versioning

This package uses the [Masterminds/semver](https://github.com/Masterminds/semver) library for semantic version parsing and comparison, which provides:

- Full semver 2.0.0 compatibility
- Constraint checking
- Pre-release version support

### Version Detection Flow

1. **Initialization**: `SetTypeScriptVersion()` is called with the detected TypeScript version
2. **Parsing**: Version string is parsed into a semver object
3. **Comparison**: All supported versions are checked against the current version
4. **Caching**: Results are stored in `TypeScriptVersionIsAtLeast` map
5. **Warning**: If version is not in the supported list, a warning is issued

### Resetting (Testing Only)

For testing purposes, the package provides a `ResetVersionCheck()` function to clear the initialization state:

```go
version.ResetVersionCheck()
// Now you can call SetTypeScriptVersion() again
```

**Note**: This should only be used in tests, not in production code.

## Examples

### Conditional Feature Usage

```go
// Use TypeScript 5.4+ decorators if available
if version.IsVersionAtLeast("5.4") {
// Parse with decorator metadata support
} else {
// Use legacy decorator syntax
}

// Check for const type parameters (5.0+)
if version.IsVersionAtLeast("5.0") {
// Handle const type parameters
}
```

### Integration with Parser

```go
import (
"github.com/web-infra-dev/rslint/internal/typescript-estree/version"
"github.com/web-infra-dev/rslint/internal/typescript-estree/parser"
)

func InitializeParser(tsVersion string) error {
// Set the TypeScript version
if err := version.SetTypeScriptVersion(tsVersion); err != nil {
return fmt.Errorf("invalid TypeScript version: %w", err)
}

// Configure parser based on version capabilities
parserConfig := parser.Config{
SupportsDecorators: version.IsVersionAtLeast("5.0"),
SupportsConstTypeParams: version.IsVersionAtLeast("5.0"),
// ... other version-dependent features
}

return nil
}
```

## References

- [Original TypeScript ESTree version-check.ts](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/typescript-estree/src/version-check.ts)
- [TypeScript Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/overview.html)
- [Semantic Versioning 2.0.0](https://semver.org/)
- [Masterminds/semver Documentation](https://github.com/Masterminds/semver)

## Contributing

When adding support for new TypeScript versions:

1. Add the version to `SupportedVersions` slice in `version.go`
2. Add corresponding test cases in `version_test.go`
3. Document any version-specific features or breaking changes
4. Update this README with any new usage patterns

## License

This package is part of the rslint project and follows the same license. See the main [LICENSE](../../../LICENSE) file for details.
Loading
Loading