|
| 1 | +package jsx_tag_spacing |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + |
| 6 | + "github.com/microsoft/TypeScript/tsc/shim/ast" |
| 7 | + "github.com/microsoft/TypeScript/tsc/shim/core" |
| 8 | + "github.com/microsoft/TypeScript/tsc/shim/scanner" |
| 9 | + "github.com/web-infra-dev/rslint/internal/plugins/react/reactutil" |
| 10 | + "github.com/web-infra-dev/rslint/internal/rule" |
| 11 | + "github.com/web-infra-dev/rslint/internal/utils" |
| 12 | + "github.com/web-infra-dev/rslint/internal/utils/ecmascript" |
| 13 | +) |
| 14 | + |
| 15 | +//go:embed jsx_tag_spacing.schema.json |
| 16 | +var schemaJSON []byte |
| 17 | + |
| 18 | +var messages = map[string]string{ |
| 19 | + "selfCloseSlashNoSpace": "Whitespace is forbidden between `/` and `>`; write `/>`", |
| 20 | + "selfCloseSlashNeedSpace": "Whitespace is required between `/` and `>`; write `/ >`", |
| 21 | + "closeSlashNoSpace": "Whitespace is forbidden between `<` and `/`; write `</`", |
| 22 | + "closeSlashNeedSpace": "Whitespace is required between `<` and `/`; write `< /`", |
| 23 | + "beforeSelfCloseNoSpace": "A space is forbidden before closing bracket", |
| 24 | + "beforeSelfCloseNeedSpace": "A space is required before closing bracket", |
| 25 | + "beforeSelfCloseNeedNewline": "A newline is required before closing bracket", |
| 26 | + "afterOpenNoSpace": "A space is forbidden after opening bracket", |
| 27 | + "afterOpenNeedSpace": "A space is required after opening bracket", |
| 28 | + "beforeCloseNoSpace": "A space is forbidden before closing bracket", |
| 29 | + "beforeCloseNeedSpace": "Whitespace is required before closing bracket", |
| 30 | + "beforeCloseNeedNewline": "A newline is required before closing bracket", |
| 31 | +} |
| 32 | + |
| 33 | +var JsxTagSpacingRule = rule.Rule{ |
| 34 | + Name: "react/jsx-tag-spacing", |
| 35 | + Schema: rule.NewSchema(schemaJSON), |
| 36 | + Run: func(ctx rule.RuleContext, options []any) rule.RuleListeners { |
| 37 | + config := map[string]string{ |
| 38 | + "closingSlash": "never", "beforeSelfClosing": "always", |
| 39 | + "afterOpening": "never", "beforeClosing": "allow", |
| 40 | + } |
| 41 | + if len(options) > 0 { |
| 42 | + if values, ok := options[0].(map[string]any); ok { |
| 43 | + for key := range config { |
| 44 | + if value, ok := values[key].(string); ok { |
| 45 | + config[key] = value |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + sf := ctx.SourceFile |
| 52 | + s := scanner.NewScanner() |
| 53 | + s.SetText(sf.Text()) |
| 54 | + s.SetSkipTrivia(false) |
| 55 | + // ESLint's isSpaceBetweenTokens ignores whitespace inside comments. |
| 56 | + spaced := func(left, right core.TextRange) bool { |
| 57 | + s.ResetTokenState(left.End()) |
| 58 | + for pos := left.End(); pos < right.Pos(); pos = s.TokenEnd() { |
| 59 | + // Scan skips U+2028/U+2029 even with trivia enabled. |
| 60 | + if ecmascript.SkipLeadingWhitespace(sf.Text(), pos, right.Pos()) != pos { |
| 61 | + return true |
| 62 | + } |
| 63 | + if s.Scan() == ast.KindEndOfFile { |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + return false |
| 68 | + } |
| 69 | + report := func(id string, loc, edit core.TextRange, text string) { |
| 70 | + ctx.ReportRangeWithFixes(loc, rule.RuleMessage{Id: id, Description: messages[id]}, rule.RuleFix{Range: edit, Text: text}) |
| 71 | + } |
| 72 | + insert := func(pos int) core.TextRange { return core.NewTextRange(pos, pos) } |
| 73 | + checkSlash := func(left, right core.TextRange, prefix string) { |
| 74 | + loc := core.NewTextRange(left.Pos(), right.End()) |
| 75 | + switch config["closingSlash"] { |
| 76 | + case "never": |
| 77 | + if spaced(left, right) { |
| 78 | + report(prefix+"NoSpace", loc, core.NewTextRange(left.End(), right.Pos()), "") |
| 79 | + } |
| 80 | + case "always": |
| 81 | + if !spaced(left, right) { |
| 82 | + if prefix == "closeSlash" { |
| 83 | + // Inserting whitespace into `</` would produce invalid TSX. |
| 84 | + ctx.ReportRange(loc, rule.RuleMessage{Id: "closeSlashNeedSpace", Description: messages["closeSlashNeedSpace"]}) |
| 85 | + return |
| 86 | + } |
| 87 | + report(prefix+"NeedSpace", loc, insert(right.Pos()), " ") |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + check := func(node *ast.Node) { |
| 92 | + selfClosing := node.Kind == ast.KindJsxSelfClosingElement |
| 93 | + closing := node.Kind == ast.KindJsxClosingElement |
| 94 | + name := reactutil.GetJsxTagName(node) |
| 95 | + if closing { |
| 96 | + name = node.AsJsxClosingElement().TagName |
| 97 | + } |
| 98 | + if name == nil { |
| 99 | + return |
| 100 | + } |
| 101 | + // Only the tag's immediate children are needed for its brackets. |
| 102 | + // Walking every attribute expression repeats work for nested JSX. |
| 103 | + children := utils.GetChildren(node, sf) |
| 104 | + if len(children) < 3 || children[len(children)-1].Kind != ast.KindGreaterThanToken { |
| 105 | + return |
| 106 | + } |
| 107 | + first := utils.TrimNodeTextRange(sf, children[0]) |
| 108 | + last := utils.TrimNodeTextRange(sf, children[len(children)-1]) |
| 109 | + nameRange := utils.TrimNodeTextRange(sf, name) |
| 110 | + opening := first |
| 111 | + if closing { |
| 112 | + // tsgo stores `</` as one token; ESTree exposes `<` and `/`. |
| 113 | + opening = core.NewTextRange(first.End()-1, first.End()) |
| 114 | + } |
| 115 | + if selfClosing { |
| 116 | + checkSlash(utils.TrimNodeTextRange(sf, children[len(children)-2]), last, "selfCloseSlash") |
| 117 | + } |
| 118 | + option := config["afterOpening"] |
| 119 | + if option != "allow" && (option != "allow-multiline" || utils.IsSameLine(sf, opening.Pos(), nameRange.Pos())) { |
| 120 | + loc := core.NewTextRange(opening.Pos(), nameRange.Pos()) |
| 121 | + if (option == "never" || option == "allow-multiline") && spaced(opening, nameRange) { |
| 122 | + report("afterOpenNoSpace", loc, core.NewTextRange(opening.End(), nameRange.Pos()), "") |
| 123 | + } else if option == "always" && !spaced(opening, nameRange) { |
| 124 | + report("afterOpenNeedSpace", loc, insert(nameRange.Pos()), " ") |
| 125 | + } |
| 126 | + } |
| 127 | + if closing { |
| 128 | + checkSlash(core.NewTextRange(first.Pos(), first.Pos()+1), opening, "closeSlash") |
| 129 | + } |
| 130 | + |
| 131 | + // Upstream uses the whole last attribute (or tag name) for self-closing |
| 132 | + // and proportional checks, but the final token for ordinary `>` checks. |
| 133 | + left := nameRange |
| 134 | + if attrs := reactutil.GetJsxElementAttributes(node); len(attrs) > 0 { |
| 135 | + left = utils.TrimNodeTextRange(sf, attrs[len(attrs)-1]) |
| 136 | + } else if !closing { |
| 137 | + if typeArgs := node.TypeArgumentList(); typeArgs != nil { |
| 138 | + // The parser's boundary identifies the outer `>`, including |
| 139 | + // nested types and comments containing other angle brackets. |
| 140 | + left = scanner.GetRangeOfTokenAtPosition(sf, typeArgs.End()) |
| 141 | + } |
| 142 | + } |
| 143 | + if selfClosing { |
| 144 | + option = config["beforeSelfClosing"] |
| 145 | + } else { |
| 146 | + option = config["beforeClosing"] |
| 147 | + if option != "allow" && option != "proportional-always" { |
| 148 | + previous, ok := utils.TokenBeforePosition(sf, last.Pos()) |
| 149 | + if !ok { |
| 150 | + return |
| 151 | + } |
| 152 | + left = previous.Range() |
| 153 | + } |
| 154 | + } |
| 155 | + if option == "allow" { |
| 156 | + return |
| 157 | + } |
| 158 | + next, ok := utils.TokenAtOrAfter(sf, left.End()) |
| 159 | + if !ok { |
| 160 | + return |
| 161 | + } |
| 162 | + right := next.Range() |
| 163 | + multiline := !utils.IsSameLine(sf, first.Pos(), last.End()) |
| 164 | + prefix := "beforeClose" |
| 165 | + if selfClosing { |
| 166 | + prefix = "beforeSelfClose" |
| 167 | + } |
| 168 | + if option == "proportional-always" && multiline && utils.IsSameLine(sf, left.End(), right.Pos()) { |
| 169 | + report(prefix+"NeedNewline", insert(left.End()), insert(right.Pos()), "\n") |
| 170 | + return |
| 171 | + } |
| 172 | + linePos := left.Pos() |
| 173 | + if selfClosing { |
| 174 | + linePos = left.End() |
| 175 | + } |
| 176 | + if !utils.IsSameLine(sf, linePos, right.Pos()) { |
| 177 | + return |
| 178 | + } |
| 179 | + adjacent := !spaced(left, right) |
| 180 | + loc := core.NewTextRange(left.End(), right.Pos()) |
| 181 | + if selfClosing { |
| 182 | + loc = insert(right.Pos()) |
| 183 | + } |
| 184 | + if option == "never" && !adjacent { |
| 185 | + report(prefix+"NoSpace", loc, core.NewTextRange(left.End(), right.Pos()), "") |
| 186 | + } else if (option == "always" || !closing && option == "proportional-always") && adjacent { |
| 187 | + report(prefix+"NeedSpace", loc, insert(right.Pos()), " ") |
| 188 | + } |
| 189 | + } |
| 190 | + return rule.RuleListeners{ |
| 191 | + ast.KindJsxOpeningElement: check, |
| 192 | + ast.KindJsxSelfClosingElement: check, |
| 193 | + ast.KindJsxClosingElement: check, |
| 194 | + } |
| 195 | + }, |
| 196 | +} |
0 commit comments