Skip to content

Add an article about using SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION #2718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.

- <doc:Working-with-SwiftSyntax>
- <doc:Macro-Versioning>
- <doc:RawSyntaxValidation>
- <doc:Glossary>

### Tutorials
Expand Down
59 changes: 59 additions & 0 deletions Sources/SwiftSyntax/Documentation.docc/RawSyntaxValidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Raw Syntax Validation for Macro Testing

A guide to using `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION` in third-party Swift macro packages.

## Introduction

SwiftSyntax is designed with fault tolerance in mind. When developing macros, it's generally robust enough to handle
incorrect syntax trees without major issues. However, for developers who want to ensure they're using the SwiftSyntax
API as intended, there's an additional validation tool available: the `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION`
environment variable.

## What SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION does?

This environment variable enables extra validation in SwiftSyntax during runtime. When activated, it checks:

1. The correctness of each syntax node's child kind.
2. That tokens only use kinds specified in the CodeGeneration package's syntax tree layout.


## How to Use It

If you decide to use this feature for testing, you can enable it by setting the environment variable when running your
tests:

```sh
SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION=true swift test
```

## Example

Consider these two syntax examples:

Incorrect syntax:
```swift
let asyncSpecifier: TokenSyntax = "async"
```

Correct syntax:
```swift
let asyncSpecifier: TokenSyntax = .keyword(.async)
```

With raw syntax validation enabled, the incorrect syntax might produce an error like:

```
Fatal error: Error validating child at index 1 of typeEffectSpecifiers:
Expected token with one of [keyword('async')] but received identifier with text 'async'
```

This error helps identify that you're using an identifier where a keyword is expected. The correct syntax uses
`.keyword(.async)`, which properly specifies that 'async' should be treated as a keyword rather than an identifier.


## Conclusion

While `SWIFTSYNTAX_ENABLE_RAWSYNTAX_VALIDATION` isn't necessary for third-party macro packages, it can be a useful tool
during development and testing. It helps ensure you're using the SwiftSyntax API as intended and can catch subtle issues
that might affect formatting. However, remember that it comes with a performance cost and isn't recommended for use in
production environments.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.

- <doc:Working-with-SwiftSyntax>
- <doc:Macro-Versioning>
- <doc:RawSyntaxValidation>
- <doc:Glossary>

### Tutorials
Expand Down