|
| 1 | +package rule |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/microsoft/typescript-go/shim/ast" |
| 8 | +) |
| 9 | + |
| 10 | +// inlineGlobalsKeywords lists the directive keywords that introduce a |
| 11 | +// `/* global */` comment. "globals" is checked before "global" since it is |
| 12 | +// the longer prefix. |
| 13 | +var inlineGlobalsKeywords = [...]string{"globals", "global"} |
| 14 | + |
| 15 | +// ParseInlineGlobals scans `/* global ... */` / `/* globals ... */` block |
| 16 | +// comments and returns the set of names they declare (name -> declared). |
| 17 | +// |
| 18 | +// This is the DisableManager-equivalent preprocessing step for globals: it |
| 19 | +// runs once per file, over the same real comment tokens DisableManager |
| 20 | +// consumes (not a regex over raw source text, so lookalike text inside a |
| 21 | +// string literal or another comment can't be mistaken for a directive), and |
| 22 | +// its result is merged with config globals before being handed to rules — |
| 23 | +// no rule should parse `/* global */` comments itself. |
| 24 | +// |
| 25 | +// Mirrors ESLint's inline-config handling: only the setting "off" un-declares |
| 26 | +// a name; a bare name or any other setting (e.g. "writable") declares it. |
| 27 | +func ParseInlineGlobals(sourceFile *ast.SourceFile, comments []*ast.CommentRange) map[string]bool { |
| 28 | + if sourceFile.Text() == "" || len(comments) == 0 { |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + text := sourceFile.Text() |
| 33 | + var globals map[string]bool |
| 34 | + |
| 35 | + for _, comment := range comments { |
| 36 | + // `/* global */` is only recognized as a block comment, matching |
| 37 | + // ESLint's convention (and rslint's prior behavior). |
| 38 | + if comment.Kind != ast.KindMultiLineCommentTrivia { |
| 39 | + continue |
| 40 | + } |
| 41 | + content := strings.TrimSpace(text[comment.Pos()+2 : comment.End()-2]) |
| 42 | + |
| 43 | + rest, ok := matchInlineGlobalsDirective(content) |
| 44 | + if !ok { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if globals == nil { |
| 49 | + globals = make(map[string]bool) |
| 50 | + } |
| 51 | + for name, setting := range parseGlobalNameList(rest) { |
| 52 | + globals[name] = setting != "off" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return globals |
| 57 | +} |
| 58 | + |
| 59 | +// matchInlineGlobalsDirective reports whether comment content begins with |
| 60 | +// the "global"/"globals" directive keyword (followed by whitespace or |
| 61 | +// end-of-string, so e.g. "globalConfig setup" is not mistaken for one) and |
| 62 | +// returns the remainder to parse as a name list. |
| 63 | +func matchInlineGlobalsDirective(content string) (string, bool) { |
| 64 | + for _, kw := range inlineGlobalsKeywords { |
| 65 | + if !strings.HasPrefix(content, kw) { |
| 66 | + continue |
| 67 | + } |
| 68 | + rest := content[len(kw):] |
| 69 | + if rest == "" || rest[0] == ' ' || rest[0] == '\t' || rest[0] == '\n' || rest[0] == '\r' { |
| 70 | + return strings.TrimSpace(rest), true |
| 71 | + } |
| 72 | + } |
| 73 | + return "", false |
| 74 | +} |
| 75 | + |
| 76 | +// MergeGlobals combines config-declared globals with inline `/* global */` |
| 77 | +// comment globals into the single set exposed to rules as ctx.Globals — |
| 78 | +// mirroring ESLint's addDeclaredGlobals, which layers inline comments on top |
| 79 | +// of config (inline wins on conflict). Returns nil if both are empty. |
| 80 | +func MergeGlobals(configGlobals, inlineGlobals map[string]bool) map[string]bool { |
| 81 | + if len(configGlobals) == 0 { |
| 82 | + return inlineGlobals |
| 83 | + } |
| 84 | + if len(inlineGlobals) == 0 { |
| 85 | + return configGlobals |
| 86 | + } |
| 87 | + merged := make(map[string]bool, len(configGlobals)+len(inlineGlobals)) |
| 88 | + for name, declared := range configGlobals { |
| 89 | + merged[name] = declared |
| 90 | + } |
| 91 | + for name, declared := range inlineGlobals { |
| 92 | + merged[name] = declared |
| 93 | + } |
| 94 | + return merged |
| 95 | +} |
| 96 | + |
| 97 | +// globalSettingSeparatorPattern collapses whitespace around a `:` or `,` |
| 98 | +// separator (e.g. "foo : writable , bar" -> "foo:writable,bar") before |
| 99 | +// splitting — mirrors ESLint's own parseStringConfig, which does the same |
| 100 | +// normalization so spaces around a separator don't get mistaken for part of |
| 101 | +// the name/setting. |
| 102 | +var globalSettingSeparatorPattern = regexp.MustCompile(`\s*([:,])\s*`) |
| 103 | + |
| 104 | +// parseGlobalNameList parses a comma-and/or-whitespace separated |
| 105 | +// "name[:setting]" list, e.g. "foo, bar:writable baz:off", returning each |
| 106 | +// name mapped to its raw setting ("" when none was given). |
| 107 | +func parseGlobalNameList(s string) map[string]string { |
| 108 | + collapsed := globalSettingSeparatorPattern.ReplaceAllString(strings.TrimSpace(s), "$1") |
| 109 | + |
| 110 | + names := make(map[string]string) |
| 111 | + for _, part := range strings.FieldsFunc(collapsed, func(r rune) bool { |
| 112 | + return r == ',' || r == ' ' || r == '\t' || r == '\n' || r == '\r' |
| 113 | + }) { |
| 114 | + name, setting, _ := strings.Cut(part, ":") |
| 115 | + if name != "" { |
| 116 | + names[name] = setting |
| 117 | + } |
| 118 | + } |
| 119 | + return names |
| 120 | +} |
0 commit comments