| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func linesOf(n int) []byte { return []byte(strings.Repeat("x\n", n)) } |
| 9 | |
| 10 | func goLinesOf(n int) []byte { |
| 11 | return []byte("package a\n" + strings.Repeat("var _ = 1\n", n)) |
| 12 | } |
| 13 | |
| 14 | func TestSizeRuleCoversTypeScript(t *testing.T) { |
| 15 | for _, tc := range []struct { |
| 16 | name, rel string |
| 17 | data []byte |
| 18 | wantRule string |
| 19 | }{ |
| 20 | {"tsx over the ceiling", "desktop/frontend/src/components/Big.tsx", linesOf(900), ruleFileSize}, |
| 21 | {"ts over the ceiling", "desktop/frontend/src/lib/big.ts", linesOf(900), ruleFileSize}, |
| 22 | {"tsx under the ceiling", "desktop/frontend/src/components/Small.tsx", linesOf(100), ""}, |
| 23 | {"front-end test dir", "desktop/frontend/src/__tests__/big.test.ts", linesOf(900), ruleTestSize}, |
| 24 | {"spec suffix", "desktop/frontend/src/lib/big.spec.ts", linesOf(900), ruleTestSize}, |
| 25 | {"worker ts", "workers/crash-report/src/big.ts", linesOf(900), ruleFileSize}, |
| 26 | {"go file keeps its rule", "internal/agent/big.go", goLinesOf(900), ruleFileSize}, |
| 27 | {"locale table is exempt", "desktop/frontend/src/locales/zh.ts", linesOf(3000), ""}, |
| 28 | } { |
| 29 | t.Run(tc.name, func(t *testing.T) { |
| 30 | s := parseBytes(tc.rel, tc.data) |
| 31 | if s == nil { |
| 32 | t.Fatal("parseBytes returned nil") |
| 33 | } |
| 34 | found := checkSize(s) |
| 35 | if tc.wantRule == "" { |
| 36 | if len(found) != 0 { |
| 37 | t.Fatalf("want no finding, got %+v", found) |
| 38 | } |
| 39 | return |
| 40 | } |
| 41 | if len(found) != 1 || found[0].Rule != tc.wantRule { |
| 42 | t.Fatalf("want one %s finding, got %+v", tc.wantRule, found) |
| 43 | } |
| 44 | }) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // The comment and layering rules are written against the Go AST, so a |
| 49 | // TypeScript file must reach checkSize without ever being handed to them. |
| 50 | func TestTypeScriptCarriesNoGoAST(t *testing.T) { |
| 51 | src := "// ===== section banner =====\nimport { x } from './y'\nexport const a = x\n" |
| 52 | s := parseBytes("desktop/frontend/src/lib/a.ts", []byte(src)) |
| 53 | if s == nil { |
| 54 | t.Fatal("a TypeScript file must still be read for the size rule") |
| 55 | } |
| 56 | if s.file != nil || s.fset != nil { |
| 57 | t.Fatal("a TypeScript file must not carry a Go AST") |
| 58 | } |
| 59 | if refs := s.importRefs(); len(refs) != 0 { |
| 60 | t.Fatalf("TypeScript imports must stay out of the layering graph: %v", refs) |
| 61 | } |
| 62 | if s.lines != 3 { |
| 63 | t.Fatalf("lines = %d, want 3", s.lines) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestLintableExtensions(t *testing.T) { |
| 68 | for name, want := range map[string]bool{ |
| 69 | "a.go": true, |
| 70 | "a.ts": true, |
| 71 | "a.tsx": true, |
| 72 | "a.js": false, |
| 73 | "a.json": false, |
| 74 | "a.css": false, |
| 75 | "a.md": false, |
| 76 | "tsconfig.tsx": true, |
| 77 | } { |
| 78 | if got := lintable(name); got != want { |
| 79 | t.Errorf("lintable(%q) = %v, want %v", name, got, want) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 |