diff --git a/blink/README.md b/blink/README.md index c9d8d8c349..80e7af2c28 100644 --- a/blink/README.md +++ b/blink/README.md @@ -13,7 +13,7 @@ CI keeps it current. | tool | rules | of ECS | |---|---:|---:| | [ECS](https://github.com/symplify/easy-coding-standard) (baseline) | 207 | 100% | -| blink | 211 | 101% | +| blink | 212 | 102% | ## Build diff --git a/blink/internal/fixer/fixer.go b/blink/internal/fixer/fixer.go index 4e843e666a..1ed15174da 100644 --- a/blink/internal/fixer/fixer.go +++ b/blink/internal/fixer/fixer.go @@ -41,6 +41,7 @@ var ecsSrcOnlyFixers = map[string]bool{ `Symplify\CodingStandard\Fixer\Annotation\RemovePropertyVariableNameDescriptionFixer`: true, `Symplify\CodingStandard\Fixer\Annotation\RemoveMethodNameDuplicateDescriptionFixer`: true, `Symplify\CodingStandard\Fixer\Annotation\RemoveEventSubscriberDescriptionFixer`: true, + `Symplify\CodingStandard\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer`: true, } // Fixer inspects a token stream and mutates it in place. It is the core contract diff --git a/blink/internal/fixer/rules/priorities.go b/blink/internal/fixer/rules/priorities.go index bc2f23efc4..531ab11233 100644 --- a/blink/internal/fixer/rules/priorities.go +++ b/blink/internal/fixer/rules/priorities.go @@ -14,6 +14,7 @@ var fixerPriority = map[string]int{ "FixParamNameTypoFixer": -37, "RemoveParamNameReferenceFixer": -37, "RemoveParamDescriptionDuplicateNameFixer": 7, + "RemoveSlashFromNativeConstantFixer": 0, "SwitchedTypeAndNameFixer": -37, "RemoveDeadParamFixer": -37, "RemoveDeadVarThisFixer": -37, diff --git a/blink/internal/fixer/rules/registry.go b/blink/internal/fixer/rules/registry.go index 11b2207dd4..ecdeb4f46d 100644 --- a/blink/internal/fixer/rules/registry.go +++ b/blink/internal/fixer/rules/registry.go @@ -228,6 +228,7 @@ func StructuralFixers() []fixer.Fixer { SingleBlankLineBeforeNamespace{}, BlankLineAfterNamespace{}, NoUnusedImports{}, + RemoveSlashFromNativeConstant{}, FullyQualifiedStrictTypes{}, SingleImportPerStatement{}, OrderedImports{}, diff --git a/blink/internal/fixer/rules/remove_slash_from_native_constant.go b/blink/internal/fixer/rules/remove_slash_from_native_constant.go new file mode 100644 index 0000000000..f802367db7 --- /dev/null +++ b/blink/internal/fixer/rules/remove_slash_from_native_constant.go @@ -0,0 +1,59 @@ +package rules + +import ( + "blink/internal/token" + "blink/internal/tokens" +) + +// Symplify: https://github.com/ecsphp/ecs-src/blob/main/packages/coding-standard/src/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer.php +// +// RemoveSlashFromNativeConstant strips the redundant leading backslash from the +// true, false and null constants (\true -> true). +type RemoveSlashFromNativeConstant struct{} + +func (RemoveSlashFromNativeConstant) Name() string { + return `Symplify\CodingStandard\Fixer\ConstantNotation\RemoveSlashFromNativeConstantFixer` +} + +func (RemoveSlashFromNativeConstant) SourceURL() string { + return "https://github.com/ecsphp/ecs-src/blob/main/packages/coding-standard/src/Fixer/ConstantNotation/RemoveSlashFromNativeConstantFixer.php" +} + +func (RemoveSlashFromNativeConstant) Fix(s *tokens.Stream) bool { + changed := false + for i := 0; i < s.Len(); i++ { + if s.At(i).Kind != token.Punct || s.At(i).Value != `\` { + continue + } + + next := nextSignificantIndex(s, i) + if next < 0 || s.At(next).Kind != token.Ident { + continue + } + + lower := lowerASCII(s.At(next).Value) + if lower != "true" && lower != "false" && lower != "null" { + continue + } + + // the keyword must stand alone, not be a namespace segment like + // "Foo\true" or "\true\Bar" + if prev := prevSignificantIndex(s, i); prev >= 0 { + pt := s.At(prev) + if pt.Kind == token.Ident || (pt.Kind == token.Punct && pt.Value == `\`) { + continue + } + } + if after := nextSignificantIndex(s, next); after >= 0 { + at := s.At(after) + if at.Kind == token.Punct && at.Value == `\` { + continue + } + } + + s.RemoveAt(i) + changed = true + i-- + } + return changed +} diff --git a/blink/internal/fixer/rules/remove_slash_from_native_constant_test.go b/blink/internal/fixer/rules/remove_slash_from_native_constant_test.go new file mode 100644 index 0000000000..6e47de5d57 --- /dev/null +++ b/blink/internal/fixer/rules/remove_slash_from_native_constant_test.go @@ -0,0 +1,33 @@ +package rules + +import "testing" + +func TestRemoveSlashFromNativeConstant(t *testing.T) { + t.Parallel() + f := RemoveSlashFromNativeConstant{} + + // leading backslash on true/false/null is stripped + got, changed := apply(t, f, "