AppendImport adds (or merges into) a top-level import statement. Imports are keyed by (Module, IsTypeOnly, SideEffect): repeat calls with the same key are merged into one statement, mixed type-only and value imports for the same module emit as separate statements, and a side-effect import (SideEffe
(decl *bindings.ImportDeclaration)
| 246 | // order is sorted alphabetically by module (then by SideEffect, then by |
| 247 | // IsTypeOnly), and within each statement the specifiers are sorted by name. |
| 248 | func (ts *Typescript) AppendImport(decl *bindings.ImportDeclaration) { |
| 249 | if decl == nil { |
| 250 | return |
| 251 | } |
| 252 | for _, existing := range ts.imports { |
| 253 | if existing.Module != decl.Module { |
| 254 | continue |
| 255 | } |
| 256 | if existing.SideEffect != decl.SideEffect { |
| 257 | continue |
| 258 | } |
| 259 | if existing.SideEffect { |
| 260 | // Side-effect imports have no specifiers and no type-only mode, |
| 261 | // so any same-module match is a complete duplicate. |
| 262 | return |
| 263 | } |
| 264 | if existing.IsTypeOnly != decl.IsTypeOnly { |
| 265 | continue |
| 266 | } |
| 267 | for _, n := range decl.Named { |
| 268 | if !containsImportSpecifier(existing.Named, n) { |
| 269 | existing.Named = append(existing.Named, n) |
| 270 | } |
| 271 | } |
| 272 | return |
| 273 | } |
| 274 | ts.imports = append(ts.imports, decl) |
| 275 | } |
| 276 | |
| 277 | func containsImportSpecifier(list []*bindings.ImportSpecifier, target *bindings.ImportSpecifier) bool { |
| 278 | for _, s := range list { |
no test coverage detected