forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkarma-config-analyzer.ts
More file actions
168 lines (150 loc) · 5.39 KB
/
karma-config-analyzer.ts
File metadata and controls
168 lines (150 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import ts from '../../third_party/typescript';
export interface RequireInfo {
module: string;
export?: string;
isCall?: boolean;
arguments?: KarmaConfigValue[];
}
export type KarmaConfigValue =
| string
| boolean
| number
| KarmaConfigValue[]
| { [key: string]: KarmaConfigValue }
| RequireInfo
| undefined;
export interface KarmaConfigAnalysis {
settings: Map<string, KarmaConfigValue>;
hasUnsupportedValues: boolean;
}
function isRequireInfo(value: KarmaConfigValue): value is RequireInfo {
return typeof value === 'object' && value !== null && !Array.isArray(value) && 'module' in value;
}
function isSupportedPropertyAssignment(
prop: ts.ObjectLiteralElementLike,
): prop is ts.PropertyAssignment & { name: ts.Identifier | ts.StringLiteral } {
return (
ts.isPropertyAssignment(prop) && (ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name))
);
}
/**
* Analyzes the content of a Karma configuration file to extract its settings.
*
* @param content The string content of the `karma.conf.js` file.
* @returns An object containing the configuration settings and a flag indicating if unsupported values were found.
*/
export function analyzeKarmaConfig(content: string): KarmaConfigAnalysis {
const sourceFile = ts.createSourceFile('karma.conf.js', content, ts.ScriptTarget.Latest, true);
const settings = new Map<string, KarmaConfigValue>();
let hasUnsupportedValues = false;
function visit(node: ts.Node) {
// The Karma configuration is defined within a `config.set({ ... })` call.
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
node.expression.expression.getText(sourceFile) === 'config' &&
node.expression.name.text === 'set' &&
node.arguments.length === 1 &&
ts.isObjectLiteralExpression(node.arguments[0])
) {
// We found `config.set`, now we extract the properties from the object literal.
for (const prop of node.arguments[0].properties) {
if (isSupportedPropertyAssignment(prop)) {
const key = prop.name.text;
const value = extractValue(prop.initializer);
settings.set(key, value);
} else {
hasUnsupportedValues = true;
}
}
} else {
ts.forEachChild(node, visit);
}
}
function extractValue(node: ts.Expression): KarmaConfigValue {
switch (node.kind) {
case ts.SyntaxKind.StringLiteral:
return (node as ts.StringLiteral).text;
case ts.SyntaxKind.NumericLiteral:
return Number((node as ts.NumericLiteral).text);
case ts.SyntaxKind.TrueKeyword:
return true;
case ts.SyntaxKind.FalseKeyword:
return false;
case ts.SyntaxKind.Identifier: {
const identifier = (node as ts.Identifier).text;
if (identifier === '__dirname' || identifier === '__filename') {
return identifier;
}
break;
}
case ts.SyntaxKind.CallExpression: {
const callExpr = node as ts.CallExpression;
// Handle require('...')
if (
ts.isIdentifier(callExpr.expression) &&
callExpr.expression.text === 'require' &&
callExpr.arguments.length === 1 &&
ts.isStringLiteral(callExpr.arguments[0])
) {
return { module: callExpr.arguments[0].text };
}
// Handle calls on a require, e.g. require('path').join()
const calleeValue = extractValue(callExpr.expression);
if (isRequireInfo(calleeValue)) {
return {
...calleeValue,
isCall: true,
arguments: callExpr.arguments.map(extractValue),
};
}
break;
}
case ts.SyntaxKind.PropertyAccessExpression: {
const propAccessExpr = node as ts.PropertyAccessExpression;
// Handle config constants like `config.LOG_INFO`
if (
ts.isIdentifier(propAccessExpr.expression) &&
propAccessExpr.expression.text === 'config'
) {
return `config.${propAccessExpr.name.text}`;
}
const value = extractValue(propAccessExpr.expression);
if (isRequireInfo(value)) {
const currentExport = value.export
? `${value.export}.${propAccessExpr.name.text}`
: propAccessExpr.name.text;
return { ...value, export: currentExport };
}
break;
}
case ts.SyntaxKind.ArrayLiteralExpression:
return (node as ts.ArrayLiteralExpression).elements.map(extractValue);
case ts.SyntaxKind.ObjectLiteralExpression: {
const obj: { [key: string]: KarmaConfigValue } = {};
for (const prop of (node as ts.ObjectLiteralExpression).properties) {
if (isSupportedPropertyAssignment(prop)) {
// Recursively extract values for nested objects.
obj[prop.name.text] = extractValue(prop.initializer);
} else {
hasUnsupportedValues = true;
}
}
return obj;
}
}
// For complex expressions (like variables) that we don't need to resolve,
// we mark the analysis as potentially incomplete.
hasUnsupportedValues = true;
return undefined;
}
visit(sourceFile);
return { settings, hasUnsupportedValues };
}