remove unused packages
This commit is contained in:
parent
2c8c81a24b
commit
02d5c2ae60
@ -1,26 +0,0 @@
|
||||
# How does TypeScript formatting work?
|
||||
|
||||
To format code you need to have a formatting context and a `SourceFile`. The formatting context contains
|
||||
all user settings like tab size, newline character, etc.
|
||||
|
||||
The end result of formatting is represented by TextChange objects which hold the new string content, and
|
||||
the text to replace it with.
|
||||
|
||||
## Internals
|
||||
|
||||
Most of the exposed APIs internally are `Format*` and they all set up and configure `FormatSpan` which could be considered the root call for formatting. Span in this case refers to the range of
|
||||
the sourcefile which should be formatted.
|
||||
|
||||
The formatSpan then uses a scanner (either with or without JSX support) which starts at the highest
|
||||
node the covers the span of text and recurses down through the node's children.
|
||||
|
||||
As it recurses, `processNode` is called on the children setting the indentation is decided and passed
|
||||
through into each of that node's children.
|
||||
|
||||
The meat of formatting decisions is made via `processPair`, the pair here being the current node and the previous node. `processPair` which mutates the formatting context to represent the current place in the scanner and requests a set of rules which can be applied to the items via `createRulesMap`.
|
||||
|
||||
There are a lot of rules, which you can find in [rules.ts](./rules.ts) each one has a left and right reference to nodes or token ranges and note of what action should be applied by the formatter.
|
||||
|
||||
### Where is this used?
|
||||
|
||||
The formatter is used mainly from any language service operation that inserts or modifies code. The formatter is not exported publicly, and so all usage can only come through the language server.
|
||||
@ -1,185 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"context"
|
||||
"unicode/utf8"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/stringutil"
|
||||
)
|
||||
|
||||
type FormatRequestKind int
|
||||
|
||||
const (
|
||||
FormatRequestKindFormatDocument FormatRequestKind = iota
|
||||
FormatRequestKindFormatSelection
|
||||
FormatRequestKindFormatOnEnter
|
||||
FormatRequestKindFormatOnSemicolon
|
||||
FormatRequestKindFormatOnOpeningCurlyBrace
|
||||
FormatRequestKindFormatOnClosingCurlyBrace
|
||||
)
|
||||
|
||||
type formatContextKey int
|
||||
|
||||
const (
|
||||
formatOptionsKey formatContextKey = iota
|
||||
formatNewlineKey
|
||||
)
|
||||
|
||||
func WithFormatCodeSettings(ctx context.Context, options *FormatCodeSettings, newLine string) context.Context {
|
||||
ctx = context.WithValue(ctx, formatOptionsKey, options)
|
||||
ctx = context.WithValue(ctx, formatNewlineKey, newLine)
|
||||
// In strada, the rules map was both globally cached *and* cached into the context, for some reason. We skip that here and just use the global one.
|
||||
return ctx
|
||||
}
|
||||
|
||||
func GetFormatCodeSettingsFromContext(ctx context.Context) *FormatCodeSettings {
|
||||
opt := ctx.Value(formatOptionsKey).(*FormatCodeSettings)
|
||||
return opt
|
||||
}
|
||||
|
||||
func GetNewLineOrDefaultFromContext(ctx context.Context) string { // TODO: Move into broader LS - more than just the formatter uses the newline editor setting/host new line
|
||||
opt := GetFormatCodeSettingsFromContext(ctx)
|
||||
if opt != nil && len(opt.NewLineCharacter) > 0 {
|
||||
return opt.NewLineCharacter
|
||||
}
|
||||
host := ctx.Value(formatNewlineKey).(string)
|
||||
if len(host) > 0 {
|
||||
return host
|
||||
}
|
||||
return "\n"
|
||||
}
|
||||
|
||||
func FormatSpan(ctx context.Context, span core.TextRange, file *ast.SourceFile, kind FormatRequestKind) []core.TextChange {
|
||||
// find the smallest node that fully wraps the range and compute the initial indentation for the node
|
||||
enclosingNode := findEnclosingNode(span, file)
|
||||
opts := GetFormatCodeSettingsFromContext(ctx)
|
||||
|
||||
return newFormattingScanner(
|
||||
file.Text(),
|
||||
file.LanguageVariant,
|
||||
getScanStartPosition(enclosingNode, span, file),
|
||||
span.End(),
|
||||
newFormatSpanWorker(
|
||||
ctx,
|
||||
span,
|
||||
enclosingNode,
|
||||
GetIndentationForNode(enclosingNode, &span, file, opts),
|
||||
getOwnOrInheritedDelta(enclosingNode, opts, file),
|
||||
kind,
|
||||
prepareRangeContainsErrorFunction(file.Diagnostics(), span),
|
||||
file,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func FormatNodeGivenIndentation(ctx context.Context, node *ast.Node, file *ast.SourceFile, languageVariant core.LanguageVariant, initialIndentation int, delta int) []core.TextChange {
|
||||
textRange := core.NewTextRange(node.Pos(), node.End())
|
||||
return newFormattingScanner(
|
||||
file.Text(),
|
||||
languageVariant,
|
||||
textRange.Pos(),
|
||||
textRange.End(),
|
||||
newFormatSpanWorker(
|
||||
ctx,
|
||||
textRange,
|
||||
node,
|
||||
initialIndentation,
|
||||
delta,
|
||||
FormatRequestKindFormatSelection,
|
||||
func(core.TextRange) bool { return false }, // assume that node does not have any errors
|
||||
file,
|
||||
))
|
||||
}
|
||||
|
||||
func formatNodeLines(ctx context.Context, sourceFile *ast.SourceFile, node *ast.Node, requestKind FormatRequestKind) []core.TextChange {
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
tokenStart := scanner.GetTokenPosOfNode(node, sourceFile, false)
|
||||
lineStart := GetLineStartPositionForPosition(tokenStart, sourceFile)
|
||||
span := core.NewTextRange(lineStart, node.End())
|
||||
return FormatSpan(ctx, span, sourceFile, requestKind)
|
||||
}
|
||||
|
||||
func FormatDocument(ctx context.Context, sourceFile *ast.SourceFile) []core.TextChange {
|
||||
return FormatSpan(ctx, core.NewTextRange(0, sourceFile.End()), sourceFile, FormatRequestKindFormatDocument)
|
||||
}
|
||||
|
||||
func FormatSelection(ctx context.Context, sourceFile *ast.SourceFile, start int, end int) []core.TextChange {
|
||||
return FormatSpan(ctx, core.NewTextRange(GetLineStartPositionForPosition(start, sourceFile), end), sourceFile, FormatRequestKindFormatSelection)
|
||||
}
|
||||
|
||||
func FormatOnOpeningCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
|
||||
openingCurly := findImmediatelyPrecedingTokenOfKind(position, ast.KindOpenBraceToken, sourceFile)
|
||||
if openingCurly == nil {
|
||||
return nil
|
||||
}
|
||||
curlyBraceRange := openingCurly.Parent
|
||||
outermostNode := findOutermostNodeWithinListLevel(curlyBraceRange)
|
||||
/**
|
||||
* We limit the span to end at the opening curly to handle the case where
|
||||
* the brace matched to that just typed will be incorrect after further edits.
|
||||
* For example, we could type the opening curly for the following method
|
||||
* body without brace-matching activated:
|
||||
* ```
|
||||
* class C {
|
||||
* foo()
|
||||
* }
|
||||
* ```
|
||||
* and we wouldn't want to move the closing brace.
|
||||
*/
|
||||
textRange := core.NewTextRange(GetLineStartPositionForPosition(scanner.GetTokenPosOfNode(outermostNode, sourceFile, false), sourceFile), position)
|
||||
return FormatSpan(ctx, textRange, sourceFile, FormatRequestKindFormatOnOpeningCurlyBrace)
|
||||
}
|
||||
|
||||
func FormatOnClosingCurly(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
|
||||
precedingToken := findImmediatelyPrecedingTokenOfKind(position, ast.KindCloseBraceToken, sourceFile)
|
||||
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(precedingToken), FormatRequestKindFormatOnClosingCurlyBrace)
|
||||
}
|
||||
|
||||
func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
|
||||
semicolon := findImmediatelyPrecedingTokenOfKind(position, ast.KindSemicolonToken, sourceFile)
|
||||
return formatNodeLines(ctx, sourceFile, findOutermostNodeWithinListLevel(semicolon), FormatRequestKindFormatOnSemicolon)
|
||||
}
|
||||
|
||||
func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
|
||||
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
|
||||
if line == 0 {
|
||||
return nil
|
||||
}
|
||||
// get start position for the previous line
|
||||
startPos := int(scanner.GetECMALineStarts(sourceFile)[line-1])
|
||||
// After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
|
||||
// If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
|
||||
// trailing whitespaces. So the end of the formatting span should be the later one between:
|
||||
// 1. the end of the previous line
|
||||
// 2. the last non-whitespace character in the current line
|
||||
endOfFormatSpan := scanner.GetECMAEndLinePosition(sourceFile, line)
|
||||
for endOfFormatSpan > startPos {
|
||||
ch, s := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
|
||||
if s == 0 || stringutil.IsWhiteSpaceSingleLine(ch) { // on multibyte character keep backing up
|
||||
endOfFormatSpan--
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to
|
||||
// touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the
|
||||
// previous character before the end of format span is line break character as well.
|
||||
ch, _ := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
|
||||
if stringutil.IsLineBreak(ch) {
|
||||
endOfFormatSpan--
|
||||
}
|
||||
|
||||
span := core.NewTextRange(
|
||||
startPos,
|
||||
// end value is exclusive so add 1 to the result
|
||||
endOfFormatSpan+1,
|
||||
)
|
||||
|
||||
return FormatSpan(ctx, span, sourceFile, FormatRequestKindFormatOnEnter)
|
||||
}
|
||||
@ -1,114 +0,0 @@
|
||||
package format_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/format"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/printer"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/repo"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func applyBulkEdits(text string, edits []core.TextChange) string {
|
||||
b := strings.Builder{}
|
||||
b.Grow(len(text))
|
||||
lastEnd := 0
|
||||
for _, e := range edits {
|
||||
start := e.TextRange.Pos()
|
||||
if start != lastEnd {
|
||||
b.WriteString(text[lastEnd:e.TextRange.Pos()])
|
||||
}
|
||||
b.WriteString(e.NewText)
|
||||
|
||||
lastEnd = e.TextRange.End()
|
||||
}
|
||||
b.WriteString(text[lastEnd:])
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func TestFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("format checker.ts", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
|
||||
EditorSettings: format.EditorSettings{
|
||||
TabSize: 4,
|
||||
IndentSize: 4,
|
||||
BaseIndentSize: 4,
|
||||
NewLineCharacter: "\n",
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: format.IndentStyleSmart,
|
||||
TrimTrailingWhitespace: true,
|
||||
},
|
||||
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
|
||||
}, "\n")
|
||||
repo.SkipIfNoTypeScriptSubmodule(t)
|
||||
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
|
||||
fileContent, err := os.ReadFile(filePath)
|
||||
assert.NilError(t, err)
|
||||
text := string(fileContent)
|
||||
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
||||
FileName: "/checker.ts",
|
||||
Path: "/checker.ts",
|
||||
}, text, core.ScriptKindTS)
|
||||
edits := format.FormatDocument(ctx, sourceFile)
|
||||
newText := applyBulkEdits(text, edits)
|
||||
assert.Assert(t, len(newText) > 0)
|
||||
assert.Assert(t, text != newText)
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkFormat(b *testing.B) {
|
||||
ctx := format.WithFormatCodeSettings(b.Context(), &format.FormatCodeSettings{
|
||||
EditorSettings: format.EditorSettings{
|
||||
TabSize: 4,
|
||||
IndentSize: 4,
|
||||
BaseIndentSize: 4,
|
||||
NewLineCharacter: "\n",
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: format.IndentStyleSmart,
|
||||
TrimTrailingWhitespace: true,
|
||||
},
|
||||
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
|
||||
}, "\n")
|
||||
repo.SkipIfNoTypeScriptSubmodule(b)
|
||||
filePath := filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")
|
||||
fileContent, err := os.ReadFile(filePath)
|
||||
assert.NilError(b, err)
|
||||
text := string(fileContent)
|
||||
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
||||
FileName: "/checker.ts",
|
||||
Path: "/checker.ts",
|
||||
}, text, core.ScriptKindTS)
|
||||
|
||||
b.Run("format checker.ts", func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
edits := format.FormatDocument(ctx, sourceFile)
|
||||
newText := applyBulkEdits(text, edits)
|
||||
assert.Assert(b, len(newText) > 0)
|
||||
}
|
||||
})
|
||||
|
||||
b.Run("format checker.ts (no edit application)", func(b *testing.B) { // for comparison (how long does applying many edits take?)
|
||||
for b.Loop() {
|
||||
edits := format.FormatDocument(ctx, sourceFile)
|
||||
assert.Assert(b, len(edits) > 0)
|
||||
}
|
||||
})
|
||||
|
||||
p := printer.NewPrinter(printer.PrinterOptions{}, printer.PrintHandlers{}, printer.NewEmitContext())
|
||||
b.Run("pretty print checker.ts", func(b *testing.B) { // for comparison
|
||||
for b.Loop() {
|
||||
newText := p.EmitSourceFile(sourceFile)
|
||||
assert.Assert(b, len(newText) > 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
package format_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/format"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/parser"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestCommentFormatting(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("format comment issue reproduction", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := format.WithFormatCodeSettings(t.Context(), &format.FormatCodeSettings{
|
||||
EditorSettings: format.EditorSettings{
|
||||
TabSize: 4,
|
||||
IndentSize: 4,
|
||||
BaseIndentSize: 4,
|
||||
NewLineCharacter: "\n",
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: format.IndentStyleSmart,
|
||||
TrimTrailingWhitespace: true,
|
||||
},
|
||||
InsertSpaceBeforeTypeAnnotation: core.TSTrue,
|
||||
}, "\n")
|
||||
|
||||
// Original code that causes the bug
|
||||
originalText := `class C {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
async x() {}
|
||||
}`
|
||||
|
||||
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
||||
FileName: "/test.ts",
|
||||
Path: "/test.ts",
|
||||
}, originalText, core.ScriptKindTS)
|
||||
|
||||
// Apply formatting once
|
||||
edits := format.FormatDocument(ctx, sourceFile)
|
||||
firstFormatted := applyBulkEdits(originalText, edits)
|
||||
|
||||
// Check that the asterisk is not corrupted
|
||||
assert.Check(t, !contains(firstFormatted, "*/\n /"), "should not corrupt */ to /")
|
||||
assert.Check(t, contains(firstFormatted, "*/"), "should preserve */ token")
|
||||
assert.Check(t, contains(firstFormatted, "async"), "should preserve async keyword")
|
||||
|
||||
// Apply formatting a second time to test stability
|
||||
sourceFile2 := parser.ParseSourceFile(ast.SourceFileParseOptions{
|
||||
FileName: "/test.ts",
|
||||
Path: "/test.ts",
|
||||
}, firstFormatted, core.ScriptKindTS)
|
||||
|
||||
edits2 := format.FormatDocument(ctx, sourceFile2)
|
||||
secondFormatted := applyBulkEdits(firstFormatted, edits2)
|
||||
|
||||
// Check that second formatting doesn't introduce corruption
|
||||
assert.Check(t, !contains(secondFormatted, " sync x()"), "should not corrupt async to sync")
|
||||
assert.Check(t, contains(secondFormatted, "async"), "should preserve async keyword on second pass")
|
||||
})
|
||||
}
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
return len(substr) > 0 && strings.Contains(s, substr)
|
||||
}
|
||||
@ -1,215 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
)
|
||||
|
||||
type IndentStyle int
|
||||
|
||||
const (
|
||||
IndentStyleNone IndentStyle = iota
|
||||
IndentStyleBlock
|
||||
IndentStyleSmart
|
||||
)
|
||||
|
||||
type SemicolonPreference string
|
||||
|
||||
const (
|
||||
SemicolonPreferenceIgnore SemicolonPreference = "ignore"
|
||||
SemicolonPreferenceInsert SemicolonPreference = "insert"
|
||||
SemicolonPreferenceRemove SemicolonPreference = "remove"
|
||||
)
|
||||
|
||||
type EditorSettings struct {
|
||||
BaseIndentSize int
|
||||
IndentSize int
|
||||
TabSize int
|
||||
NewLineCharacter string
|
||||
ConvertTabsToSpaces bool
|
||||
IndentStyle IndentStyle
|
||||
TrimTrailingWhitespace bool
|
||||
}
|
||||
|
||||
type FormatCodeSettings struct {
|
||||
EditorSettings
|
||||
InsertSpaceAfterCommaDelimiter core.Tristate
|
||||
InsertSpaceAfterSemicolonInForStatements core.Tristate
|
||||
InsertSpaceBeforeAndAfterBinaryOperators core.Tristate
|
||||
InsertSpaceAfterConstructor core.Tristate
|
||||
InsertSpaceAfterKeywordsInControlFlowStatements core.Tristate
|
||||
InsertSpaceAfterFunctionKeywordForAnonymousFunctions core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingEmptyBraces core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces core.Tristate
|
||||
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces core.Tristate
|
||||
InsertSpaceAfterTypeAssertion core.Tristate
|
||||
InsertSpaceBeforeFunctionParenthesis core.Tristate
|
||||
PlaceOpenBraceOnNewLineForFunctions core.Tristate
|
||||
PlaceOpenBraceOnNewLineForControlBlocks core.Tristate
|
||||
InsertSpaceBeforeTypeAnnotation core.Tristate
|
||||
IndentMultiLineObjectLiteralBeginningOnBlankLine core.Tristate
|
||||
Semicolons SemicolonPreference
|
||||
IndentSwitchCase core.Tristate
|
||||
}
|
||||
|
||||
func GetDefaultFormatCodeSettings(newLineCharacter string) *FormatCodeSettings {
|
||||
return &FormatCodeSettings{
|
||||
EditorSettings: EditorSettings{
|
||||
IndentSize: 4,
|
||||
TabSize: 4,
|
||||
NewLineCharacter: newLineCharacter,
|
||||
ConvertTabsToSpaces: true,
|
||||
IndentStyle: IndentStyleSmart,
|
||||
TrimTrailingWhitespace: true,
|
||||
},
|
||||
InsertSpaceAfterConstructor: core.TSFalse,
|
||||
InsertSpaceAfterCommaDelimiter: core.TSTrue,
|
||||
InsertSpaceAfterSemicolonInForStatements: core.TSTrue,
|
||||
InsertSpaceBeforeAndAfterBinaryOperators: core.TSTrue,
|
||||
InsertSpaceAfterKeywordsInControlFlowStatements: core.TSTrue,
|
||||
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: core.TSFalse,
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: core.TSFalse,
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: core.TSFalse,
|
||||
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: core.TSTrue,
|
||||
InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: core.TSFalse,
|
||||
InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: core.TSFalse,
|
||||
InsertSpaceBeforeFunctionParenthesis: core.TSFalse,
|
||||
PlaceOpenBraceOnNewLineForFunctions: core.TSFalse,
|
||||
PlaceOpenBraceOnNewLineForControlBlocks: core.TSFalse,
|
||||
Semicolons: SemicolonPreferenceIgnore,
|
||||
IndentSwitchCase: core.TSTrue,
|
||||
}
|
||||
}
|
||||
|
||||
type formattingContext struct {
|
||||
currentTokenSpan TextRangeWithKind
|
||||
nextTokenSpan TextRangeWithKind
|
||||
contextNode *ast.Node
|
||||
currentTokenParent *ast.Node
|
||||
nextTokenParent *ast.Node
|
||||
|
||||
contextNodeAllOnSameLine core.Tristate
|
||||
nextNodeAllOnSameLine core.Tristate
|
||||
tokensAreOnSameLine core.Tristate
|
||||
contextNodeBlockIsOnOneLine core.Tristate
|
||||
nextNodeBlockIsOnOneLine core.Tristate
|
||||
|
||||
SourceFile *ast.SourceFile
|
||||
FormattingRequestKind FormatRequestKind
|
||||
Options *FormatCodeSettings
|
||||
|
||||
scanner *scanner.Scanner
|
||||
}
|
||||
|
||||
func NewFormattingContext(file *ast.SourceFile, kind FormatRequestKind, options *FormatCodeSettings) *formattingContext {
|
||||
res := &formattingContext{
|
||||
SourceFile: file,
|
||||
FormattingRequestKind: kind,
|
||||
Options: options,
|
||||
scanner: scanner.NewScanner(),
|
||||
}
|
||||
res.scanner.SetText(file.Text())
|
||||
res.scanner.SetSkipTrivia(true)
|
||||
return res
|
||||
}
|
||||
|
||||
func (this *formattingContext) UpdateContext(cur TextRangeWithKind, curParent *ast.Node, next TextRangeWithKind, nextParent *ast.Node, commonParent *ast.Node) {
|
||||
if curParent == nil {
|
||||
panic("nil current range node parent in update context")
|
||||
}
|
||||
if nextParent == nil {
|
||||
panic("nil next range node parent in update context")
|
||||
}
|
||||
if commonParent == nil {
|
||||
panic("nil common parent node in update context")
|
||||
}
|
||||
this.currentTokenSpan = cur
|
||||
this.currentTokenParent = curParent
|
||||
this.nextTokenSpan = next
|
||||
this.nextTokenParent = nextParent
|
||||
this.contextNode = commonParent
|
||||
|
||||
// drop cached results
|
||||
this.contextNodeAllOnSameLine = core.TSUnknown
|
||||
this.nextNodeAllOnSameLine = core.TSUnknown
|
||||
this.tokensAreOnSameLine = core.TSUnknown
|
||||
this.contextNodeBlockIsOnOneLine = core.TSUnknown
|
||||
this.nextNodeBlockIsOnOneLine = core.TSUnknown
|
||||
}
|
||||
|
||||
func (this *formattingContext) rangeIsOnOneLine(node core.TextRange) core.Tristate {
|
||||
if rangeIsOnOneLine(node, this.SourceFile) {
|
||||
return core.TSTrue
|
||||
}
|
||||
return core.TSFalse
|
||||
}
|
||||
|
||||
func (this *formattingContext) nodeIsOnOneLine(node *ast.Node) core.Tristate {
|
||||
return this.rangeIsOnOneLine(withTokenStart(node, this.SourceFile))
|
||||
}
|
||||
|
||||
func withTokenStart(loc *ast.Node, file *ast.SourceFile) core.TextRange {
|
||||
startPos := scanner.GetTokenPosOfNode(loc, file, false)
|
||||
return core.NewTextRange(startPos, loc.End())
|
||||
}
|
||||
|
||||
func (this *formattingContext) blockIsOnOneLine(node *ast.Node) core.Tristate {
|
||||
// In strada, this relies on token child manifesting - we just use the scanner here,
|
||||
// so this will have a differing performance profile. Is this OK? Needs profiling to know.
|
||||
this.scanner.ResetPos(node.Pos())
|
||||
end := node.End()
|
||||
firstOpenBrace := -1
|
||||
lastCloseBrace := -1
|
||||
for this.scanner.TokenEnd() < end {
|
||||
// tokenStart instead of tokenfullstart to skip trivia
|
||||
if firstOpenBrace == -1 && this.scanner.Token() == ast.KindOpenBraceToken {
|
||||
firstOpenBrace = this.scanner.TokenStart()
|
||||
} else if this.scanner.Token() == ast.KindCloseBraceToken {
|
||||
lastCloseBrace = this.scanner.TokenStart()
|
||||
}
|
||||
this.scanner.Scan()
|
||||
}
|
||||
if firstOpenBrace != -1 && lastCloseBrace != -1 {
|
||||
return this.rangeIsOnOneLine(core.NewTextRange(firstOpenBrace, lastCloseBrace))
|
||||
}
|
||||
return core.TSFalse
|
||||
}
|
||||
|
||||
func (this *formattingContext) ContextNodeAllOnSameLine() bool {
|
||||
if this.contextNodeAllOnSameLine == core.TSUnknown {
|
||||
this.contextNodeAllOnSameLine = this.nodeIsOnOneLine(this.contextNode)
|
||||
}
|
||||
return this.contextNodeAllOnSameLine == core.TSTrue
|
||||
}
|
||||
|
||||
func (this *formattingContext) NextNodeAllOnSameLine() bool {
|
||||
if this.nextNodeAllOnSameLine == core.TSUnknown {
|
||||
this.nextNodeAllOnSameLine = this.nodeIsOnOneLine(this.nextTokenParent)
|
||||
}
|
||||
return this.nextNodeAllOnSameLine == core.TSTrue
|
||||
}
|
||||
|
||||
func (this *formattingContext) TokensAreOnSameLine() bool {
|
||||
if this.tokensAreOnSameLine == core.TSUnknown {
|
||||
this.tokensAreOnSameLine = this.rangeIsOnOneLine(core.NewTextRange(this.currentTokenSpan.Loc.Pos(), this.nextTokenSpan.Loc.End()))
|
||||
}
|
||||
return this.tokensAreOnSameLine == core.TSTrue
|
||||
}
|
||||
|
||||
func (this *formattingContext) ContextNodeBlockIsOnOneLine() bool {
|
||||
if this.contextNodeBlockIsOnOneLine == core.TSUnknown {
|
||||
this.contextNodeBlockIsOnOneLine = this.blockIsOnOneLine(this.contextNode)
|
||||
}
|
||||
return this.contextNodeBlockIsOnOneLine == core.TSTrue
|
||||
}
|
||||
|
||||
func (this *formattingContext) NextNodeBlockIsOnOneLine() bool {
|
||||
if this.nextNodeBlockIsOnOneLine == core.TSUnknown {
|
||||
this.nextNodeBlockIsOnOneLine = this.blockIsOnOneLine(this.nextTokenParent)
|
||||
}
|
||||
return this.nextNodeBlockIsOnOneLine == core.TSTrue
|
||||
}
|
||||
@ -1,559 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"unicode/utf8"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/stringutil"
|
||||
)
|
||||
|
||||
func GetIndentationForNode(n *ast.Node, ignoreActualIndentationRange *core.TextRange, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
startline, startpos := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
|
||||
return getIndentationForNodeWorker(n, startline, startpos, ignoreActualIndentationRange /*indentationDelta*/, 0, sourceFile /*isNextChild*/, false, options)
|
||||
}
|
||||
|
||||
func getIndentationForNodeWorker(
|
||||
current *ast.Node,
|
||||
currentStartLine int,
|
||||
currentStartCharacter int,
|
||||
ignoreActualIndentationRange *core.TextRange,
|
||||
indentationDelta int,
|
||||
sourceFile *ast.SourceFile,
|
||||
isNextChild bool,
|
||||
options *FormatCodeSettings,
|
||||
) int {
|
||||
parent := current.Parent
|
||||
|
||||
// Walk up the tree and collect indentation for parent-child node pairs. Indentation is not added if
|
||||
// * parent and child nodes start on the same line, or
|
||||
// * parent is an IfStatement and child starts on the same line as an 'else clause'.
|
||||
for parent != nil {
|
||||
useActualIndentation := true
|
||||
if ignoreActualIndentationRange != nil {
|
||||
start := scanner.GetTokenPosOfNode(current, sourceFile, false)
|
||||
useActualIndentation = start < ignoreActualIndentationRange.Pos() || start > ignoreActualIndentationRange.End()
|
||||
}
|
||||
|
||||
containingListOrParentStartLine, containingListOrParentStartCharacter := getContainingListOrParentStart(parent, current, sourceFile)
|
||||
parentAndChildShareLine := containingListOrParentStartLine == currentStartLine ||
|
||||
childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStartLine, sourceFile)
|
||||
|
||||
if useActualIndentation {
|
||||
// check if current node is a list item - if yes, take indentation from it
|
||||
var firstListChild *ast.Node
|
||||
containerList := GetContainingList(current, sourceFile)
|
||||
if containerList != nil {
|
||||
firstListChild = core.FirstOrNil(containerList.Nodes)
|
||||
}
|
||||
// A list indents its children if the children begin on a later line than the list itself:
|
||||
//
|
||||
// f1( L0 - List start
|
||||
// { L1 - First child start: indented, along with all other children
|
||||
// prop: 0
|
||||
// },
|
||||
// {
|
||||
// prop: 1
|
||||
// }
|
||||
// )
|
||||
//
|
||||
// f2({ L0 - List start and first child start: children are not indented.
|
||||
// prop: 0 Object properties are indented only one level, because the list
|
||||
// }, { itself contributes nothing.
|
||||
// prop: 1 L3 - The indentation of the second object literal is best understood by
|
||||
// }) looking at the relationship between the list and *first* list item.
|
||||
listLine, _ := getStartLineAndCharacterForNode(firstListChild, sourceFile)
|
||||
listIndentsChild := firstListChild != nil && listLine > containingListOrParentStartLine
|
||||
actualIndentation := getActualIndentationForListItem(current, sourceFile, options, listIndentsChild)
|
||||
if actualIndentation != -1 {
|
||||
return actualIndentation + indentationDelta
|
||||
}
|
||||
|
||||
// try to fetch actual indentation for current node from source text
|
||||
actualIndentation = getActualIndentationForNode(current, parent, currentStartLine, currentStartCharacter, parentAndChildShareLine, sourceFile, options)
|
||||
if actualIndentation != -1 {
|
||||
return actualIndentation + indentationDelta
|
||||
}
|
||||
}
|
||||
|
||||
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
|
||||
if ShouldIndentChildNode(options, parent, current, sourceFile, isNextChild) && !parentAndChildShareLine {
|
||||
indentationDelta += options.IndentSize
|
||||
}
|
||||
|
||||
// In our AST, a call argument's `parent` is the call-expression, not the argument list.
|
||||
// We would like to increase indentation based on the relationship between an argument and its argument-list,
|
||||
// so we spoof the starting position of the (parent) call-expression to match the (non-parent) argument-list.
|
||||
// But, the spoofed start-value could then cause a problem when comparing the start position of the call-expression
|
||||
// to *its* parent (in the case of an iife, an expression statement), adding an extra level of indentation.
|
||||
//
|
||||
// Instead, when at an argument, we unspoof the starting position of the enclosing call expression
|
||||
// *after* applying indentation for the argument.
|
||||
|
||||
useTrueStart := isArgumentAndStartLineOverlapsExpressionBeingCalled(parent, current, currentStartLine, sourceFile)
|
||||
|
||||
current = parent
|
||||
parent = current.Parent
|
||||
|
||||
if useTrueStart {
|
||||
currentStartLine, currentStartCharacter = scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false))
|
||||
} else {
|
||||
currentStartLine = containingListOrParentStartLine
|
||||
currentStartCharacter = containingListOrParentStartCharacter
|
||||
}
|
||||
}
|
||||
|
||||
return indentationDelta + options.BaseIndentSize
|
||||
}
|
||||
|
||||
/*
|
||||
* Function returns -1 if actual indentation for node should not be used (i.e because node is nested expression)
|
||||
*/
|
||||
func getActualIndentationForNode(current *ast.Node, parent *ast.Node, cuurentLine int, currentChar int, parentAndChildShareLine bool, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
// actual indentation is used for statements\declarations if one of cases below is true:
|
||||
// - parent is SourceFile - by default immediate children of SourceFile are not indented except when user indents them manually
|
||||
// - parent and child are not on the same line
|
||||
useActualIndentation := (ast.IsDeclaration(current) || ast.IsStatementButNotDeclaration(current)) && (parent.Kind == ast.KindSourceFile || !parentAndChildShareLine)
|
||||
|
||||
if !useActualIndentation {
|
||||
return -1
|
||||
}
|
||||
|
||||
return findColumnForFirstNonWhitespaceCharacterInLine(cuurentLine, currentChar, sourceFile, options)
|
||||
}
|
||||
|
||||
func isArgumentAndStartLineOverlapsExpressionBeingCalled(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
|
||||
if !(ast.IsCallExpression(child) && slices.Contains(parent.AsCallExpression().Arguments.Nodes, child)) {
|
||||
return false
|
||||
}
|
||||
expressionOfCallExpressionEnd := parent.Expression().End()
|
||||
expressionOfCallExpressionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, expressionOfCallExpressionEnd)
|
||||
return expressionOfCallExpressionEndLine == childStartLine
|
||||
}
|
||||
|
||||
func getActualIndentationForListItem(node *ast.Node, sourceFile *ast.SourceFile, options *FormatCodeSettings, listIndentsChild bool) int {
|
||||
if node.Parent != nil && node.Parent.Kind == ast.KindVariableDeclarationList {
|
||||
// VariableDeclarationList has no wrapping tokens
|
||||
return -1
|
||||
}
|
||||
containingList := GetContainingList(node, sourceFile)
|
||||
if containingList != nil {
|
||||
index := core.FindIndex(containingList.Nodes, func(e *ast.Node) bool { return e == node })
|
||||
if index != -1 {
|
||||
result := deriveActualIndentationFromList(containingList, index, sourceFile, options)
|
||||
if result != -1 {
|
||||
return result
|
||||
}
|
||||
}
|
||||
delta := 0
|
||||
if listIndentsChild {
|
||||
delta = options.IndentSize
|
||||
}
|
||||
res := getActualIndentationForListStartLine(containingList, sourceFile, options)
|
||||
if res == -1 {
|
||||
return delta
|
||||
}
|
||||
return res + delta
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func getActualIndentationForListStartLine(list *ast.NodeList, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
if list == nil {
|
||||
return -1
|
||||
}
|
||||
line, char := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Loc.Pos())
|
||||
return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options)
|
||||
}
|
||||
|
||||
func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
debug.Assert(list != nil && index >= 0 && index < len(list.Nodes))
|
||||
|
||||
node := list.Nodes[index]
|
||||
|
||||
// walk toward the start of the list starting from current node and check if the line is the same for all items.
|
||||
// if end line for item [i - 1] differs from the start line for item [i] - find column of the first non-whitespace character on the line of item [i]
|
||||
|
||||
line, char := getStartLineAndCharacterForNode(node, sourceFile)
|
||||
|
||||
for i := index; i >= 0; i-- {
|
||||
if list.Nodes[i].Kind == ast.KindCommaToken {
|
||||
continue
|
||||
}
|
||||
// skip list items that ends on the same line with the current list element
|
||||
prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Nodes[i].End())
|
||||
if prevEndLine != line {
|
||||
return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options)
|
||||
}
|
||||
|
||||
line, char = getStartLineAndCharacterForNode(list.Nodes[i], sourceFile)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func findColumnForFirstNonWhitespaceCharacterInLine(line int, char int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, line, 0)
|
||||
return FindFirstNonWhitespaceColumn(lineStart, lineStart+char, sourceFile, options)
|
||||
}
|
||||
|
||||
func FindFirstNonWhitespaceColumn(startPos int, endPos int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
|
||||
_, col := findFirstNonWhitespaceCharacterAndColumn(startPos, endPos, sourceFile, options)
|
||||
return col
|
||||
}
|
||||
|
||||
/**
|
||||
* Character is the actual index of the character since the beginning of the line.
|
||||
* Column - position of the character after expanding tabs to spaces.
|
||||
* "0\t2$"
|
||||
* value of 'character' for '$' is 3
|
||||
* value of 'column' for '$' is 6 (assuming that tab size is 4)
|
||||
*/
|
||||
func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFile *ast.SourceFile, options *FormatCodeSettings) (character int, column int) {
|
||||
character = 0
|
||||
column = 0
|
||||
text := sourceFile.Text()
|
||||
for pos := startPos; pos < endPos; pos++ {
|
||||
ch, size := utf8.DecodeRuneInString(text[pos:])
|
||||
if size == 0 && ch == utf8.RuneError {
|
||||
continue // multibyte character - TODO: recognize non-tab multicolumn characters? ideographic space?
|
||||
}
|
||||
if !stringutil.IsWhiteSpaceSingleLine(ch) {
|
||||
break
|
||||
}
|
||||
|
||||
if ch == '\t' {
|
||||
column += options.TabSize + (column % options.TabSize)
|
||||
} else {
|
||||
column++
|
||||
}
|
||||
|
||||
character++
|
||||
}
|
||||
return character, column
|
||||
}
|
||||
|
||||
func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
|
||||
if parent.Kind == ast.KindIfStatement && parent.AsIfStatement().ElseStatement == child {
|
||||
elseKeyword := astnav.FindPrecedingToken(sourceFile, child.Pos())
|
||||
debug.AssertIsDefined(elseKeyword)
|
||||
elseKeywordStartLine, _ := getStartLineAndCharacterForNode(elseKeyword, sourceFile)
|
||||
return elseKeywordStartLine == childStartLine
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getStartLineAndCharacterForNode(n *ast.Node, sourceFile *ast.SourceFile) (line int, character int) {
|
||||
return scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
|
||||
}
|
||||
|
||||
func GetContainingList(node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
|
||||
if node.Parent == nil {
|
||||
return nil
|
||||
}
|
||||
return getListByRange(scanner.GetTokenPosOfNode(node, sourceFile, false), node.End(), node.Parent, sourceFile)
|
||||
}
|
||||
|
||||
func getListByPosition(pos int, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
return getListByRange(pos, pos, node, sourceFile)
|
||||
}
|
||||
|
||||
func getListByRange(start int, end int, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
|
||||
r := core.NewTextRange(start, end)
|
||||
switch node.Kind {
|
||||
case ast.KindTypeReference:
|
||||
return getList(node.AsTypeReferenceNode().TypeArguments, r, node, sourceFile)
|
||||
case ast.KindObjectLiteralExpression:
|
||||
return getList(node.AsObjectLiteralExpression().Properties, r, node, sourceFile)
|
||||
case ast.KindArrayLiteralExpression:
|
||||
return getList(node.AsArrayLiteralExpression().Elements, r, node, sourceFile)
|
||||
case ast.KindTypeLiteral:
|
||||
return getList(node.AsTypeLiteralNode().Members, r, node, sourceFile)
|
||||
case ast.KindFunctionDeclaration,
|
||||
ast.KindFunctionExpression,
|
||||
ast.KindArrowFunction,
|
||||
ast.KindMethodDeclaration,
|
||||
ast.KindMethodSignature,
|
||||
ast.KindCallSignature,
|
||||
ast.KindConstructor,
|
||||
ast.KindConstructorType,
|
||||
ast.KindConstructSignature:
|
||||
tpl := getList(node.TypeParameterList(), r, node, sourceFile)
|
||||
if tpl != nil {
|
||||
return tpl
|
||||
}
|
||||
return getList(node.ParameterList(), r, node, sourceFile)
|
||||
case ast.KindGetAccessor:
|
||||
return getList(node.ParameterList(), r, node, sourceFile)
|
||||
case ast.KindClassDeclaration,
|
||||
ast.KindClassExpression,
|
||||
ast.KindInterfaceDeclaration,
|
||||
ast.KindTypeAliasDeclaration,
|
||||
ast.KindJSDocTemplateTag:
|
||||
return getList(node.TypeParameterList(), r, node, sourceFile)
|
||||
case ast.KindNewExpression, ast.KindCallExpression:
|
||||
l := getList(node.TypeArgumentList(), r, node, sourceFile)
|
||||
if l != nil {
|
||||
return l
|
||||
}
|
||||
return getList(node.ArgumentList(), r, node, sourceFile)
|
||||
case ast.KindVariableDeclarationList:
|
||||
return getList(node.AsVariableDeclarationList().Declarations, r, node, sourceFile)
|
||||
case ast.KindNamedImports:
|
||||
return getList(node.AsNamedImports().Elements, r, node, sourceFile)
|
||||
case ast.KindNamedExports:
|
||||
return getList(node.AsNamedExports().Elements, r, node, sourceFile)
|
||||
case ast.KindObjectBindingPattern, ast.KindArrayBindingPattern:
|
||||
return getList(node.AsBindingPattern().Elements, r, node, sourceFile)
|
||||
}
|
||||
return nil // TODO: should this be a panic? It isn't in strada.
|
||||
}
|
||||
|
||||
func getList(list *ast.NodeList, r core.TextRange, node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
|
||||
if list == nil {
|
||||
return nil
|
||||
}
|
||||
if r.ContainedBy(getVisualListRange(node, list.Loc, sourceFile)) {
|
||||
return list
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getVisualListRange(node *ast.Node, list core.TextRange, sourceFile *ast.SourceFile) core.TextRange {
|
||||
// In strada, this relied on the services .getChildren method, which manifested synthetic token nodes
|
||||
// _however_, the logic boils down to "find the child with the matching span and adjust its start to the
|
||||
// previous (possibly token) child's end and its end to the token start of the following element" - basically
|
||||
// expanding the range to encompass all the neighboring non-token trivia
|
||||
// Now, we perform that logic with the scanner instead
|
||||
prior := astnav.FindPrecedingToken(sourceFile, list.Pos())
|
||||
var priorEnd int
|
||||
if prior == nil {
|
||||
priorEnd = list.Pos()
|
||||
} else {
|
||||
priorEnd = prior.End()
|
||||
}
|
||||
next := astnav.FindNextToken(prior, node, sourceFile)
|
||||
var nextStart int
|
||||
if next == nil {
|
||||
nextStart = list.End()
|
||||
} else {
|
||||
nextStart = next.Pos()
|
||||
}
|
||||
return core.NewTextRange(priorEnd, nextStart)
|
||||
}
|
||||
|
||||
func getContainingListOrParentStart(parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile) (line int, character int) {
|
||||
containingList := GetContainingList(child, sourceFile)
|
||||
var startPos int
|
||||
if containingList != nil {
|
||||
startPos = containingList.Loc.Pos()
|
||||
} else {
|
||||
startPos = scanner.GetTokenPosOfNode(parent, sourceFile, false)
|
||||
}
|
||||
return scanner.GetECMALineAndCharacterOfPosition(sourceFile, startPos)
|
||||
}
|
||||
|
||||
func isControlFlowEndingStatement(kind ast.Kind, parentKind ast.Kind) bool {
|
||||
switch kind {
|
||||
case ast.KindReturnStatement, ast.KindThrowStatement, ast.KindContinueStatement, ast.KindBreakStatement:
|
||||
return parentKind != ast.KindBlock
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the parent node should indent the given child by an explicit rule.
|
||||
* @param isNextChild If true, we are judging indent of a hypothetical child *after* this one, not the current child.
|
||||
*/
|
||||
func ShouldIndentChildNode(settings *FormatCodeSettings, parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile, isNextChildArg ...bool) bool {
|
||||
isNextChild := false
|
||||
if len(isNextChildArg) > 0 {
|
||||
isNextChild = isNextChildArg[0]
|
||||
}
|
||||
|
||||
return NodeWillIndentChild(settings, parent, child, sourceFile, false) && !(isNextChild && child != nil && isControlFlowEndingStatement(child.Kind, parent.Kind))
|
||||
}
|
||||
|
||||
func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *ast.Node, sourceFile *ast.SourceFile, indentByDefault bool) bool {
|
||||
childKind := ast.KindUnknown
|
||||
if child != nil {
|
||||
childKind = child.Kind
|
||||
}
|
||||
|
||||
switch parent.Kind {
|
||||
case ast.KindExpressionStatement,
|
||||
ast.KindClassDeclaration,
|
||||
ast.KindClassExpression,
|
||||
ast.KindInterfaceDeclaration,
|
||||
ast.KindEnumDeclaration,
|
||||
ast.KindTypeAliasDeclaration,
|
||||
ast.KindArrayLiteralExpression,
|
||||
ast.KindBlock,
|
||||
ast.KindModuleBlock,
|
||||
ast.KindObjectLiteralExpression,
|
||||
ast.KindTypeLiteral,
|
||||
ast.KindMappedType,
|
||||
ast.KindTupleType,
|
||||
ast.KindParenthesizedExpression,
|
||||
ast.KindPropertyAccessExpression,
|
||||
ast.KindCallExpression,
|
||||
ast.KindNewExpression,
|
||||
ast.KindVariableStatement,
|
||||
ast.KindExportAssignment,
|
||||
ast.KindReturnStatement,
|
||||
ast.KindConditionalExpression,
|
||||
ast.KindArrayBindingPattern,
|
||||
ast.KindObjectBindingPattern,
|
||||
ast.KindJsxOpeningElement,
|
||||
ast.KindJsxOpeningFragment,
|
||||
ast.KindJsxSelfClosingElement,
|
||||
ast.KindJsxExpression,
|
||||
ast.KindMethodSignature,
|
||||
ast.KindCallSignature,
|
||||
ast.KindConstructSignature,
|
||||
ast.KindParameter,
|
||||
ast.KindFunctionType,
|
||||
ast.KindConstructorType,
|
||||
ast.KindParenthesizedType,
|
||||
ast.KindTaggedTemplateExpression,
|
||||
ast.KindAwaitExpression,
|
||||
ast.KindNamedExports,
|
||||
ast.KindNamedImports,
|
||||
ast.KindExportSpecifier,
|
||||
ast.KindImportSpecifier,
|
||||
ast.KindPropertyDeclaration,
|
||||
ast.KindCaseClause,
|
||||
ast.KindDefaultClause:
|
||||
return true
|
||||
case ast.KindCaseBlock:
|
||||
return settings.IndentSwitchCase.IsTrueOrUnknown()
|
||||
case ast.KindVariableDeclaration, ast.KindPropertyAssignment, ast.KindBinaryExpression:
|
||||
if settings.IndentMultiLineObjectLiteralBeginningOnBlankLine.IsFalseOrUnknown() && sourceFile != nil && childKind == ast.KindObjectLiteralExpression {
|
||||
return rangeIsOnOneLine(child.Loc, sourceFile)
|
||||
}
|
||||
if parent.Kind == ast.KindBinaryExpression && sourceFile != nil && childKind == ast.KindJsxElement {
|
||||
parentStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos()))
|
||||
childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos()))
|
||||
return parentStartLine != childStartLine
|
||||
}
|
||||
if parent.Kind != ast.KindBinaryExpression {
|
||||
return true
|
||||
}
|
||||
return indentByDefault
|
||||
case ast.KindDoStatement,
|
||||
ast.KindWhileStatement,
|
||||
ast.KindForInStatement,
|
||||
ast.KindForOfStatement,
|
||||
ast.KindForStatement,
|
||||
ast.KindIfStatement,
|
||||
ast.KindFunctionDeclaration,
|
||||
ast.KindFunctionExpression,
|
||||
ast.KindMethodDeclaration,
|
||||
ast.KindConstructor,
|
||||
ast.KindGetAccessor,
|
||||
ast.KindSetAccessor:
|
||||
return childKind != ast.KindBlock
|
||||
case ast.KindArrowFunction:
|
||||
if sourceFile != nil && childKind == ast.KindParenthesizedExpression {
|
||||
return rangeIsOnOneLine(child.Loc, sourceFile)
|
||||
}
|
||||
return childKind != ast.KindBlock
|
||||
case ast.KindExportDeclaration:
|
||||
return childKind != ast.KindNamedExports
|
||||
case ast.KindImportDeclaration:
|
||||
return childKind != ast.KindImportClause || (child.AsImportClause().NamedBindings != nil && child.AsImportClause().NamedBindings.Kind != ast.KindNamedImports)
|
||||
case ast.KindJsxElement:
|
||||
return childKind != ast.KindJsxClosingElement
|
||||
case ast.KindJsxFragment:
|
||||
return childKind != ast.KindJsxClosingFragment
|
||||
case ast.KindIntersectionType, ast.KindUnionType, ast.KindSatisfiesExpression:
|
||||
if childKind == ast.KindTypeLiteral || childKind == ast.KindTupleType || childKind == ast.KindMappedType {
|
||||
return false
|
||||
}
|
||||
return indentByDefault
|
||||
case ast.KindTryStatement:
|
||||
if childKind == ast.KindBlock {
|
||||
return false
|
||||
}
|
||||
return indentByDefault
|
||||
}
|
||||
|
||||
// No explicit rule for given nodes so the result will follow the default value argument
|
||||
return indentByDefault
|
||||
}
|
||||
|
||||
// A multiline conditional typically increases the indentation of its whenTrue and whenFalse children:
|
||||
//
|
||||
// condition
|
||||
//
|
||||
// ? whenTrue
|
||||
// : whenFalse;
|
||||
//
|
||||
// However, that indentation does not apply if the subexpressions themselves span multiple lines,
|
||||
// applying their own indentation:
|
||||
//
|
||||
// (() => {
|
||||
// return complexCalculationForCondition();
|
||||
// })() ? {
|
||||
//
|
||||
// whenTrue: 'multiline object literal'
|
||||
// } : (
|
||||
//
|
||||
// whenFalse('multiline parenthesized expression')
|
||||
//
|
||||
// );
|
||||
//
|
||||
// In these cases, we must discard the indentation increase that would otherwise be applied to the
|
||||
// whenTrue and whenFalse children to avoid double-indenting their contents. To identify this scenario,
|
||||
// we check for the whenTrue branch beginning on the line that the condition ends, and the whenFalse
|
||||
// branch beginning on the line that the whenTrue branch ends.
|
||||
func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
|
||||
if parent.Kind == ast.KindConditionalExpression && (child == parent.AsConditionalExpression().WhenTrue || child == parent.AsConditionalExpression().WhenFalse) {
|
||||
conditionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End())
|
||||
if child == parent.AsConditionalExpression().WhenTrue {
|
||||
return childStartLine == conditionEndLine
|
||||
} else {
|
||||
// On the whenFalse side, we have to look at the whenTrue side, because if that one was
|
||||
// indented, whenFalse must also be indented:
|
||||
//
|
||||
// const y = true
|
||||
// ? 1 : ( L1: whenTrue indented because it's on a new line
|
||||
// 0 L2: indented two stops, one because whenTrue was indented
|
||||
// ); and one because of the parentheses spanning multiple lines
|
||||
trueStartLine, _ := getStartLineAndCharacterForNode(parent.AsConditionalExpression().WhenTrue, sourceFile)
|
||||
trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
|
||||
return conditionEndLine == trueStartLine && trueEndLine == childStartLine
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func argumentStartsOnSameLineAsPreviousArgument(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
|
||||
if ast.IsCallExpression(parent) || ast.IsNewExpression(parent) {
|
||||
if len(parent.Arguments()) == 0 {
|
||||
return false
|
||||
}
|
||||
currentIndex := core.FindIndex(parent.Arguments(), func(n *ast.Node) bool { return n == child })
|
||||
if currentIndex == -1 {
|
||||
// If it's not one of the arguments, don't look past this
|
||||
return false
|
||||
}
|
||||
if currentIndex == 0 {
|
||||
return false // Can't look at previous node if first
|
||||
}
|
||||
|
||||
previousNode := parent.Arguments()[currentIndex-1]
|
||||
lineOfPreviousNode, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, previousNode.End())
|
||||
if childStartLine == lineOfPreviousNode {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
package format
|
||||
|
||||
import "efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
|
||||
type ruleImpl struct {
|
||||
debugName string
|
||||
context []contextPredicate
|
||||
action ruleAction
|
||||
flags ruleFlags
|
||||
}
|
||||
|
||||
func (r ruleImpl) Action() ruleAction {
|
||||
return r.action
|
||||
}
|
||||
|
||||
func (r ruleImpl) Context() []contextPredicate {
|
||||
return r.context
|
||||
}
|
||||
|
||||
func (r ruleImpl) Flags() ruleFlags {
|
||||
return r.flags
|
||||
}
|
||||
|
||||
func (r ruleImpl) String() string {
|
||||
return r.debugName
|
||||
}
|
||||
|
||||
type tokenRange struct {
|
||||
tokens []ast.Kind
|
||||
isSpecific bool
|
||||
}
|
||||
|
||||
type ruleSpec struct {
|
||||
leftTokenRange tokenRange
|
||||
rightTokenRange tokenRange
|
||||
rule *ruleImpl
|
||||
}
|
||||
|
||||
/**
|
||||
* A rule takes a two tokens (left/right) and a particular context
|
||||
* for which you're meant to look at them. You then declare what should the
|
||||
* whitespace annotation be between these tokens via the action param.
|
||||
*
|
||||
* @param debugName Name to print
|
||||
* @param left The left side of the comparison
|
||||
* @param right The right side of the comparison
|
||||
* @param context A set of filters to narrow down the space in which this formatter rule applies
|
||||
* @param action a declaration of the expected whitespace
|
||||
* @param flags whether the rule deletes a line or not, defaults to no-op
|
||||
*/
|
||||
func rule(debugName string, left any, right any, context []contextPredicate, action ruleAction, flags ...ruleFlags) ruleSpec {
|
||||
flag := ruleFlagsNone
|
||||
if len(flags) > 0 {
|
||||
flag = flags[0]
|
||||
}
|
||||
leftRange := toTokenRange(left)
|
||||
rightRange := toTokenRange(right)
|
||||
rule := &ruleImpl{
|
||||
debugName: debugName,
|
||||
context: context,
|
||||
action: action,
|
||||
flags: flag,
|
||||
}
|
||||
return ruleSpec{
|
||||
leftTokenRange: leftRange,
|
||||
rightTokenRange: rightRange,
|
||||
rule: rule,
|
||||
}
|
||||
}
|
||||
|
||||
func toTokenRange(e any) tokenRange {
|
||||
switch t := e.(type) {
|
||||
case ast.Kind:
|
||||
return tokenRange{isSpecific: true, tokens: []ast.Kind{t}}
|
||||
case []ast.Kind:
|
||||
return tokenRange{isSpecific: true, tokens: t}
|
||||
case tokenRange:
|
||||
return t
|
||||
}
|
||||
panic("Unknown argument type passed to toTokenRange - only ast.Kind, []ast.Kind, and tokenRange supported")
|
||||
}
|
||||
|
||||
type contextPredicate = func(ctx *formattingContext) bool
|
||||
|
||||
var anyContext = []contextPredicate{}
|
||||
|
||||
type ruleAction int
|
||||
|
||||
const (
|
||||
ruleActionNone ruleAction = 0
|
||||
ruleActionStopProcessingSpaceActions ruleAction = 1 << 0
|
||||
ruleActionStopProcessingTokenActions ruleAction = 1 << 1
|
||||
ruleActionInsertSpace ruleAction = 1 << 2
|
||||
ruleActionInsertNewLine ruleAction = 1 << 3
|
||||
ruleActionDeleteSpace ruleAction = 1 << 4
|
||||
ruleActionDeleteToken ruleAction = 1 << 5
|
||||
ruleActionInsertTrailingSemicolon ruleAction = 1 << 6
|
||||
|
||||
ruleActionStopAction ruleAction = ruleActionStopProcessingSpaceActions | ruleActionStopProcessingTokenActions
|
||||
ruleActionModifySpaceAction ruleAction = ruleActionInsertSpace | ruleActionInsertNewLine | ruleActionDeleteSpace
|
||||
ruleActionModifyTokenAction ruleAction = ruleActionDeleteToken | ruleActionInsertTrailingSemicolon
|
||||
)
|
||||
|
||||
type ruleFlags int
|
||||
|
||||
const (
|
||||
ruleFlagsNone ruleFlags = iota
|
||||
ruleFlagsCanDeleteNewLines
|
||||
)
|
||||
@ -1,644 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/lsutil"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
)
|
||||
|
||||
///
|
||||
/// Contexts
|
||||
///
|
||||
|
||||
type (
|
||||
optionSelector = func(options *FormatCodeSettings) core.Tristate
|
||||
anyOptionSelector[T comparable] = func(options *FormatCodeSettings) T
|
||||
)
|
||||
|
||||
func semicolonOption(options *FormatCodeSettings) SemicolonPreference { return options.Semicolons }
|
||||
func insertSpaceAfterCommaDelimiterOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterCommaDelimiter
|
||||
}
|
||||
|
||||
func insertSpaceAfterSemicolonInForStatementsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterSemicolonInForStatements
|
||||
}
|
||||
|
||||
func insertSpaceBeforeAndAfterBinaryOperatorsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceBeforeAndAfterBinaryOperators
|
||||
}
|
||||
|
||||
func insertSpaceAfterConstructorOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterConstructor
|
||||
}
|
||||
|
||||
func insertSpaceAfterKeywordsInControlFlowStatementsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterKeywordsInControlFlowStatements
|
||||
}
|
||||
|
||||
func insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterFunctionKeywordForAnonymousFunctions
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingEmptyBraces
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces
|
||||
}
|
||||
|
||||
func insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces
|
||||
}
|
||||
|
||||
func insertSpaceAfterTypeAssertionOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceAfterTypeAssertion
|
||||
}
|
||||
|
||||
func insertSpaceBeforeFunctionParenthesisOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceBeforeFunctionParenthesis
|
||||
}
|
||||
|
||||
func placeOpenBraceOnNewLineForFunctionsOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.PlaceOpenBraceOnNewLineForFunctions
|
||||
}
|
||||
|
||||
func placeOpenBraceOnNewLineForControlBlocksOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.PlaceOpenBraceOnNewLineForControlBlocks
|
||||
}
|
||||
|
||||
func insertSpaceBeforeTypeAnnotationOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.InsertSpaceBeforeTypeAnnotation
|
||||
}
|
||||
|
||||
func indentMultiLineObjectLiteralBeginningOnBlankLineOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.IndentMultiLineObjectLiteralBeginningOnBlankLine
|
||||
}
|
||||
|
||||
func indentSwitchCaseOption(options *FormatCodeSettings) core.Tristate {
|
||||
return options.IndentSwitchCase
|
||||
}
|
||||
|
||||
func optionEquals[T comparable](optionName anyOptionSelector[T], optionValue T) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return false
|
||||
}
|
||||
return optionName(context.Options) == optionValue
|
||||
}
|
||||
}
|
||||
|
||||
func isOptionEnabled(optionName optionSelector) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return false
|
||||
}
|
||||
return optionName(context.Options).IsTrue()
|
||||
}
|
||||
}
|
||||
|
||||
func isOptionDisabled(optionName optionSelector) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return true
|
||||
}
|
||||
return optionName(context.Options).IsFalse()
|
||||
}
|
||||
}
|
||||
|
||||
func isOptionDisabledOrUndefined(optionName optionSelector) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return true
|
||||
}
|
||||
return optionName(context.Options).IsFalseOrUnknown()
|
||||
}
|
||||
}
|
||||
|
||||
func isOptionDisabledOrUndefinedOrTokensOnSameLine(optionName optionSelector) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return true
|
||||
}
|
||||
return optionName(context.Options).IsFalseOrUnknown() || context.TokensAreOnSameLine()
|
||||
}
|
||||
}
|
||||
|
||||
func isOptionEnabledOrUndefined(optionName optionSelector) contextPredicate {
|
||||
return func(context *formattingContext) bool {
|
||||
if context.Options == nil {
|
||||
return true
|
||||
}
|
||||
return optionName(context.Options).IsTrueOrUnknown()
|
||||
}
|
||||
}
|
||||
|
||||
func isForContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindForStatement
|
||||
}
|
||||
|
||||
func isNotForContext(context *formattingContext) bool {
|
||||
return !isForContext(context)
|
||||
}
|
||||
|
||||
func isBinaryOpContext(context *formattingContext) bool {
|
||||
switch context.contextNode.Kind {
|
||||
case ast.KindBinaryExpression:
|
||||
return context.contextNode.AsBinaryExpression().OperatorToken.Kind != ast.KindCommaToken
|
||||
case ast.KindConditionalExpression,
|
||||
ast.KindConditionalType,
|
||||
ast.KindAsExpression,
|
||||
ast.KindExportSpecifier,
|
||||
ast.KindImportSpecifier,
|
||||
ast.KindTypePredicate,
|
||||
ast.KindUnionType,
|
||||
ast.KindIntersectionType,
|
||||
ast.KindSatisfiesExpression:
|
||||
return true
|
||||
|
||||
// equals in binding elements func foo([[x, y] = [1, 2]])
|
||||
case ast.KindBindingElement:
|
||||
// equals in type X = ...
|
||||
fallthrough
|
||||
case ast.KindTypeAliasDeclaration:
|
||||
// equal in import a = module('a');
|
||||
fallthrough
|
||||
case ast.KindImportEqualsDeclaration:
|
||||
// equal in export = 1
|
||||
fallthrough
|
||||
case ast.KindExportAssignment:
|
||||
// equal in let a = 0
|
||||
fallthrough
|
||||
case ast.KindVariableDeclaration:
|
||||
// equal in p = 0
|
||||
fallthrough
|
||||
case ast.KindParameter,
|
||||
ast.KindEnumMember,
|
||||
ast.KindPropertyDeclaration,
|
||||
ast.KindPropertySignature:
|
||||
return context.currentTokenSpan.Kind == ast.KindEqualsToken || context.nextTokenSpan.Kind == ast.KindEqualsToken
|
||||
// "in" keyword in for (let x in []) { }
|
||||
case ast.KindForInStatement:
|
||||
// "in" keyword in [P in keyof T] T[P]
|
||||
fallthrough
|
||||
case ast.KindTypeParameter:
|
||||
return context.currentTokenSpan.Kind == ast.KindInKeyword || context.nextTokenSpan.Kind == ast.KindInKeyword || context.currentTokenSpan.Kind == ast.KindEqualsToken || context.nextTokenSpan.Kind == ast.KindEqualsToken
|
||||
// Technically, "of" is not a binary operator, but format it the same way as "in"
|
||||
case ast.KindForOfStatement:
|
||||
return context.currentTokenSpan.Kind == ast.KindOfKeyword || context.nextTokenSpan.Kind == ast.KindOfKeyword
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isNotBinaryOpContext(context *formattingContext) bool {
|
||||
return !isBinaryOpContext(context)
|
||||
}
|
||||
|
||||
func isNotTypeAnnotationContext(context *formattingContext) bool {
|
||||
return !isTypeAnnotationContext(context)
|
||||
}
|
||||
|
||||
func isTypeAnnotationContext(context *formattingContext) bool {
|
||||
contextKind := context.contextNode.Kind
|
||||
return contextKind == ast.KindPropertyDeclaration ||
|
||||
contextKind == ast.KindPropertySignature ||
|
||||
contextKind == ast.KindParameter ||
|
||||
contextKind == ast.KindVariableDeclaration ||
|
||||
ast.IsFunctionLikeKind(contextKind)
|
||||
}
|
||||
|
||||
func isOptionalPropertyContext(context *formattingContext) bool {
|
||||
return ast.IsPropertyDeclaration(context.contextNode) && context.contextNode.AsPropertyDeclaration().PostfixToken != nil && context.contextNode.AsPropertyDeclaration().PostfixToken.Kind == ast.KindQuestionToken
|
||||
}
|
||||
|
||||
func isNonOptionalPropertyContext(context *formattingContext) bool {
|
||||
return !isOptionalPropertyContext(context)
|
||||
}
|
||||
|
||||
func isConditionalOperatorContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindConditionalExpression ||
|
||||
context.contextNode.Kind == ast.KindConditionalType
|
||||
}
|
||||
|
||||
func isSameLineTokenOrBeforeBlockContext(context *formattingContext) bool {
|
||||
return context.TokensAreOnSameLine() || isBeforeBlockContext(context)
|
||||
}
|
||||
|
||||
func isBraceWrappedContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindObjectBindingPattern ||
|
||||
context.contextNode.Kind == ast.KindMappedType ||
|
||||
isSingleLineBlockContext(context)
|
||||
}
|
||||
|
||||
// This check is done before an open brace in a control construct, a function, or a typescript block declaration
|
||||
func isBeforeMultilineBlockContext(context *formattingContext) bool {
|
||||
return isBeforeBlockContext(context) && !(context.NextNodeAllOnSameLine() || context.NextNodeBlockIsOnOneLine())
|
||||
}
|
||||
|
||||
func isMultilineBlockContext(context *formattingContext) bool {
|
||||
return isBlockContext(context) && !(context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine())
|
||||
}
|
||||
|
||||
func isSingleLineBlockContext(context *formattingContext) bool {
|
||||
return isBlockContext(context) && (context.ContextNodeAllOnSameLine() || context.ContextNodeBlockIsOnOneLine())
|
||||
}
|
||||
|
||||
func isBlockContext(context *formattingContext) bool {
|
||||
return nodeIsBlockContext(context.contextNode)
|
||||
}
|
||||
|
||||
func isBeforeBlockContext(context *formattingContext) bool {
|
||||
return nodeIsBlockContext(context.nextTokenParent)
|
||||
}
|
||||
|
||||
// IMPORTANT!!! This method must return true ONLY for nodes with open and close braces as immediate children
|
||||
func nodeIsBlockContext(node *ast.Node) bool {
|
||||
if nodeIsTypeScriptDeclWithBlockContext(node) {
|
||||
// This means we are in a context that looks like a block to the user, but in the grammar is actually not a node (it's a class, module, enum, object type literal, etc).
|
||||
return true
|
||||
}
|
||||
|
||||
switch node.Kind {
|
||||
case ast.KindBlock,
|
||||
ast.KindCaseBlock,
|
||||
ast.KindObjectLiteralExpression,
|
||||
ast.KindModuleBlock:
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isFunctionDeclContext(context *formattingContext) bool {
|
||||
switch context.contextNode.Kind {
|
||||
case ast.KindFunctionDeclaration,
|
||||
ast.KindMethodDeclaration,
|
||||
ast.KindMethodSignature:
|
||||
// case ast.KindMemberFunctionDeclaration:
|
||||
fallthrough
|
||||
case ast.KindGetAccessor,
|
||||
ast.KindSetAccessor:
|
||||
// case ast.KindMethodSignature:
|
||||
fallthrough
|
||||
case ast.KindCallSignature,
|
||||
ast.KindFunctionExpression,
|
||||
ast.KindConstructor,
|
||||
ast.KindArrowFunction:
|
||||
// case ast.KindConstructorDeclaration:
|
||||
// case ast.KindSimpleArrowFunctionExpression:
|
||||
// case ast.KindParenthesizedArrowFunctionExpression:
|
||||
fallthrough
|
||||
case ast.KindInterfaceDeclaration: // This one is not truly a function, but for formatting purposes, it acts just like one
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isNotFunctionDeclContext(context *formattingContext) bool {
|
||||
return !isFunctionDeclContext(context)
|
||||
}
|
||||
|
||||
func isFunctionDeclarationOrFunctionExpressionContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindFunctionDeclaration || context.contextNode.Kind == ast.KindFunctionExpression
|
||||
}
|
||||
|
||||
func isTypeScriptDeclWithBlockContext(context *formattingContext) bool {
|
||||
return nodeIsTypeScriptDeclWithBlockContext(context.contextNode)
|
||||
}
|
||||
|
||||
func nodeIsTypeScriptDeclWithBlockContext(node *ast.Node) bool {
|
||||
switch node.Kind {
|
||||
case ast.KindClassDeclaration,
|
||||
ast.KindClassExpression,
|
||||
ast.KindInterfaceDeclaration,
|
||||
ast.KindEnumDeclaration,
|
||||
ast.KindTypeLiteral,
|
||||
ast.KindModuleDeclaration,
|
||||
ast.KindExportDeclaration,
|
||||
ast.KindNamedExports,
|
||||
ast.KindImportDeclaration,
|
||||
ast.KindNamedImports:
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isAfterCodeBlockContext(context *formattingContext) bool {
|
||||
switch context.currentTokenParent.Kind {
|
||||
case ast.KindClassDeclaration,
|
||||
ast.KindModuleDeclaration,
|
||||
ast.KindEnumDeclaration,
|
||||
ast.KindCatchClause,
|
||||
ast.KindModuleBlock,
|
||||
ast.KindSwitchStatement:
|
||||
return true
|
||||
case ast.KindBlock:
|
||||
blockParent := context.currentTokenParent.Parent
|
||||
// In a codefix scenario, we can't rely on parents being set. So just always return true.
|
||||
if blockParent == nil || blockParent.Kind != ast.KindArrowFunction && blockParent.Kind != ast.KindFunctionExpression {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isControlDeclContext(context *formattingContext) bool {
|
||||
switch context.contextNode.Kind {
|
||||
case ast.KindIfStatement,
|
||||
ast.KindSwitchStatement,
|
||||
ast.KindForStatement,
|
||||
ast.KindForInStatement,
|
||||
ast.KindForOfStatement,
|
||||
ast.KindWhileStatement,
|
||||
ast.KindTryStatement,
|
||||
ast.KindDoStatement,
|
||||
ast.KindWithStatement:
|
||||
// TODO
|
||||
// case ast.KindElseClause:
|
||||
fallthrough
|
||||
case ast.KindCatchClause:
|
||||
return true
|
||||
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isObjectContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindObjectLiteralExpression
|
||||
}
|
||||
|
||||
func isFunctionCallContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindCallExpression
|
||||
}
|
||||
|
||||
func isNewContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindNewExpression
|
||||
}
|
||||
|
||||
func isFunctionCallOrNewContext(context *formattingContext) bool {
|
||||
return isFunctionCallContext(context) || isNewContext(context)
|
||||
}
|
||||
|
||||
func isPreviousTokenNotComma(context *formattingContext) bool {
|
||||
return context.currentTokenSpan.Kind != ast.KindCommaToken
|
||||
}
|
||||
|
||||
func isNextTokenNotCloseBracket(context *formattingContext) bool {
|
||||
return context.nextTokenSpan.Kind != ast.KindCloseBracketToken
|
||||
}
|
||||
|
||||
func isNextTokenNotCloseParen(context *formattingContext) bool {
|
||||
return context.nextTokenSpan.Kind != ast.KindCloseParenToken
|
||||
}
|
||||
|
||||
func isArrowFunctionContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindArrowFunction
|
||||
}
|
||||
|
||||
func isImportTypeContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindImportType
|
||||
}
|
||||
|
||||
func isNonJsxSameLineTokenContext(context *formattingContext) bool {
|
||||
return context.TokensAreOnSameLine() && context.contextNode.Kind != ast.KindJsxText
|
||||
}
|
||||
|
||||
func isNonJsxTextContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind != ast.KindJsxText
|
||||
}
|
||||
|
||||
func isNonJsxElementOrFragmentContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind != ast.KindJsxElement && context.contextNode.Kind != ast.KindJsxFragment
|
||||
}
|
||||
|
||||
func isJsxExpressionContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindJsxExpression || context.contextNode.Kind == ast.KindJsxSpreadAttribute
|
||||
}
|
||||
|
||||
func isNextTokenParentJsxAttribute(context *formattingContext) bool {
|
||||
return context.nextTokenParent.Kind == ast.KindJsxAttribute || (context.nextTokenParent.Kind == ast.KindJsxNamespacedName && context.nextTokenParent.Parent.Kind == ast.KindJsxAttribute)
|
||||
}
|
||||
|
||||
func isJsxAttributeContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindJsxAttribute
|
||||
}
|
||||
|
||||
func isNextTokenParentNotJsxNamespacedName(context *formattingContext) bool {
|
||||
return context.nextTokenParent.Kind != ast.KindJsxNamespacedName
|
||||
}
|
||||
|
||||
func isNextTokenParentJsxNamespacedName(context *formattingContext) bool {
|
||||
return context.nextTokenParent.Kind == ast.KindJsxNamespacedName
|
||||
}
|
||||
|
||||
func isJsxSelfClosingElementContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindJsxSelfClosingElement
|
||||
}
|
||||
|
||||
func isNotBeforeBlockInFunctionDeclarationContext(context *formattingContext) bool {
|
||||
return !isFunctionDeclContext(context) && !isBeforeBlockContext(context)
|
||||
}
|
||||
|
||||
func isEndOfDecoratorContextOnSameLine(context *formattingContext) bool {
|
||||
return context.TokensAreOnSameLine() &&
|
||||
ast.HasDecorators(context.contextNode) &&
|
||||
nodeIsInDecoratorContext(context.currentTokenParent) &&
|
||||
!nodeIsInDecoratorContext(context.nextTokenParent)
|
||||
}
|
||||
|
||||
func nodeIsInDecoratorContext(node *ast.Node) bool {
|
||||
for node != nil && ast.IsExpression(node) {
|
||||
node = node.Parent
|
||||
}
|
||||
return node != nil && node.Kind == ast.KindDecorator
|
||||
}
|
||||
|
||||
func isStartOfVariableDeclarationList(context *formattingContext) bool {
|
||||
return context.currentTokenParent.Kind == ast.KindVariableDeclarationList &&
|
||||
scanner.GetTokenPosOfNode(context.currentTokenParent, context.SourceFile, false) == context.currentTokenSpan.Loc.Pos()
|
||||
}
|
||||
|
||||
func isNotFormatOnEnter(context *formattingContext) bool {
|
||||
return context.FormattingRequestKind != FormatRequestKindFormatOnEnter
|
||||
}
|
||||
|
||||
func isModuleDeclContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindModuleDeclaration
|
||||
}
|
||||
|
||||
func isObjectTypeContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindTypeLiteral // && context.contextNode.parent.Kind != ast.KindInterfaceDeclaration;
|
||||
}
|
||||
|
||||
func isConstructorSignatureContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindConstructSignature
|
||||
}
|
||||
|
||||
func isTypeArgumentOrParameterOrAssertion(token TextRangeWithKind, parent *ast.Node) bool {
|
||||
if token.Kind != ast.KindLessThanToken && token.Kind != ast.KindGreaterThanToken {
|
||||
return false
|
||||
}
|
||||
switch parent.Kind {
|
||||
case ast.KindTypeReference,
|
||||
ast.KindTypeAssertionExpression,
|
||||
ast.KindTypeAliasDeclaration,
|
||||
ast.KindClassDeclaration,
|
||||
ast.KindClassExpression,
|
||||
ast.KindInterfaceDeclaration,
|
||||
ast.KindFunctionDeclaration,
|
||||
ast.KindFunctionExpression,
|
||||
ast.KindArrowFunction,
|
||||
ast.KindMethodDeclaration,
|
||||
ast.KindMethodSignature,
|
||||
ast.KindCallSignature,
|
||||
ast.KindConstructSignature,
|
||||
ast.KindCallExpression,
|
||||
ast.KindNewExpression,
|
||||
ast.KindExpressionWithTypeArguments:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isTypeArgumentOrParameterOrAssertionContext(context *formattingContext) bool {
|
||||
return isTypeArgumentOrParameterOrAssertion(context.currentTokenSpan, context.currentTokenParent) ||
|
||||
isTypeArgumentOrParameterOrAssertion(context.nextTokenSpan, context.nextTokenParent)
|
||||
}
|
||||
|
||||
func isTypeAssertionContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindTypeAssertionExpression
|
||||
}
|
||||
|
||||
func isNonTypeAssertionContext(context *formattingContext) bool {
|
||||
return !isTypeAssertionContext(context)
|
||||
}
|
||||
|
||||
func isVoidOpContext(context *formattingContext) bool {
|
||||
return context.currentTokenSpan.Kind == ast.KindVoidKeyword && context.currentTokenParent.Kind == ast.KindVoidExpression
|
||||
}
|
||||
|
||||
func isYieldOrYieldStarWithOperand(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindYieldExpression && context.contextNode.AsYieldExpression().Expression != nil
|
||||
}
|
||||
|
||||
func isNonNullAssertionContext(context *formattingContext) bool {
|
||||
return context.contextNode.Kind == ast.KindNonNullExpression
|
||||
}
|
||||
|
||||
func isNotStatementConditionContext(context *formattingContext) bool {
|
||||
return !isStatementConditionContext(context)
|
||||
}
|
||||
|
||||
func isStatementConditionContext(context *formattingContext) bool {
|
||||
switch context.contextNode.Kind {
|
||||
case ast.KindIfStatement,
|
||||
ast.KindForStatement,
|
||||
ast.KindForInStatement,
|
||||
ast.KindForOfStatement,
|
||||
ast.KindDoStatement,
|
||||
ast.KindWhileStatement:
|
||||
return true
|
||||
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isSemicolonDeletionContext(context *formattingContext) bool {
|
||||
nextTokenKind := context.nextTokenSpan.Kind
|
||||
nextTokenStart := context.nextTokenSpan.Loc.Pos()
|
||||
if ast.IsTrivia(nextTokenKind) {
|
||||
var nextRealToken *ast.Node
|
||||
if context.nextTokenParent == context.currentTokenParent {
|
||||
// !!! TODO: very different from strada, but strada's logic here is wonky - find the first ancestor without a parent? that's just the source file.
|
||||
nextRealToken = astnav.FindNextToken(context.nextTokenParent, context.SourceFile.AsNode(), context.SourceFile)
|
||||
} else {
|
||||
nextRealToken = lsutil.GetFirstToken(context.nextTokenParent, context.SourceFile)
|
||||
}
|
||||
|
||||
if nextRealToken == nil {
|
||||
return true
|
||||
}
|
||||
nextTokenKind = nextRealToken.Kind
|
||||
nextTokenStart = scanner.GetTokenPosOfNode(nextRealToken, context.SourceFile, false)
|
||||
}
|
||||
|
||||
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos())
|
||||
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, nextTokenStart)
|
||||
if startLine == endLine {
|
||||
return nextTokenKind == ast.KindCloseBraceToken || nextTokenKind == ast.KindEndOfFile
|
||||
}
|
||||
|
||||
if nextTokenKind == ast.KindSemicolonToken &&
|
||||
context.currentTokenSpan.Kind == ast.KindSemicolonToken {
|
||||
return true
|
||||
}
|
||||
|
||||
if nextTokenKind == ast.KindSemicolonClassElement ||
|
||||
nextTokenKind == ast.KindSemicolonToken {
|
||||
return false
|
||||
}
|
||||
|
||||
if context.contextNode.Kind == ast.KindInterfaceDeclaration ||
|
||||
context.contextNode.Kind == ast.KindTypeAliasDeclaration {
|
||||
// Can't remove semicolon after `foo`; it would parse as a method declaration:
|
||||
//
|
||||
// interface I {
|
||||
// foo;
|
||||
// () void
|
||||
// }
|
||||
return context.currentTokenParent.Kind != ast.KindPropertySignature ||
|
||||
context.currentTokenParent.Type() != nil ||
|
||||
nextTokenKind != ast.KindOpenParenToken
|
||||
}
|
||||
|
||||
if ast.IsPropertyDeclaration(context.currentTokenParent) {
|
||||
return context.currentTokenParent.Initializer() == nil
|
||||
}
|
||||
|
||||
return context.currentTokenParent.Kind != ast.KindForStatement &&
|
||||
context.currentTokenParent.Kind != ast.KindEmptyStatement &&
|
||||
context.currentTokenParent.Kind != ast.KindSemicolonClassElement &&
|
||||
nextTokenKind != ast.KindOpenBracketToken &&
|
||||
nextTokenKind != ast.KindOpenParenToken &&
|
||||
nextTokenKind != ast.KindPlusToken &&
|
||||
nextTokenKind != ast.KindMinusToken &&
|
||||
nextTokenKind != ast.KindSlashToken &&
|
||||
nextTokenKind != ast.KindRegularExpressionLiteral &&
|
||||
nextTokenKind != ast.KindCommaToken &&
|
||||
nextTokenKind != ast.KindTemplateExpression &&
|
||||
nextTokenKind != ast.KindTemplateHead &&
|
||||
nextTokenKind != ast.KindNoSubstitutionTemplateLiteral &&
|
||||
nextTokenKind != ast.KindDotToken
|
||||
}
|
||||
|
||||
func isSemicolonInsertionContext(context *formattingContext) bool {
|
||||
return lsutil.PositionIsASICandidate(context.currentTokenSpan.Loc.End(), context.currentTokenParent, context.SourceFile)
|
||||
}
|
||||
|
||||
func isNotPropertyAccessOnIntegerLiteral(context *formattingContext) bool {
|
||||
return !ast.IsPropertyAccessExpression(context.contextNode) ||
|
||||
!ast.IsNumericLiteral(context.contextNode.Expression()) ||
|
||||
strings.Contains(context.contextNode.Expression().Text(), ".")
|
||||
}
|
||||
@ -1,446 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
)
|
||||
|
||||
func getAllRules() []ruleSpec {
|
||||
allTokens := make([]ast.Kind, 0, ast.KindLastToken-ast.KindFirstToken+1)
|
||||
for token := ast.KindFirstToken; token <= ast.KindLastToken; token++ {
|
||||
allTokens = append(allTokens, token)
|
||||
}
|
||||
|
||||
anyTokenExcept := func(tokens ...ast.Kind) tokenRange {
|
||||
newTokens := make([]ast.Kind, 0, ast.KindLastToken-ast.KindFirstToken+1)
|
||||
for token := ast.KindFirstToken; token <= ast.KindLastToken; token++ {
|
||||
if slices.Contains(tokens, token) {
|
||||
continue
|
||||
}
|
||||
newTokens = append(newTokens, token)
|
||||
}
|
||||
return tokenRange{
|
||||
isSpecific: false,
|
||||
tokens: newTokens,
|
||||
}
|
||||
}
|
||||
|
||||
anyToken := tokenRange{
|
||||
isSpecific: false,
|
||||
tokens: allTokens,
|
||||
}
|
||||
|
||||
anyTokenIncludingMultilineComments := tokenRangeFromEx(allTokens, ast.KindMultiLineCommentTrivia)
|
||||
anyTokenIncludingEOF := tokenRangeFromEx(allTokens, ast.KindEndOfFile)
|
||||
keywords := tokenRangeFromRange(ast.KindFirstKeyword, ast.KindLastKeyword)
|
||||
binaryOperators := tokenRangeFromRange(ast.KindFirstBinaryOperator, ast.KindLastBinaryOperator)
|
||||
binaryKeywordOperators := []ast.Kind{
|
||||
ast.KindInKeyword,
|
||||
ast.KindInstanceOfKeyword,
|
||||
ast.KindOfKeyword,
|
||||
ast.KindAsKeyword,
|
||||
ast.KindIsKeyword,
|
||||
ast.KindSatisfiesKeyword,
|
||||
}
|
||||
unaryPrefixOperators := []ast.Kind{ast.KindPlusPlusToken, ast.KindMinusToken, ast.KindTildeToken, ast.KindExclamationToken}
|
||||
unaryPrefixExpressions := []ast.Kind{
|
||||
ast.KindNumericLiteral,
|
||||
ast.KindBigIntLiteral,
|
||||
ast.KindIdentifier,
|
||||
ast.KindOpenParenToken,
|
||||
ast.KindOpenBracketToken,
|
||||
ast.KindOpenBraceToken,
|
||||
ast.KindThisKeyword,
|
||||
ast.KindNewKeyword,
|
||||
}
|
||||
unaryPreincrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindOpenParenToken, ast.KindThisKeyword, ast.KindNewKeyword}
|
||||
unaryPostincrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken, ast.KindCloseBracketToken, ast.KindNewKeyword}
|
||||
unaryPredecrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindOpenParenToken, ast.KindThisKeyword, ast.KindNewKeyword}
|
||||
unaryPostdecrementExpressions := []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken, ast.KindCloseBracketToken, ast.KindNewKeyword}
|
||||
comments := []ast.Kind{ast.KindSingleLineCommentTrivia, ast.KindMultiLineCommentTrivia}
|
||||
typeKeywords := []ast.Kind{
|
||||
ast.KindAnyKeyword,
|
||||
ast.KindAssertsKeyword,
|
||||
ast.KindBigIntKeyword,
|
||||
ast.KindBooleanKeyword,
|
||||
ast.KindFalseKeyword,
|
||||
ast.KindInferKeyword,
|
||||
ast.KindKeyOfKeyword,
|
||||
ast.KindNeverKeyword,
|
||||
ast.KindNullKeyword,
|
||||
ast.KindNumberKeyword,
|
||||
ast.KindObjectKeyword,
|
||||
ast.KindReadonlyKeyword,
|
||||
ast.KindStringKeyword,
|
||||
ast.KindSymbolKeyword,
|
||||
ast.KindTypeOfKeyword,
|
||||
ast.KindTrueKeyword,
|
||||
ast.KindVoidKeyword,
|
||||
ast.KindUndefinedKeyword,
|
||||
ast.KindUniqueKeyword,
|
||||
ast.KindUnknownKeyword,
|
||||
}
|
||||
typeNames := append([]ast.Kind{ast.KindIdentifier}, typeKeywords...)
|
||||
|
||||
// Place a space before open brace in a function declaration
|
||||
// TypeScript: Function can have return types, which can be made of tons of different token kinds
|
||||
functionOpenBraceLeftTokenRange := anyTokenIncludingMultilineComments
|
||||
|
||||
// Place a space before open brace in a TypeScript declaration that has braces as children (class, module, enum, etc)
|
||||
typeScriptOpenBraceLeftTokenRange := tokenRangeFrom(ast.KindIdentifier, ast.KindGreaterThanToken, ast.KindMultiLineCommentTrivia, ast.KindClassKeyword, ast.KindExportKeyword, ast.KindImportKeyword)
|
||||
|
||||
// Place a space before open brace in a control flow construct
|
||||
controlOpenBraceLeftTokenRange := tokenRangeFrom(ast.KindCloseParenToken, ast.KindMultiLineCommentTrivia, ast.KindDoKeyword, ast.KindTryKeyword, ast.KindFinallyKeyword, ast.KindElseKeyword, ast.KindCatchKeyword)
|
||||
|
||||
// These rules are higher in priority than user-configurable
|
||||
highPriorityCommonRules := []ruleSpec{
|
||||
// Leave comments alone
|
||||
rule("IgnoreBeforeComment", anyToken, comments, anyContext, ruleActionStopProcessingSpaceActions),
|
||||
rule("IgnoreAfterLineComment", ast.KindSingleLineCommentTrivia, anyToken, anyContext, ruleActionStopProcessingSpaceActions),
|
||||
|
||||
rule("NotSpaceBeforeColon", anyToken, ast.KindColonToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext}, ruleActionDeleteSpace),
|
||||
rule("SpaceAfterColon", ast.KindColonToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNextTokenParentNotJsxNamespacedName}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeQuestionMark", anyToken, ast.KindQuestionToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext, isNotTypeAnnotationContext}, ruleActionDeleteSpace),
|
||||
// insert space after '?' only when it is used in conditional operator
|
||||
rule("SpaceAfterQuestionMarkInConditionalOperator", ast.KindQuestionToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isConditionalOperatorContext}, ruleActionInsertSpace),
|
||||
|
||||
// in other cases there should be no space between '?' and next token
|
||||
rule("NoSpaceAfterQuestionMark", ast.KindQuestionToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNonOptionalPropertyContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("NoSpaceBeforeDot", anyToken, []ast.Kind{ast.KindDotToken, ast.KindQuestionDotToken}, []contextPredicate{isNonJsxSameLineTokenContext, isNotPropertyAccessOnIntegerLiteral}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterDot", []ast.Kind{ast.KindDotToken, ast.KindQuestionDotToken}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("NoSpaceBetweenImportParenInImportType", ast.KindImportKeyword, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isImportTypeContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Special handling of unary operators.
|
||||
// Prefix operators generally shouldn't have a space between
|
||||
// them and their target unary expression.
|
||||
rule("NoSpaceAfterUnaryPrefixOperator", unaryPrefixOperators, unaryPrefixExpressions, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterUnaryPreincrementOperator", ast.KindPlusPlusToken, unaryPreincrementExpressions, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterUnaryPredecrementOperator", ast.KindMinusMinusToken, unaryPredecrementExpressions, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeUnaryPostincrementOperator", unaryPostincrementExpressions, ast.KindPlusPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotStatementConditionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeUnaryPostdecrementOperator", unaryPostdecrementExpressions, ast.KindMinusMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotStatementConditionContext}, ruleActionDeleteSpace),
|
||||
|
||||
// More unary operator special-casing.
|
||||
// DevDiv 181814: Be careful when removing leading whitespace
|
||||
// around unary operators. Examples:
|
||||
// 1 - -2 --X--> 1--2
|
||||
// a + ++b --X--> a+++b
|
||||
rule("SpaceAfterPostincrementWhenFollowedByAdd", ast.KindPlusPlusToken, ast.KindPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterAddWhenFollowedByUnaryPlus", ast.KindPlusToken, ast.KindPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterAddWhenFollowedByPreincrement", ast.KindPlusToken, ast.KindPlusPlusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterPostdecrementWhenFollowedBySubtract", ast.KindMinusMinusToken, ast.KindMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterSubtractWhenFollowedByUnaryMinus", ast.KindMinusToken, ast.KindMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterSubtractWhenFollowedByPredecrement", ast.KindMinusToken, ast.KindMinusMinusToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
|
||||
rule("NoSpaceAfterCloseBrace", ast.KindCloseBraceToken, []ast.Kind{ast.KindCommaToken, ast.KindSemicolonToken}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
// For functions and control block place } on a new line []ast.Kind{multi-line rule}
|
||||
rule("NewLineBeforeCloseBraceInBlockContext", anyTokenIncludingMultilineComments, ast.KindCloseBraceToken, []contextPredicate{isMultilineBlockContext}, ruleActionInsertNewLine),
|
||||
|
||||
// Space/new line after }.
|
||||
rule("SpaceAfterCloseBrace", ast.KindCloseBraceToken, anyTokenExcept(ast.KindCloseParenToken), []contextPredicate{isNonJsxSameLineTokenContext, isAfterCodeBlockContext}, ruleActionInsertSpace),
|
||||
// Special case for (}, else) and (}, while) since else & while tokens are not part of the tree which makes SpaceAfterCloseBrace rule not applied
|
||||
// Also should not apply to })
|
||||
rule("SpaceBetweenCloseBraceAndElse", ast.KindCloseBraceToken, ast.KindElseKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBetweenCloseBraceAndWhile", ast.KindCloseBraceToken, ast.KindWhileKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Add a space after control dec context if the next character is an open bracket ex: 'if (false)[]ast.Kind{a, b} = []ast.Kind{1, 2};' -> 'if (false) []ast.Kind{a, b} = []ast.Kind{1, 2};'
|
||||
rule("SpaceAfterConditionalClosingParen", ast.KindCloseParenToken, ast.KindOpenBracketToken, []contextPredicate{isControlDeclContext}, ruleActionInsertSpace),
|
||||
|
||||
rule("NoSpaceBetweenFunctionKeywordAndStar", ast.KindFunctionKeyword, ast.KindAsteriskToken, []contextPredicate{isFunctionDeclarationOrFunctionExpressionContext}, ruleActionDeleteSpace),
|
||||
rule("SpaceAfterStarInGeneratorDeclaration", ast.KindAsteriskToken, ast.KindIdentifier, []contextPredicate{isFunctionDeclarationOrFunctionExpressionContext}, ruleActionInsertSpace),
|
||||
|
||||
rule("SpaceAfterFunctionInFuncDecl", ast.KindFunctionKeyword, anyToken, []contextPredicate{isFunctionDeclContext}, ruleActionInsertSpace),
|
||||
// Insert new line after { and before } in multi-line contexts.
|
||||
rule("NewLineAfterOpenBraceInBlockContext", ast.KindOpenBraceToken, anyToken, []contextPredicate{isMultilineBlockContext}, ruleActionInsertNewLine),
|
||||
|
||||
// For get/set members, we check for (identifier,identifier) since get/set don't have tokens and they are represented as just an identifier token.
|
||||
// Though, we do extra check on the context to make sure we are dealing with get/set node. Example:
|
||||
// get x() {}
|
||||
// set x(val) {}
|
||||
rule("SpaceAfterGetSetInMember", []ast.Kind{ast.KindGetKeyword, ast.KindSetKeyword}, ast.KindIdentifier, []contextPredicate{isFunctionDeclContext}, ruleActionInsertSpace),
|
||||
|
||||
rule("NoSpaceBetweenYieldKeywordAndStar", ast.KindYieldKeyword, ast.KindAsteriskToken, []contextPredicate{isNonJsxSameLineTokenContext, isYieldOrYieldStarWithOperand}, ruleActionDeleteSpace),
|
||||
rule("SpaceBetweenYieldOrYieldStarAndOperand", []ast.Kind{ast.KindYieldKeyword, ast.KindAsteriskToken}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isYieldOrYieldStarWithOperand}, ruleActionInsertSpace),
|
||||
|
||||
rule("NoSpaceBetweenReturnAndSemicolon", ast.KindReturnKeyword, ast.KindSemicolonToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("SpaceAfterCertainKeywords", []ast.Kind{ast.KindVarKeyword, ast.KindThrowKeyword, ast.KindNewKeyword, ast.KindDeleteKeyword, ast.KindReturnKeyword, ast.KindTypeOfKeyword, ast.KindAwaitKeyword}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterLetConstInVariableDeclaration", []ast.Kind{ast.KindLetKeyword, ast.KindConstKeyword}, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isStartOfVariableDeclarationList}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeOpenParenInFuncCall", anyToken, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isFunctionCallOrNewContext, isPreviousTokenNotComma}, ruleActionDeleteSpace),
|
||||
|
||||
// Special case for binary operators (that are keywords). For these we have to add a space and shouldn't follow any user options.
|
||||
rule("SpaceBeforeBinaryKeywordOperator", anyToken, binaryKeywordOperators, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterBinaryKeywordOperator", binaryKeywordOperators, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
|
||||
rule("SpaceAfterVoidOperator", ast.KindVoidKeyword, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isVoidOpContext}, ruleActionInsertSpace),
|
||||
|
||||
// Async-await
|
||||
rule("SpaceBetweenAsyncAndOpenParen", ast.KindAsyncKeyword, ast.KindOpenParenToken, []contextPredicate{isArrowFunctionContext, isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBetweenAsyncAndFunctionKeyword", ast.KindAsyncKeyword, []ast.Kind{ast.KindFunctionKeyword, ast.KindIdentifier}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
|
||||
// Template string
|
||||
rule("NoSpaceBetweenTagAndTemplateString", []ast.Kind{ast.KindIdentifier, ast.KindCloseParenToken}, []ast.Kind{ast.KindNoSubstitutionTemplateLiteral, ast.KindTemplateHead}, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// JSX opening elements
|
||||
rule("SpaceBeforeJsxAttribute", anyToken, ast.KindIdentifier, []contextPredicate{isNextTokenParentJsxAttribute, isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBeforeSlashInJsxOpeningElement", anyToken, ast.KindSlashToken, []contextPredicate{isJsxSelfClosingElementContext, isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeGreaterThanTokenInJsxOpeningElement", ast.KindSlashToken, ast.KindGreaterThanToken, []contextPredicate{isJsxSelfClosingElementContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeEqualInJsxAttribute", anyToken, ast.KindEqualsToken, []contextPredicate{isJsxAttributeContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterEqualInJsxAttribute", ast.KindEqualsToken, anyToken, []contextPredicate{isJsxAttributeContext, isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeJsxNamespaceColon", ast.KindIdentifier, ast.KindColonToken, []contextPredicate{isNextTokenParentJsxNamespacedName}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterJsxNamespaceColon", ast.KindColonToken, ast.KindIdentifier, []contextPredicate{isNextTokenParentJsxNamespacedName}, ruleActionDeleteSpace),
|
||||
|
||||
// TypeScript-specific rules
|
||||
// Use of module as a function call. e.g.: import m2 = module("m2");
|
||||
rule("NoSpaceAfterModuleImport", []ast.Kind{ast.KindModuleKeyword, ast.KindRequireKeyword}, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
// Add a space around certain TypeScript keywords
|
||||
rule(
|
||||
"SpaceAfterCertainTypeScriptKeywords",
|
||||
[]ast.Kind{
|
||||
ast.KindAbstractKeyword,
|
||||
ast.KindAccessorKeyword,
|
||||
ast.KindClassKeyword,
|
||||
ast.KindDeclareKeyword,
|
||||
ast.KindDefaultKeyword,
|
||||
ast.KindEnumKeyword,
|
||||
ast.KindExportKeyword,
|
||||
ast.KindExtendsKeyword,
|
||||
ast.KindGetKeyword,
|
||||
ast.KindImplementsKeyword,
|
||||
ast.KindImportKeyword,
|
||||
ast.KindInterfaceKeyword,
|
||||
ast.KindModuleKeyword,
|
||||
ast.KindNamespaceKeyword,
|
||||
ast.KindPrivateKeyword,
|
||||
ast.KindPublicKeyword,
|
||||
ast.KindProtectedKeyword,
|
||||
ast.KindReadonlyKeyword,
|
||||
ast.KindSetKeyword,
|
||||
ast.KindStaticKeyword,
|
||||
ast.KindTypeKeyword,
|
||||
ast.KindFromKeyword,
|
||||
ast.KindKeyOfKeyword,
|
||||
ast.KindInferKeyword,
|
||||
},
|
||||
anyToken,
|
||||
[]contextPredicate{isNonJsxSameLineTokenContext},
|
||||
ruleActionInsertSpace,
|
||||
),
|
||||
rule(
|
||||
"SpaceBeforeCertainTypeScriptKeywords",
|
||||
anyToken,
|
||||
[]ast.Kind{ast.KindExtendsKeyword, ast.KindImplementsKeyword, ast.KindFromKeyword},
|
||||
[]contextPredicate{isNonJsxSameLineTokenContext},
|
||||
ruleActionInsertSpace,
|
||||
),
|
||||
// Treat string literals in module names as identifiers, and add a space between the literal and the opening Brace braces, e.g.: module "m2" {
|
||||
rule("SpaceAfterModuleName", ast.KindStringLiteral, ast.KindOpenBraceToken, []contextPredicate{isModuleDeclContext}, ruleActionInsertSpace),
|
||||
|
||||
// Lambda expressions
|
||||
rule("SpaceBeforeArrow", anyToken, ast.KindEqualsGreaterThanToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterArrow", ast.KindEqualsGreaterThanToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
|
||||
// Optional parameters and let args
|
||||
rule("NoSpaceAfterEllipsis", ast.KindDotDotDotToken, ast.KindIdentifier, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterOptionalParameters", ast.KindQuestionToken, []ast.Kind{ast.KindCloseParenToken, ast.KindCommaToken}, []contextPredicate{isNonJsxSameLineTokenContext, isNotBinaryOpContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Remove spaces in empty interface literals. e.g.: x: {}
|
||||
rule("NoSpaceBetweenEmptyInterfaceBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectTypeContext}, ruleActionDeleteSpace),
|
||||
|
||||
// generics and type assertions
|
||||
rule("NoSpaceBeforeOpenAngularBracket", typeNames, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBetweenCloseParenAndAngularBracket", ast.KindCloseParenToken, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterOpenAngularBracket", ast.KindLessThanToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeCloseAngularBracket", anyToken, ast.KindGreaterThanToken, []contextPredicate{isNonJsxSameLineTokenContext, isTypeArgumentOrParameterOrAssertionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterCloseAngularBracket", ast.KindGreaterThanToken, []ast.Kind{ast.KindOpenParenToken, ast.KindOpenBracketToken, ast.KindGreaterThanToken, ast.KindCommaToken}, []contextPredicate{
|
||||
isNonJsxSameLineTokenContext,
|
||||
isTypeArgumentOrParameterOrAssertionContext,
|
||||
isNotFunctionDeclContext, /*To prevent an interference with the SpaceBeforeOpenParenInFuncDecl rule*/
|
||||
isNonTypeAssertionContext,
|
||||
}, ruleActionDeleteSpace),
|
||||
|
||||
// decorators
|
||||
rule("SpaceBeforeAt", []ast.Kind{ast.KindCloseParenToken, ast.KindIdentifier}, ast.KindAtToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterAt", ast.KindAtToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
// Insert space after @ in decorator
|
||||
rule(
|
||||
"SpaceAfterDecorator",
|
||||
anyToken,
|
||||
[]ast.Kind{
|
||||
ast.KindAbstractKeyword,
|
||||
ast.KindIdentifier,
|
||||
ast.KindExportKeyword,
|
||||
ast.KindDefaultKeyword,
|
||||
ast.KindClassKeyword,
|
||||
ast.KindStaticKeyword,
|
||||
ast.KindPublicKeyword,
|
||||
ast.KindPrivateKeyword,
|
||||
ast.KindProtectedKeyword,
|
||||
ast.KindGetKeyword,
|
||||
ast.KindSetKeyword,
|
||||
ast.KindOpenBracketToken,
|
||||
ast.KindAsteriskToken,
|
||||
},
|
||||
[]contextPredicate{isEndOfDecoratorContextOnSameLine},
|
||||
ruleActionInsertSpace,
|
||||
),
|
||||
|
||||
rule("NoSpaceBeforeNonNullAssertionOperator", anyToken, ast.KindExclamationToken, []contextPredicate{isNonJsxSameLineTokenContext, isNonNullAssertionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterNewKeywordOnConstructorSignature", ast.KindNewKeyword, ast.KindOpenParenToken, []contextPredicate{isNonJsxSameLineTokenContext, isConstructorSignatureContext}, ruleActionDeleteSpace),
|
||||
rule("SpaceLessThanAndNonJSXTypeAnnotation", ast.KindLessThanToken, ast.KindLessThanToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
}
|
||||
|
||||
// These rules are applied after high priority
|
||||
userConfigurableRules := []ruleSpec{
|
||||
// Treat constructor as an identifier in a function declaration, and remove spaces between constructor and following left parentheses
|
||||
rule("SpaceAfterConstructor", ast.KindConstructorKeyword, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterConstructorOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterConstructor", ast.KindConstructorKeyword, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterConstructorOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("SpaceAfterComma", ast.KindCommaToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterCommaDelimiterOption), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket, isNextTokenNotCloseParen}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterComma", ast.KindCommaToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterCommaDelimiterOption), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after function keyword for anonymous functions
|
||||
rule("SpaceAfterAnonymousFunctionKeyword", []ast.Kind{ast.KindFunctionKeyword, ast.KindAsteriskToken}, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption), isFunctionDeclContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterAnonymousFunctionKeyword", []ast.Kind{ast.KindFunctionKeyword, ast.KindAsteriskToken}, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterFunctionKeywordForAnonymousFunctionsOption), isFunctionDeclContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after keywords in control flow statements
|
||||
rule("SpaceAfterKeywordInControl", keywords, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterKeywordsInControlFlowStatementsOption), isControlDeclContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterKeywordInControl", keywords, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterKeywordsInControlFlowStatementsOption), isControlDeclContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after opening and before closing nonempty parenthesis
|
||||
rule("SpaceAfterOpenParen", ast.KindOpenParenToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBeforeCloseParen", anyToken, ast.KindCloseParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBetweenOpenParens", ast.KindOpenParenToken, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBetweenParens", ast.KindOpenParenToken, ast.KindCloseParenToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterOpenParen", ast.KindOpenParenToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeCloseParen", anyToken, ast.KindCloseParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesisOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after opening and before closing nonempty brackets
|
||||
rule("SpaceAfterOpenBracket", ast.KindOpenBracketToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBeforeCloseBracket", anyToken, ast.KindCloseBracketToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBetweenBrackets", ast.KindOpenBracketToken, ast.KindCloseBracketToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterOpenBracket", ast.KindOpenBracketToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeCloseBracket", anyToken, ast.KindCloseBracketToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracketsOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
|
||||
rule("SpaceAfterOpenBrace", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionEnabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isBraceWrappedContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBeforeCloseBrace", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isBraceWrappedContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isNonJsxSameLineTokenContext, isObjectContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterOpenBrace", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeCloseBrace", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingNonemptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert a space after opening and before closing empty brace brackets
|
||||
rule("SpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption)}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBetweenEmptyBraceBrackets", ast.KindOpenBraceToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabled(insertSpaceAfterOpeningAndBeforeClosingEmptyBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after opening and before closing template string braces
|
||||
rule("SpaceAfterTemplateHeadAndMiddle", []ast.Kind{ast.KindTemplateHead, ast.KindTemplateMiddle}, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxTextContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines),
|
||||
rule("SpaceBeforeTemplateMiddleAndTail", anyToken, []ast.Kind{ast.KindTemplateMiddle, ast.KindTemplateTail}, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterTemplateHeadAndMiddle", []ast.Kind{ast.KindTemplateHead, ast.KindTemplateMiddle}, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxTextContext}, ruleActionDeleteSpace, ruleFlagsCanDeleteNewLines),
|
||||
rule("NoSpaceBeforeTemplateMiddleAndTail", anyToken, []ast.Kind{ast.KindTemplateMiddle, ast.KindTemplateTail}, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingTemplateStringBracesOption), isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// No space after { and before } in JSX expression
|
||||
rule("SpaceAfterOpenBraceInJsxExpression", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionInsertSpace),
|
||||
rule("SpaceBeforeCloseBraceInJsxExpression", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionEnabled(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterOpenBraceInJsxExpression", ast.KindOpenBraceToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceBeforeCloseBraceInJsxExpression", anyToken, ast.KindCloseBraceToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBracesOption), isNonJsxSameLineTokenContext, isJsxExpressionContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space after semicolon in for statement
|
||||
rule("SpaceAfterSemicolonInFor", ast.KindSemicolonToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterSemicolonInForStatementsOption), isNonJsxSameLineTokenContext, isForContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterSemicolonInFor", ast.KindSemicolonToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterSemicolonInForStatementsOption), isNonJsxSameLineTokenContext, isForContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Insert space before and after binary operators
|
||||
rule("SpaceBeforeBinaryOperator", anyToken, binaryOperators, []contextPredicate{isOptionEnabled(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("SpaceAfterBinaryOperator", binaryOperators, anyToken, []contextPredicate{isOptionEnabled(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeBinaryOperator", anyToken, binaryOperators, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterBinaryOperator", binaryOperators, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeAndAfterBinaryOperatorsOption), isNonJsxSameLineTokenContext, isBinaryOpContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("SpaceBeforeOpenParenInFuncDecl", anyToken, ast.KindOpenParenToken, []contextPredicate{isOptionEnabled(insertSpaceBeforeFunctionParenthesisOption), isNonJsxSameLineTokenContext, isFunctionDeclContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeOpenParenInFuncDecl", anyToken, ast.KindOpenParenToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeFunctionParenthesisOption), isNonJsxSameLineTokenContext, isFunctionDeclContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Open Brace braces after control block
|
||||
rule("NewLineBeforeOpenBraceInControl", controlOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForControlBlocksOption), isControlDeclContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines),
|
||||
|
||||
// Open Brace braces after function
|
||||
// TypeScript: Function can have return types, which can be made of tons of different token kinds
|
||||
rule("NewLineBeforeOpenBraceInFunction", functionOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForFunctionsOption), isFunctionDeclContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines),
|
||||
// Open Brace braces after TypeScript module/class/interface
|
||||
rule("NewLineBeforeOpenBraceInTypeScriptDeclWithBlock", typeScriptOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionEnabled(placeOpenBraceOnNewLineForFunctionsOption), isTypeScriptDeclWithBlockContext, isBeforeMultilineBlockContext}, ruleActionInsertNewLine, ruleFlagsCanDeleteNewLines),
|
||||
|
||||
rule("SpaceAfterTypeAssertion", ast.KindGreaterThanToken, anyToken, []contextPredicate{isOptionEnabled(insertSpaceAfterTypeAssertionOption), isNonJsxSameLineTokenContext, isTypeAssertionContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceAfterTypeAssertion", ast.KindGreaterThanToken, anyToken, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceAfterTypeAssertionOption), isNonJsxSameLineTokenContext, isTypeAssertionContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("SpaceBeforeTypeAnnotation", anyToken, []ast.Kind{ast.KindQuestionToken, ast.KindColonToken}, []contextPredicate{isOptionEnabled(insertSpaceBeforeTypeAnnotationOption), isNonJsxSameLineTokenContext, isTypeAnnotationContext}, ruleActionInsertSpace),
|
||||
rule("NoSpaceBeforeTypeAnnotation", anyToken, []ast.Kind{ast.KindQuestionToken, ast.KindColonToken}, []contextPredicate{isOptionDisabledOrUndefined(insertSpaceBeforeTypeAnnotationOption), isNonJsxSameLineTokenContext, isTypeAnnotationContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("NoOptionalSemicolon", ast.KindSemicolonToken, anyTokenIncludingEOF, []contextPredicate{optionEquals(semicolonOption, SemicolonPreferenceRemove), isSemicolonDeletionContext}, ruleActionDeleteToken),
|
||||
rule("OptionalSemicolon", anyToken, anyTokenIncludingEOF, []contextPredicate{optionEquals(semicolonOption, SemicolonPreferenceInsert), isSemicolonInsertionContext}, ruleActionInsertTrailingSemicolon),
|
||||
}
|
||||
|
||||
// These rules are lower in priority than user-configurable. Rules earlier in this list have priority over rules later in the list.
|
||||
lowPriorityCommonRules := []ruleSpec{
|
||||
// Space after keyword but not before ; or : or ?
|
||||
rule("NoSpaceBeforeSemicolon", anyToken, ast.KindSemicolonToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
rule("SpaceBeforeOpenBraceInControl", controlOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForControlBlocksOption), isControlDeclContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines),
|
||||
rule("SpaceBeforeOpenBraceInFunction", functionOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForFunctionsOption), isFunctionDeclContext, isBeforeBlockContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines),
|
||||
rule("SpaceBeforeOpenBraceInTypeScriptDeclWithBlock", typeScriptOpenBraceLeftTokenRange, ast.KindOpenBraceToken, []contextPredicate{isOptionDisabledOrUndefinedOrTokensOnSameLine(placeOpenBraceOnNewLineForFunctionsOption), isTypeScriptDeclWithBlockContext, isNotFormatOnEnter, isSameLineTokenOrBeforeBlockContext}, ruleActionInsertSpace, ruleFlagsCanDeleteNewLines),
|
||||
|
||||
rule("NoSpaceBeforeComma", anyToken, ast.KindCommaToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// No space before and after indexer `x[]ast.Kind{}`
|
||||
rule("NoSpaceBeforeOpenBracket", anyTokenExcept(ast.KindAsyncKeyword, ast.KindCaseKeyword), ast.KindOpenBracketToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
rule("NoSpaceAfterCloseBracket", ast.KindCloseBracketToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext, isNotBeforeBlockInFunctionDeclarationContext}, ruleActionDeleteSpace),
|
||||
rule("SpaceAfterSemicolon", ast.KindSemicolonToken, anyToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
|
||||
// Remove extra space between for and await
|
||||
rule("SpaceBetweenForAndAwaitKeyword", ast.KindForKeyword, ast.KindAwaitKeyword, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
|
||||
// Remove extra spaces between ... and type name in tuple spread
|
||||
rule("SpaceBetweenDotDotDotAndTypeName", ast.KindDotDotDotToken, typeNames, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionDeleteSpace),
|
||||
|
||||
// Add a space between statements. All keywords except (do,else,case) has open/close parens after them.
|
||||
// So, we have a rule to add a space for []ast.Kind{),Any}, []ast.Kind{do,Any}, []ast.Kind{else,Any}, and []ast.Kind{case,Any}
|
||||
rule(
|
||||
"SpaceBetweenStatements",
|
||||
[]ast.Kind{ast.KindCloseParenToken, ast.KindDoKeyword, ast.KindElseKeyword, ast.KindCaseKeyword},
|
||||
anyToken,
|
||||
[]contextPredicate{isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNotForContext},
|
||||
ruleActionInsertSpace,
|
||||
),
|
||||
// This low-pri rule takes care of "try {", "catch {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
|
||||
rule("SpaceAfterTryCatchFinally", []ast.Kind{ast.KindTryKeyword, ast.KindCatchKeyword, ast.KindFinallyKeyword}, ast.KindOpenBraceToken, []contextPredicate{isNonJsxSameLineTokenContext}, ruleActionInsertSpace),
|
||||
}
|
||||
|
||||
result := make([]ruleSpec, 0, len(highPriorityCommonRules)+len(userConfigurableRules)+len(lowPriorityCommonRules))
|
||||
result = append(result, highPriorityCommonRules...)
|
||||
result = append(result, userConfigurableRules...)
|
||||
result = append(result, lowPriorityCommonRules...)
|
||||
return result
|
||||
}
|
||||
|
||||
func tokenRangeFrom(tokens ...ast.Kind) tokenRange {
|
||||
return tokenRange{
|
||||
isSpecific: true,
|
||||
tokens: tokens,
|
||||
}
|
||||
}
|
||||
|
||||
func tokenRangeFromEx(prefix []ast.Kind, tokens ...ast.Kind) tokenRange {
|
||||
tokens = append(prefix, tokens...)
|
||||
return tokenRange{
|
||||
isSpecific: true,
|
||||
tokens: tokens,
|
||||
}
|
||||
}
|
||||
|
||||
func tokenRangeFromRange(start ast.Kind, end ast.Kind) tokenRange {
|
||||
tokens := make([]ast.Kind, 0, end-start+1)
|
||||
for token := start; token <= end; token++ {
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
|
||||
return tokenRangeFrom(tokens...)
|
||||
}
|
||||
@ -1,156 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug"
|
||||
)
|
||||
|
||||
func getRules(context *formattingContext, rules []*ruleImpl) []*ruleImpl {
|
||||
bucket := getRulesMap()[getRuleBucketIndex(context.currentTokenSpan.Kind, context.nextTokenSpan.Kind)]
|
||||
if len(bucket) > 0 {
|
||||
ruleActionMask := ruleActionNone
|
||||
outer:
|
||||
for _, rule := range bucket {
|
||||
acceptRuleActions := ^getRuleActionExclusion(ruleActionMask)
|
||||
if rule.Action()&acceptRuleActions != 0 {
|
||||
preds := rule.Context()
|
||||
for _, p := range preds {
|
||||
if !p(context) {
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
rules = append(rules, rule)
|
||||
ruleActionMask |= rule.Action()
|
||||
}
|
||||
}
|
||||
return rules
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
func getRuleBucketIndex(row ast.Kind, column ast.Kind) int {
|
||||
debug.Assert(row <= ast.KindLastKeyword && column <= ast.KindLastKeyword, "Must compute formatting context from tokens")
|
||||
return (int(row) * mapRowLength) + int(column)
|
||||
}
|
||||
|
||||
const (
|
||||
maskBitSize = 5
|
||||
mask = 0b11111 // MaskBitSize bits
|
||||
mapRowLength = int(ast.KindLastToken) + 1
|
||||
)
|
||||
|
||||
/**
|
||||
* For a given rule action, gets a mask of other rule actions that
|
||||
* cannot be applied at the same position.
|
||||
*/
|
||||
func getRuleActionExclusion(ruleAction ruleAction) ruleAction {
|
||||
mask := ruleActionNone
|
||||
if ruleAction&ruleActionStopProcessingSpaceActions != 0 {
|
||||
mask |= ruleActionModifySpaceAction
|
||||
}
|
||||
if ruleAction&ruleActionStopProcessingTokenActions != 0 {
|
||||
mask |= ruleActionModifyTokenAction
|
||||
}
|
||||
if ruleAction&ruleActionModifySpaceAction != 0 {
|
||||
mask |= ruleActionModifySpaceAction
|
||||
}
|
||||
if ruleAction&ruleActionModifyTokenAction != 0 {
|
||||
mask |= ruleActionModifyTokenAction
|
||||
}
|
||||
return mask
|
||||
}
|
||||
|
||||
var getRulesMap = sync.OnceValue(buildRulesMap)
|
||||
|
||||
func buildRulesMap() [][]*ruleImpl {
|
||||
rules := getAllRules()
|
||||
// Map from bucket index to array of rules
|
||||
m := make([][]*ruleImpl, mapRowLength*mapRowLength)
|
||||
// This array is used only during construction of the rulesbucket in the map
|
||||
rulesBucketConstructionStateList := make([]int, len(m))
|
||||
for _, rule := range rules {
|
||||
specificRule := rule.leftTokenRange.isSpecific && rule.rightTokenRange.isSpecific
|
||||
|
||||
for _, left := range rule.leftTokenRange.tokens {
|
||||
for _, right := range rule.rightTokenRange.tokens {
|
||||
index := getRuleBucketIndex(left, right)
|
||||
m[index] = addRule(m[index], rule.rule, specificRule, rulesBucketConstructionStateList, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type RulesPosition int
|
||||
|
||||
const (
|
||||
RulesPositionStopRulesSpecific RulesPosition = 0
|
||||
RulesPositionStopRulesAny RulesPosition = maskBitSize * 1
|
||||
RulesPositionContextRulesSpecific RulesPosition = maskBitSize * 2
|
||||
RulesPositionContextRulesAny RulesPosition = maskBitSize * 3
|
||||
RulesPositionNoContextRulesSpecific RulesPosition = maskBitSize * 4
|
||||
RulesPositionNoContextRulesAny RulesPosition = maskBitSize * 5
|
||||
)
|
||||
|
||||
// The Rules list contains all the inserted rules into a rulebucket in the following order:
|
||||
//
|
||||
// 1- Ignore rules with specific token combination
|
||||
// 2- Ignore rules with any token combination
|
||||
// 3- Context rules with specific token combination
|
||||
// 4- Context rules with any token combination
|
||||
// 5- Non-context rules with specific token combination
|
||||
// 6- Non-context rules with any token combination
|
||||
//
|
||||
// The member rulesInsertionIndexBitmap is used to describe the number of rules
|
||||
// in each sub-bucket (above) hence can be used to know the index of where to insert
|
||||
// the next rule. It's a bitmap which contains 6 different sections each is given 5 bits.
|
||||
//
|
||||
// Example:
|
||||
// In order to insert a rule to the end of sub-bucket (3), we get the index by adding
|
||||
// the values in the bitmap segments 3rd, 2nd, and 1st.
|
||||
func addRule(rules []*ruleImpl, rule *ruleImpl, specificTokens bool, constructionState []int, rulesBucketIndex int) []*ruleImpl {
|
||||
var position RulesPosition
|
||||
if rule.Action()&ruleActionStopAction != 0 {
|
||||
if specificTokens {
|
||||
position = RulesPositionStopRulesSpecific
|
||||
} else {
|
||||
position = RulesPositionStopRulesAny
|
||||
}
|
||||
} else if len(rule.Context()) != 0 {
|
||||
if specificTokens {
|
||||
position = RulesPositionContextRulesSpecific
|
||||
} else {
|
||||
position = RulesPositionContextRulesAny
|
||||
}
|
||||
} else {
|
||||
if specificTokens {
|
||||
position = RulesPositionNoContextRulesSpecific
|
||||
} else {
|
||||
position = RulesPositionNoContextRulesAny
|
||||
}
|
||||
}
|
||||
|
||||
state := constructionState[rulesBucketIndex]
|
||||
|
||||
rules = slices.Insert(rules, getRuleInsertionIndex(state, position), rule)
|
||||
constructionState[rulesBucketIndex] = increaseInsertionIndex(state, position)
|
||||
return rules
|
||||
}
|
||||
|
||||
func getRuleInsertionIndex(indexBitmap int, maskPosition RulesPosition) int {
|
||||
index := 0
|
||||
for pos := 0; pos <= int(maskPosition); pos += maskBitSize {
|
||||
index += indexBitmap & mask
|
||||
indexBitmap >>= maskBitSize
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func increaseInsertionIndex(indexBitmap int, maskPosition RulesPosition) int {
|
||||
value := ((indexBitmap >> maskPosition) & mask) + 1
|
||||
debug.Assert((value&mask) == value, "Adding more rules into the sub-bucket than allowed. Maximum allowed is 32 rules.")
|
||||
return (indexBitmap & ^(mask << maskPosition)) | (value << maskPosition)
|
||||
}
|
||||
@ -1,355 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/debug"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
)
|
||||
|
||||
type TextRangeWithKind struct {
|
||||
Loc core.TextRange
|
||||
Kind ast.Kind
|
||||
}
|
||||
|
||||
func NewTextRangeWithKind(pos int, end int, kind ast.Kind) TextRangeWithKind {
|
||||
return TextRangeWithKind{
|
||||
Loc: core.NewTextRange(pos, end),
|
||||
Kind: kind,
|
||||
}
|
||||
}
|
||||
|
||||
type tokenInfo struct {
|
||||
leadingTrivia []TextRangeWithKind
|
||||
token TextRangeWithKind
|
||||
trailingTrivia []TextRangeWithKind
|
||||
}
|
||||
|
||||
type formattingScanner struct {
|
||||
s *scanner.Scanner
|
||||
startPos int
|
||||
endPos int
|
||||
savedPos int
|
||||
hasLastTokenInfo bool
|
||||
lastTokenInfo tokenInfo
|
||||
lastScanAction scanAction
|
||||
leadingTrivia []TextRangeWithKind
|
||||
trailingTrivia []TextRangeWithKind
|
||||
wasNewLine bool
|
||||
}
|
||||
|
||||
func newFormattingScanner(text string, languageVariant core.LanguageVariant, startPos int, endPos int, worker *formatSpanWorker) []core.TextChange {
|
||||
scan := scanner.NewScanner()
|
||||
scan.Reset()
|
||||
scan.SetSkipTrivia(false)
|
||||
scan.SetLanguageVariant(languageVariant)
|
||||
scan.SetText(text)
|
||||
|
||||
fmtScn := &formattingScanner{
|
||||
s: scan,
|
||||
startPos: startPos,
|
||||
endPos: endPos,
|
||||
wasNewLine: true,
|
||||
}
|
||||
|
||||
res := worker.execute(fmtScn)
|
||||
|
||||
fmtScn.hasLastTokenInfo = false
|
||||
scan.Reset()
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *formattingScanner) advance() {
|
||||
s.hasLastTokenInfo = false
|
||||
isStarted := s.s.TokenFullStart() != s.startPos
|
||||
|
||||
if isStarted {
|
||||
s.wasNewLine = len(s.trailingTrivia) > 0 && core.LastOrNil(s.trailingTrivia).Kind == ast.KindNewLineTrivia
|
||||
} else {
|
||||
s.s.Scan()
|
||||
}
|
||||
|
||||
s.leadingTrivia = nil
|
||||
s.trailingTrivia = nil
|
||||
|
||||
pos := s.s.TokenFullStart()
|
||||
|
||||
// Read leading trivia and token
|
||||
for pos < s.endPos {
|
||||
t := s.s.Token()
|
||||
if !ast.IsTrivia(t) {
|
||||
break
|
||||
}
|
||||
|
||||
// consume leading trivia
|
||||
s.s.Scan()
|
||||
item := NewTextRangeWithKind(pos, s.s.TokenFullStart(), t)
|
||||
|
||||
pos = s.s.TokenFullStart()
|
||||
|
||||
s.leadingTrivia = append(s.leadingTrivia, item)
|
||||
}
|
||||
|
||||
s.savedPos = s.s.TokenFullStart()
|
||||
}
|
||||
|
||||
func shouldRescanGreaterThanToken(node *ast.Node) bool {
|
||||
switch node.Kind {
|
||||
case ast.KindGreaterThanEqualsToken,
|
||||
ast.KindGreaterThanGreaterThanEqualsToken,
|
||||
ast.KindGreaterThanGreaterThanGreaterThanEqualsToken,
|
||||
ast.KindGreaterThanGreaterThanGreaterThanToken,
|
||||
ast.KindGreaterThanGreaterThanToken:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func shouldRescanJsxIdentifier(node *ast.Node) bool {
|
||||
if node.Parent != nil {
|
||||
switch node.Parent.Kind {
|
||||
case ast.KindJsxAttribute,
|
||||
ast.KindJsxOpeningElement,
|
||||
ast.KindJsxClosingElement,
|
||||
ast.KindJsxSelfClosingElement:
|
||||
// May parse an identifier like `module-layout`; that will be scanned as a keyword at first, but we should parse the whole thing to get an identifier.
|
||||
return ast.IsKeywordKind(node.Kind) || node.Kind == ast.KindIdentifier
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *formattingScanner) shouldRescanJsxText(node *ast.Node) bool {
|
||||
if ast.IsJsxText(node) {
|
||||
return true
|
||||
}
|
||||
if !ast.IsJsxElement(node) || s.hasLastTokenInfo == false {
|
||||
return false
|
||||
}
|
||||
|
||||
return s.lastTokenInfo.token.Kind == ast.KindJsxText
|
||||
}
|
||||
|
||||
func shouldRescanSlashToken(container *ast.Node) bool {
|
||||
return container.Kind == ast.KindRegularExpressionLiteral
|
||||
}
|
||||
|
||||
func shouldRescanTemplateToken(container *ast.Node) bool {
|
||||
return container.Kind == ast.KindTemplateMiddle ||
|
||||
container.Kind == ast.KindTemplateTail
|
||||
}
|
||||
|
||||
func shouldRescanJsxAttributeValue(node *ast.Node) bool {
|
||||
return node.Parent != nil && ast.IsJsxAttribute(node.Parent) && node.Parent.Initializer() == node
|
||||
}
|
||||
|
||||
func startsWithSlashToken(t ast.Kind) bool {
|
||||
return t == ast.KindSlashToken || t == ast.KindSlashEqualsToken
|
||||
}
|
||||
|
||||
type scanAction int
|
||||
|
||||
const (
|
||||
actionScan scanAction = iota
|
||||
actionRescanGreaterThanToken
|
||||
actionRescanSlashToken
|
||||
actionRescanTemplateToken
|
||||
actionRescanJsxIdentifier
|
||||
actionRescanJsxText
|
||||
actionRescanJsxAttributeValue
|
||||
)
|
||||
|
||||
func fixTokenKind(tokenInfo tokenInfo, container *ast.Node) tokenInfo {
|
||||
if ast.IsTokenKind(container.Kind) && tokenInfo.token.Kind != container.Kind {
|
||||
tokenInfo.token.Kind = container.Kind
|
||||
}
|
||||
return tokenInfo
|
||||
}
|
||||
|
||||
func (s *formattingScanner) readTokenInfo(n *ast.Node) tokenInfo {
|
||||
debug.Assert(s.isOnToken())
|
||||
|
||||
// normally scanner returns the smallest available token
|
||||
// check the kind of context node to determine if scanner should have more greedy behavior and consume more text.
|
||||
|
||||
var expectedScanAction scanAction
|
||||
if shouldRescanGreaterThanToken(n) {
|
||||
expectedScanAction = actionRescanGreaterThanToken
|
||||
} else if shouldRescanSlashToken(n) {
|
||||
expectedScanAction = actionRescanSlashToken
|
||||
} else if shouldRescanTemplateToken(n) {
|
||||
expectedScanAction = actionRescanTemplateToken
|
||||
} else if shouldRescanJsxIdentifier(n) {
|
||||
expectedScanAction = actionRescanJsxIdentifier
|
||||
} else if s.shouldRescanJsxText(n) {
|
||||
expectedScanAction = actionRescanJsxText
|
||||
} else if shouldRescanJsxAttributeValue(n) {
|
||||
expectedScanAction = actionRescanJsxAttributeValue
|
||||
} else {
|
||||
expectedScanAction = actionScan
|
||||
}
|
||||
|
||||
if s.hasLastTokenInfo && expectedScanAction == s.lastScanAction {
|
||||
// readTokenInfo was called before with the same expected scan action.
|
||||
// No need to re-scan text, return existing 'lastTokenInfo'
|
||||
// it is ok to call fixTokenKind here since it does not affect
|
||||
// what portion of text is consumed. In contrast rescanning can change it,
|
||||
// i.e. for '>=' when originally scanner eats just one character
|
||||
// and rescanning forces it to consume more.
|
||||
s.lastTokenInfo = fixTokenKind(s.lastTokenInfo, n)
|
||||
return s.lastTokenInfo
|
||||
}
|
||||
|
||||
if s.s.TokenFullStart() != s.savedPos {
|
||||
// readTokenInfo was called before but scan action differs - rescan text
|
||||
s.s.ResetTokenState(s.savedPos)
|
||||
s.s.Scan()
|
||||
}
|
||||
|
||||
currentToken := s.getNextToken(n, expectedScanAction)
|
||||
|
||||
token := NewTextRangeWithKind(
|
||||
s.s.TokenFullStart(),
|
||||
s.s.TokenEnd(),
|
||||
currentToken,
|
||||
)
|
||||
|
||||
// consume trailing trivia
|
||||
s.trailingTrivia = nil
|
||||
for s.s.TokenFullStart() < s.endPos {
|
||||
currentToken = s.s.Scan()
|
||||
if !ast.IsTrivia(currentToken) {
|
||||
break
|
||||
}
|
||||
trivia := NewTextRangeWithKind(
|
||||
s.s.TokenFullStart(),
|
||||
s.s.TokenEnd(),
|
||||
currentToken,
|
||||
)
|
||||
|
||||
s.trailingTrivia = append(s.trailingTrivia, trivia)
|
||||
|
||||
if currentToken == ast.KindNewLineTrivia {
|
||||
// move past new line
|
||||
s.s.Scan()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
s.hasLastTokenInfo = true
|
||||
s.lastTokenInfo = tokenInfo{
|
||||
leadingTrivia: slices.Clone(s.leadingTrivia),
|
||||
token: token,
|
||||
trailingTrivia: slices.Clone(s.trailingTrivia),
|
||||
}
|
||||
s.lastTokenInfo = fixTokenKind(s.lastTokenInfo, n)
|
||||
|
||||
return s.lastTokenInfo
|
||||
}
|
||||
|
||||
func (s *formattingScanner) getNextToken(n *ast.Node, expectedScanAction scanAction) ast.Kind {
|
||||
token := s.s.Token()
|
||||
s.lastScanAction = actionScan
|
||||
switch expectedScanAction {
|
||||
case actionRescanGreaterThanToken:
|
||||
if token == ast.KindGreaterThanToken {
|
||||
s.lastScanAction = actionRescanGreaterThanToken
|
||||
newToken := s.s.ReScanGreaterThanToken()
|
||||
debug.Assert(n.Kind == newToken)
|
||||
return newToken
|
||||
}
|
||||
case actionRescanSlashToken:
|
||||
if startsWithSlashToken(token) {
|
||||
s.lastScanAction = actionRescanSlashToken
|
||||
newToken := s.s.ReScanSlashToken()
|
||||
debug.Assert(n.Kind == newToken)
|
||||
return newToken
|
||||
}
|
||||
case actionRescanTemplateToken:
|
||||
if token == ast.KindCloseBraceToken {
|
||||
s.lastScanAction = actionRescanTemplateToken
|
||||
return s.s.ReScanTemplateToken( /*isTaggedTemplate*/ false)
|
||||
}
|
||||
case actionRescanJsxIdentifier:
|
||||
s.lastScanAction = actionRescanJsxIdentifier
|
||||
return s.s.ScanJsxIdentifier()
|
||||
case actionRescanJsxText:
|
||||
s.lastScanAction = actionRescanJsxText
|
||||
return s.s.ReScanJsxToken( /*allowMultilineJsxText*/ false)
|
||||
case actionRescanJsxAttributeValue:
|
||||
s.lastScanAction = actionRescanJsxAttributeValue
|
||||
return s.s.ReScanJsxAttributeValue()
|
||||
case actionScan:
|
||||
break
|
||||
default:
|
||||
debug.AssertNever(expectedScanAction, "unhandled scan action kind")
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func (s *formattingScanner) readEOFTokenRange() TextRangeWithKind {
|
||||
debug.Assert(s.isOnEOF())
|
||||
return NewTextRangeWithKind(
|
||||
s.s.TokenFullStart(),
|
||||
s.s.TokenEnd(),
|
||||
ast.KindEndOfFile,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *formattingScanner) isOnToken() bool {
|
||||
current := s.s.Token()
|
||||
if s.hasLastTokenInfo {
|
||||
current = s.lastTokenInfo.token.Kind
|
||||
}
|
||||
return current != ast.KindEndOfFile && !ast.IsTrivia(current)
|
||||
}
|
||||
|
||||
func (s *formattingScanner) isOnEOF() bool {
|
||||
current := s.s.Token()
|
||||
if s.hasLastTokenInfo {
|
||||
current = s.lastTokenInfo.token.Kind
|
||||
}
|
||||
return current == ast.KindEndOfFile
|
||||
}
|
||||
|
||||
func (s *formattingScanner) skipToEndOf(r *core.TextRange) {
|
||||
s.s.ResetTokenState(r.End())
|
||||
s.savedPos = s.s.TokenFullStart()
|
||||
s.lastScanAction = actionScan
|
||||
s.hasLastTokenInfo = false
|
||||
s.wasNewLine = false
|
||||
s.leadingTrivia = nil
|
||||
s.trailingTrivia = nil
|
||||
}
|
||||
|
||||
func (s *formattingScanner) skipToStartOf(r *core.TextRange) {
|
||||
s.s.ResetTokenState(r.Pos())
|
||||
s.savedPos = s.s.TokenFullStart()
|
||||
s.lastScanAction = actionScan
|
||||
s.hasLastTokenInfo = false
|
||||
s.wasNewLine = false
|
||||
s.leadingTrivia = nil
|
||||
s.trailingTrivia = nil
|
||||
}
|
||||
|
||||
func (s *formattingScanner) getCurrentLeadingTrivia() []TextRangeWithKind {
|
||||
return s.leadingTrivia
|
||||
}
|
||||
|
||||
func (s *formattingScanner) lastTrailingTriviaWasNewLine() bool {
|
||||
return s.wasNewLine
|
||||
}
|
||||
|
||||
func (s *formattingScanner) getTokenFullStart() int {
|
||||
if s.hasLastTokenInfo {
|
||||
return s.lastTokenInfo.token.Loc.Pos()
|
||||
}
|
||||
return s.s.TokenFullStart()
|
||||
}
|
||||
|
||||
func (s *formattingScanner) getStartPos() int { // TODO: redundant?
|
||||
return s.getTokenFullStart()
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,190 +0,0 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/ast"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/astnav"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/core"
|
||||
"efprojects.com/kitten-ipc/kitcom/internal/tsgo/scanner"
|
||||
)
|
||||
|
||||
func rangeIsOnOneLine(node core.TextRange, file *ast.SourceFile) bool {
|
||||
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.Pos())
|
||||
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.End())
|
||||
return startLine == endLine
|
||||
}
|
||||
|
||||
func getOpenTokenForList(node *ast.Node, list *ast.NodeList) ast.Kind {
|
||||
switch node.Kind {
|
||||
case ast.KindConstructor,
|
||||
ast.KindFunctionDeclaration,
|
||||
ast.KindFunctionExpression,
|
||||
ast.KindMethodDeclaration,
|
||||
ast.KindMethodSignature,
|
||||
ast.KindArrowFunction,
|
||||
ast.KindCallSignature,
|
||||
ast.KindConstructSignature,
|
||||
ast.KindFunctionType,
|
||||
ast.KindConstructorType,
|
||||
ast.KindGetAccessor,
|
||||
ast.KindSetAccessor:
|
||||
if node.TypeParameterList() == list {
|
||||
return ast.KindLessThanToken
|
||||
} else if node.ParameterList() == list {
|
||||
return ast.KindOpenParenToken
|
||||
}
|
||||
case ast.KindCallExpression, ast.KindNewExpression:
|
||||
if node.TypeArgumentList() == list {
|
||||
return ast.KindLessThanToken
|
||||
} else if node.ArgumentList() == list {
|
||||
return ast.KindOpenParenToken
|
||||
}
|
||||
case ast.KindClassDeclaration,
|
||||
ast.KindClassExpression,
|
||||
ast.KindInterfaceDeclaration,
|
||||
ast.KindTypeAliasDeclaration:
|
||||
if node.TypeParameterList() == list {
|
||||
return ast.KindLessThanToken
|
||||
}
|
||||
case ast.KindTypeReference,
|
||||
ast.KindTaggedTemplateExpression,
|
||||
ast.KindTypeQuery,
|
||||
ast.KindExpressionWithTypeArguments,
|
||||
ast.KindImportType:
|
||||
if node.TypeArgumentList() == list {
|
||||
return ast.KindLessThanToken
|
||||
}
|
||||
case ast.KindTypeLiteral:
|
||||
return ast.KindOpenBraceToken
|
||||
}
|
||||
|
||||
return ast.KindUnknown
|
||||
}
|
||||
|
||||
func getCloseTokenForOpenToken(kind ast.Kind) ast.Kind {
|
||||
// TODO: matches strada - seems like it could handle more pairs of braces, though? [] notably missing
|
||||
switch kind {
|
||||
case ast.KindOpenParenToken:
|
||||
return ast.KindCloseParenToken
|
||||
case ast.KindLessThanToken:
|
||||
return ast.KindGreaterThanToken
|
||||
case ast.KindOpenBraceToken:
|
||||
return ast.KindCloseBraceToken
|
||||
}
|
||||
return ast.KindUnknown
|
||||
}
|
||||
|
||||
func GetLineStartPositionForPosition(position int, sourceFile *ast.SourceFile) int {
|
||||
lineStarts := scanner.GetECMALineStarts(sourceFile)
|
||||
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
|
||||
return int(lineStarts[line])
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether `child` is a grammar error on `parent`.
|
||||
* In strada, this also checked node arrays, but it is never actually called with one in practice.
|
||||
*/
|
||||
func isGrammarError(parent *ast.Node, child *ast.Node) bool {
|
||||
if ast.IsTypeParameterDeclaration(parent) {
|
||||
return child == parent.AsTypeParameter().Expression
|
||||
}
|
||||
if ast.IsPropertySignatureDeclaration(parent) {
|
||||
return child == parent.AsPropertySignatureDeclaration().Initializer
|
||||
}
|
||||
if ast.IsPropertyDeclaration(parent) {
|
||||
return ast.IsAutoAccessorPropertyDeclaration(parent) && child == parent.AsPropertyDeclaration().PostfixToken && child.Kind == ast.KindQuestionToken
|
||||
}
|
||||
if ast.IsPropertyAssignment(parent) {
|
||||
pa := parent.AsPropertyAssignment()
|
||||
mods := pa.Modifiers()
|
||||
return child == pa.PostfixToken || (mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike))
|
||||
}
|
||||
if ast.IsShorthandPropertyAssignment(parent) {
|
||||
sp := parent.AsShorthandPropertyAssignment()
|
||||
mods := sp.Modifiers()
|
||||
return child == sp.EqualsToken || child == sp.PostfixToken || (mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike))
|
||||
}
|
||||
if ast.IsMethodDeclaration(parent) {
|
||||
return child == parent.AsMethodDeclaration().PostfixToken && child.Kind == ast.KindExclamationToken
|
||||
}
|
||||
if ast.IsConstructorDeclaration(parent) {
|
||||
return child == parent.AsConstructorDeclaration().Type || isGrammarErrorElement(parent.AsConstructorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration)
|
||||
}
|
||||
if ast.IsGetAccessorDeclaration(parent) {
|
||||
return isGrammarErrorElement(parent.AsGetAccessorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration)
|
||||
}
|
||||
if ast.IsSetAccessorDeclaration(parent) {
|
||||
return child == parent.AsSetAccessorDeclaration().Type || isGrammarErrorElement(parent.AsSetAccessorDeclaration().TypeParameters, child, ast.IsTypeParameterDeclaration)
|
||||
}
|
||||
if ast.IsNamespaceExportDeclaration(parent) {
|
||||
mods := parent.AsNamespaceExportDeclaration().Modifiers()
|
||||
return mods != nil && isGrammarErrorElement(&mods.NodeList, child, ast.IsModifierLike)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isGrammarErrorElement(list *ast.NodeList, child *ast.Node, isPossibleElement func(node *ast.Node) bool) bool {
|
||||
if list == nil || len(list.Nodes) == 0 {
|
||||
return false
|
||||
}
|
||||
if !isPossibleElement(child) {
|
||||
return false
|
||||
}
|
||||
return slices.Contains(list.Nodes, child)
|
||||
}
|
||||
|
||||
/**
|
||||
* Validating `expectedTokenKind` ensures the token was typed in the context we expect (eg: not a comment).
|
||||
* @param expectedTokenKind The kind of the last token constituting the desired parent node.
|
||||
*/
|
||||
func findImmediatelyPrecedingTokenOfKind(end int, expectedTokenKind ast.Kind, sourceFile *ast.SourceFile) *ast.Node {
|
||||
precedingToken := astnav.FindPrecedingToken(sourceFile, end)
|
||||
if precedingToken == nil || precedingToken.Kind != expectedTokenKind || precedingToken.End() != end {
|
||||
return nil
|
||||
}
|
||||
return precedingToken
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the highest node enclosing `node` at the same list level as `node`
|
||||
* and whose end does not exceed `node.end`.
|
||||
*
|
||||
* Consider typing the following
|
||||
* ```
|
||||
* let x = 1;
|
||||
* while (true) {
|
||||
* }
|
||||
* ```
|
||||
* Upon typing the closing curly, we want to format the entire `while`-statement, but not the preceding
|
||||
* variable declaration.
|
||||
*/
|
||||
func findOutermostNodeWithinListLevel(node *ast.Node) *ast.Node {
|
||||
current := node
|
||||
for current != nil &&
|
||||
current.Parent != nil &&
|
||||
current.Parent.End() == node.End() &&
|
||||
!isListElement(current.Parent, current) {
|
||||
current = current.Parent
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
// Returns true if node is a element in some list in parent
|
||||
// i.e. parent is class declaration with the list of members and node is one of members.
|
||||
func isListElement(parent *ast.Node, node *ast.Node) bool {
|
||||
switch parent.Kind {
|
||||
case ast.KindClassDeclaration, ast.KindInterfaceDeclaration:
|
||||
return node.Loc.ContainedBy(parent.MemberList().Loc)
|
||||
case ast.KindModuleDeclaration:
|
||||
body := parent.Body()
|
||||
return body != nil && body.Kind == ast.KindModuleBlock && node.Loc.ContainedBy(body.StatementList().Loc)
|
||||
case ast.KindSourceFile, ast.KindBlock, ast.KindModuleBlock:
|
||||
return node.Loc.ContainedBy(parent.StatementList().Loc)
|
||||
case ast.KindCatchClause:
|
||||
return node.Loc.ContainedBy(parent.AsCatchClause().Block.StatementList().Loc)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user