• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

source-academy / js-slang / 15237418122

25 May 2025 11:31AM UTC coverage: 77.048% (-3.5%) from 80.538%
15237418122

push

github

web-flow
Rewrite: Stepper (#1742)

3433 of 4826 branches covered (71.14%)

Branch coverage included in aggregate %.

1032 of 1260 new or added lines in 27 files covered. (81.9%)

440 existing lines in 29 files now uncovered.

10099 of 12737 relevant lines covered (79.29%)

142411.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.72
/src/utils/ast/helpers.ts
1
import type es from 'estree'
2

3
import assert from '../assert'
79✔
4
import { simple } from '../walkers'
79✔
5
import { ArrayMap } from '../dict'
79✔
6
import { isIdentifier, isImportDeclaration, isVariableDeclaration } from './typeGuards'
79✔
7

8
export function getModuleDeclarationSource(
79✔
9
  node: Exclude<es.ModuleDeclaration, es.ExportDefaultDeclaration>
10
): string {
11
  assert(
691✔
12
    typeof node.source?.value === 'string',
13
    `Expected ${node.type} to have a source value of type string, got ${node.source?.value}`
14
  )
15
  return node.source.value
691✔
16
}
17

18
/**
19
 * Filters out all import declarations from a program, and sorts them by
20
 * the module they import from
21
 */
22
export function filterImportDeclarations({
79✔
23
  body
24
}: es.Program): [
25
  ArrayMap<string, es.ImportDeclaration>,
26
  Exclude<es.Program['body'][0], es.ImportDeclaration>[]
27
] {
28
  return body.reduce(
1,829✔
29
    ([importNodes, otherNodes], node) => {
30
      if (!isImportDeclaration(node)) return [importNodes, [...otherNodes, node]]
35,221✔
31

32
      const moduleName = getModuleDeclarationSource(node)
23✔
33
      importNodes.add(moduleName, node)
23✔
34
      return [importNodes, otherNodes]
23✔
35
    },
36
    [new ArrayMap(), []] as [
37
      ArrayMap<string, es.ImportDeclaration>,
38
      Exclude<es.Program['body'][0], es.ImportDeclaration>[]
39
    ]
40
  )
41
}
42

43
export function extractIdsFromPattern(pattern: es.Pattern) {
79✔
44
  const identifiers: es.Identifier[] = []
26✔
45

46
  simple(pattern, {
26✔
47
    Identifier: (node: es.Identifier) => {
48
      identifiers.push(node)
26✔
49
    }
50
  })
51

52
  return identifiers
26✔
53
}
54

55
export function getIdsFromDeclaration(
56
  decl: es.Declaration,
57
  allowNull: true
58
): (es.Identifier | null)[]
59
export function getIdsFromDeclaration(decl: es.Declaration, allowNull?: false): es.Identifier[]
60
export function getIdsFromDeclaration(decl: es.Declaration, allowNull?: boolean) {
79✔
61
  const rawIds = isVariableDeclaration(decl)
41✔
62
    ? decl.declarations.flatMap(({ id }) => extractIdsFromPattern(id))
26✔
63
    : [decl.id]
64

65
  if (!allowNull) {
41✔
66
    rawIds.forEach(each => {
41✔
67
      assert(each !== null, 'Encountered a null identifier!')
41✔
68
    })
69
  }
70

71
  return rawIds
41✔
72
}
73

74
export function getSourceVariableDeclaration(decl: es.VariableDeclaration) {
79✔
75
  assert(
22,942✔
76
    decl.declarations.length === 1,
77
    'Variable Declarations in Source should only have 1 declarator!'
78
  )
79

80
  const [declaration] = decl.declarations
22,942✔
81
  assert(
22,942✔
82
    isIdentifier(declaration.id),
83
    'Variable Declarations in Source should be declared using an Identifier!'
84
  )
85

86
  assert(!!declaration.init, 'Variable declarations in Source must be initialized!')
22,942✔
87

88
  return {
22,942✔
89
    id: declaration.id,
90
    init: declaration.init,
91
    loc: declaration.loc
92
  }
93
}
94

95
export const getImportedName = (
79✔
96
  spec: es.ImportSpecifier | es.ImportDefaultSpecifier | es.ExportSpecifier
97
) => {
98
  switch (spec.type) {
159✔
99
    case 'ImportDefaultSpecifier':
100
      return 'default'
32✔
101
    case 'ImportSpecifier':
102
      return spec.imported.name
119✔
103
    case 'ExportSpecifier':
104
      return spec.local.name
8✔
105
  }
106
}
107

108
export const speciferToString = (
79✔
109
  spec: es.ImportSpecifier | es.ImportDefaultSpecifier | es.ExportSpecifier
110
) => {
111
  switch (spec.type) {
5!
112
    case 'ImportSpecifier': {
UNCOV
113
      if (spec.imported.name === spec.local.name) {
×
UNCOV
114
        return spec.imported.name
×
115
      }
UNCOV
116
      return `${spec.imported.name} as ${spec.local.name}`
×
117
    }
118
    case 'ImportDefaultSpecifier':
UNCOV
119
      return `default as ${spec.local.name}`
×
120
    case 'ExportSpecifier': {
121
      if (spec.local.name === spec.exported.name) {
5✔
122
        return spec.local.name
4✔
123
      }
124
      return `${spec.local.name} as ${spec.exported.name}`
1✔
125
    }
126
  }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc