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

riot / compiler / 9813678147

05 Jul 2024 09:13PM CUT coverage: 99.704%. Remained the same
9813678147

push

github

GianlucaGuarini
added: Support for nested css https://github.com/riot/riot/issues/3009

539 of 558 branches covered (96.59%)

6 of 6 new or added lines in 1 file covered. (100.0%)

3368 of 3378 relevant lines covered (99.7%)

38.53 hits per line

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

100.0
/src/generators/javascript/utils.js
1
import {
1✔
2
  RIOT_INTERFACE_WRAPPER_NAME,
1✔
3
  RIOT_MODULE_ID,
1✔
4
  RIOT_TAG_INTERFACE_NAME,
1✔
5
  TAG_LOGIC_PROPERTY,
1✔
6
} from '../../constants.js'
1✔
7
import { builders, types } from '../../utils/build-types.js'
1✔
8
import {
1✔
9
  isExportDefaultStatement,
1✔
10
  isExportNamedDeclaration,
1✔
11
  isImportDeclaration,
1✔
12
  isInterfaceDeclaration,
1✔
13
  isThisExpressionStatement,
1✔
14
  isTypeAliasDeclaration,
1✔
15
} from '../../utils/ast-nodes-checks.js'
1✔
16
import compose from 'cumpa'
1✔
17

1✔
18
/**
1✔
19
 * Find the export default statement
1✔
20
 * @param   { Array } body - tree structure containing the program code
1✔
21
 * @returns { Object } node containing only the code of the export default statement
1✔
22
 */
1✔
23
export function findExportDefaultStatement(body) {
1✔
24
  return body.find(isExportDefaultStatement)
22✔
25
}
22✔
26

1✔
27
/**
1✔
28
 * Find all import declarations
1✔
29
 * @param   { Array } body - tree structure containing the program code
1✔
30
 * @returns { Array } array containing all the import declarations detected
1✔
31
 */
1✔
32
export function findAllImportDeclarations(body) {
1✔
33
  return body.filter(isImportDeclaration)
8✔
34
}
8✔
35

1✔
36
/**
1✔
37
 * Find all the named export declarations
1✔
38
 * @param   { Array } body - tree structure containing the program code
1✔
39
 * @returns { Array } array containing all the named export declarations detected
1✔
40
 */
1✔
41
export function findAllExportNamedDeclarations(body) {
1✔
42
  return body.filter(isExportNamedDeclaration)
3✔
43
}
3✔
44

1✔
45
/**
1✔
46
 * Filter all the import declarations
1✔
47
 * @param   { Array } body - tree structure containing the program code
1✔
48
 * @returns { Array } array containing all the ast expressions without the import declarations
1✔
49
 */
1✔
50
export function filterOutAllImportDeclarations(body) {
1✔
51
  return body.filter((n) => !isImportDeclaration(n))
3✔
52
}
3✔
53

1✔
54
/**
1✔
55
 * Filter all the export declarations
1✔
56
 * @param   { Array } body - tree structure containing the program code
1✔
57
 * @returns { Array } array containing all the ast expressions without the export declarations
1✔
58
 */
1✔
59
export function filterOutAllExportDeclarations(body) {
1✔
60
  return body.filter(
3✔
61
    (n) => !isExportNamedDeclaration(n) || isExportDefaultStatement(n),
3✔
62
  )
3✔
63
}
3✔
64

1✔
65
/**
1✔
66
 * Find the component interface exported
1✔
67
 * @param   { Array } body - tree structure containing the program code
1✔
68
 * @returns { Object|null } the object referencing the component interface if found
1✔
69
 */
1✔
70
export function findComponentInterface(body) {
1✔
71
  const exportNamedDeclarations = body
22✔
72
    .filter(isExportNamedDeclaration)
22✔
73
    .map((n) => n.declaration)
22✔
74
  const types = exportNamedDeclarations.filter(isTypeAliasDeclaration)
22✔
75
  const interfaces = exportNamedDeclarations.filter(isInterfaceDeclaration)
22✔
76
  const isRiotComponentTypeName = ({ typeName }) =>
22✔
77
    typeName && typeName.name
8✔
78
      ? typeName.name === RIOT_TAG_INTERFACE_NAME
8✔
79
      : false
8✔
80
  const extendsRiotComponent = ({ expression }) =>
22✔
81
    expression.name === RIOT_TAG_INTERFACE_NAME
2✔
82

22✔
83
  return (
22✔
84
    types.find(
22✔
85
      (node) =>
22✔
86
        (node.typeAnnotation.types &&
8✔
87
          node.typeAnnotation.types.some(isRiotComponentTypeName)) ||
8✔
88
        isRiotComponentTypeName(node.typeAnnotation),
22✔
89
    ) ||
22✔
90
    interfaces.find(
19✔
91
      (node) => node.extends && node.extends.some(extendsRiotComponent),
19✔
92
    )
19✔
93
  )
22✔
94
}
22✔
95

1✔
96
/**
1✔
97
 * Add the component interface to the export declaration
1✔
98
 * @param   { Object } ast - ast object generated by recast
1✔
99
 * @param   { Object } componentInterface - the component typescript interface
1✔
100
 * @returns { Object } the component object exported combined with the riot typescript interfaces
1✔
101
 */
1✔
102
export function addComponentInterfaceToExportedObject(ast, componentInterface) {
1✔
103
  const body = getProgramBody(ast)
5✔
104
  const RiotComponentWrapperImportSpecifier = builders.importSpecifier(
5✔
105
    builders.identifier(RIOT_INTERFACE_WRAPPER_NAME),
5✔
106
  )
5✔
107
  const componentInterfaceName = componentInterface.id.name
5✔
108
  const riotImportDeclaration = findAllImportDeclarations(body).find(
5✔
109
    (node) => node.source.value === RIOT_MODULE_ID,
5✔
110
  )
5✔
111
  const exportDefaultStatement = body.find(isExportDefaultStatement)
5✔
112
  const objectExport = exportDefaultStatement.declaration
5✔
113

5✔
114
  // add the RiotComponentWrapper to this component imports
5✔
115
  if (riotImportDeclaration) {
5✔
116
    riotImportDeclaration.specifiers.push(RiotComponentWrapperImportSpecifier)
2✔
117
  } else {
5✔
118
    // otherwise create the whole import statement from riot
3✔
119
    body.unshift(
3✔
120
      0,
3✔
121
      builders.importDeclaration(
3✔
122
        [RiotComponentWrapperImportSpecifier],
3✔
123
        builders.stringLiteral(RIOT_MODULE_ID),
3✔
124
      ),
3✔
125
    )
3✔
126
  }
3✔
127

5✔
128
  // override the object export adding the types detected
5✔
129
  exportDefaultStatement.declaration = builders.tsAsExpression(
5✔
130
    objectExport,
5✔
131
    builders.tsTypeReference(
5✔
132
      builders.identifier(RIOT_INTERFACE_WRAPPER_NAME),
5✔
133
      builders.tsTypeParameterInstantiation([
5✔
134
        builders.tsTypeReference(builders.identifier(componentInterfaceName)),
5✔
135
      ]),
5✔
136
    ),
5✔
137
  )
5✔
138

5✔
139
  return ast
5✔
140
}
5✔
141

1✔
142
/**
1✔
143
 * Create the default export declaration interpreting the old riot syntax relying on "this" statements
1✔
144
 * @param   { Array } body - tree structure containing the program code
1✔
145
 * @returns { Object } ExportDefaultDeclaration
1✔
146
 */
1✔
147
export function createDefaultExportFromLegacySyntax(body) {
1✔
148
  return builders.exportDefaultDeclaration(
3✔
149
    builders.functionDeclaration(
3✔
150
      builders.identifier(TAG_LOGIC_PROPERTY),
3✔
151
      [],
3✔
152
      builders.blockStatement([
3✔
153
        ...compose(
3✔
154
          filterOutAllImportDeclarations,
3✔
155
          filterOutAllExportDeclarations,
3✔
156
        )(body),
3✔
157
        builders.returnStatement(builders.thisExpression()),
3✔
158
      ]),
3✔
159
    ),
3✔
160
  )
3✔
161
}
3✔
162

1✔
163
/**
1✔
164
 * Find all the code in an ast program except for the export default statements
1✔
165
 * @param   { Array } body - tree structure containing the program code
1✔
166
 * @returns { Array } array containing all the program code except the export default expressions
1✔
167
 */
1✔
168
export function filterNonExportDefaultStatements(body) {
1✔
169
  return body.filter(
18✔
170
    (node) =>
18✔
171
      !isExportDefaultStatement(node) && !isThisExpressionStatement(node),
18✔
172
  )
18✔
173
}
18✔
174

1✔
175
/**
1✔
176
 * Get the body of the AST structure
1✔
177
 * @param   { Object } ast - ast object generated by recast
1✔
178
 * @returns { Array } array containing the program code
1✔
179
 */
1✔
180
export function getProgramBody(ast) {
1✔
181
  return ast.body || ast.program.body
49✔
182
}
49✔
183

1✔
184
/**
1✔
185
 * Extend the AST adding the new tag method containing our tag sourcecode
1✔
186
 * @param   { Object } ast - current output ast
1✔
187
 * @param   { Object } exportDefaultNode - tag export default node
1✔
188
 * @returns { Object } the output ast having the "tag" key extended with the content of the export default
1✔
189
 */
1✔
190
export function extendTagProperty(ast, exportDefaultNode) {
1✔
191
  types.visit(ast, {
21✔
192
    visitProperty(path) {
21✔
193
      if (path.value.key.name === TAG_LOGIC_PROPERTY) {
84✔
194
        path.value.value = exportDefaultNode.declaration
21✔
195
        return false
21✔
196
      }
21✔
197

63✔
198
      this.traverse(path)
63✔
199
    },
21✔
200
  })
21✔
201

21✔
202
  return ast
21✔
203
}
21✔
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

© 2025 Coveralls, Inc