@opencrvs/toolkit 2.2.0-rc.30529dd → 2.2.0-rc.3de6f63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/application-config/index.js +1 -1
- package/dist/authentication/index.js +10 -2
- package/dist/cli.js +586 -303
- package/dist/commons/events/ActionConfig.d.ts +2 -2
- package/dist/commons/events/EventConfig.d.ts +1 -10
- package/dist/commons/events/eventConfigValidation.d.ts +1 -1
- package/dist/commons/events/index.d.ts +1 -0
- package/dist/commons/events/test.utils.d.ts +2 -3
- package/dist/commons/events/toNotificationForm.d.ts +17 -0
- package/dist/commons/events/utils.d.ts +77 -56
- package/dist/environment/custom-types.d.ts +1 -0
- package/dist/environment/custom-types.d.ts.map +1 -1
- package/dist/environment/derived-variables.d.ts +6 -4
- package/dist/environment/derived-variables.d.ts.map +1 -1
- package/dist/environment/derived-variables.js +6 -4
- package/dist/environment/questions.d.ts +14 -2
- package/dist/environment/questions.d.ts.map +1 -1
- package/dist/environment/questions.js +39 -18
- package/dist/environment/registry.d.ts +15 -0
- package/dist/environment/registry.d.ts.map +1 -0
- package/dist/environment/registry.js +112 -0
- package/dist/environment/setup-environment.d.ts.map +1 -1
- package/dist/environment/setup-environment.js +76 -28
- package/dist/events/index.js +60 -97
- package/dist/migrations/add-notify-config.d.ts +40 -0
- package/dist/migrations/add-notify-config.d.ts.map +1 -0
- package/dist/migrations/add-notify-config.js +249 -0
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/index.js +345 -143
- package/dist/migrations/move-declaration-to-declare-action.d.ts.map +1 -1
- package/dist/migrations/move-declaration-to-declare-action.js +27 -144
- package/dist/migrations/read-declaration-through-helper.d.ts.map +1 -1
- package/dist/migrations/read-declaration-through-helper.js +3 -61
- package/dist/migrations/utils.d.ts +61 -0
- package/dist/migrations/utils.d.ts.map +1 -0
- package/dist/migrations/utils.js +218 -0
- package/dist/notification/index.js +47 -75
- package/dist/scopes/index.d.ts +5 -2
- package/dist/scopes/index.js +10 -2
- package/dist/templates/charts-values/opencrvs-services/values.yaml +3 -4
- package/opencrvs-toolkit-2.2.0-rc.3de6f63.tgz +0 -0
- package/package.json +2 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/dist/commons/events/notificationFormFallback.d.ts +0 -10
- package/dist/environment/dockerhub.d.ts +0 -15
- package/dist/environment/dockerhub.d.ts.map +0 -1
- package/dist/environment/dockerhub.js +0 -104
- package/opencrvs-toolkit-2.2.0-rc.30529dd.tgz +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"move-declaration-to-declare-action.d.ts","sourceRoot":"","sources":["../../src/migrations/move-declaration-to-declare-action.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAOL,UAAU,
|
|
1
|
+
{"version":3,"file":"move-declaration-to-declare-action.d.ts","sourceRoot":"","sources":["../../src/migrations/move-declaration-to-declare-action.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAOL,UAAU,EAEX,MAAM,UAAU,CAAA;AA2MjB;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAyC7E;AAED,iBAAe,IAAI,kBA6ClB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
|
|
@@ -53,149 +53,39 @@ exports.main = main;
|
|
|
53
53
|
*/
|
|
54
54
|
const ts_morph_1 = require("ts-morph");
|
|
55
55
|
const path_1 = __importDefault(require("path"));
|
|
56
|
+
const utils_1 = require("./utils");
|
|
56
57
|
const DEFINE_CONFIG_NAME = 'defineConfig';
|
|
57
58
|
const DECLARATION_PROPERTY_NAME = 'declaration';
|
|
58
59
|
const ACTIONS_PROPERTY_NAME = 'actions';
|
|
59
60
|
const DECLARE_ACTION_TYPE = 'DECLARE';
|
|
60
|
-
function location(node) {
|
|
61
|
-
const relPath = path_1.default.relative(process.cwd(), node.getSourceFile().getFilePath());
|
|
62
|
-
return `${relPath}:${node.getStartLineNumber()}`;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Returns true if the call is to the `defineConfig` published by the toolkit,
|
|
66
|
-
* as opposed to a function of the same name declared in the country config.
|
|
67
|
-
*/
|
|
68
|
-
function isToolkitDefineConfigCall(call) {
|
|
69
|
-
var _a;
|
|
70
|
-
const symbol = call.getExpression().getSymbol();
|
|
71
|
-
const resolved = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.getAliasedSymbol()) !== null && _a !== void 0 ? _a : symbol;
|
|
72
|
-
if ((resolved === null || resolved === void 0 ? void 0 : resolved.getName()) !== DEFINE_CONFIG_NAME) {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
return resolved
|
|
76
|
-
.getDeclarations()
|
|
77
|
-
.some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
78
|
-
}
|
|
79
|
-
/** Finds a property given by name, whether written `key: value` or shorthand. */
|
|
80
|
-
function findProperty(literal, name) {
|
|
81
|
-
const property = literal.getProperty(name);
|
|
82
|
-
return ts_morph_1.Node.isPropertyAssignment(property) ||
|
|
83
|
-
ts_morph_1.Node.isShorthandPropertyAssignment(property)
|
|
84
|
-
? property
|
|
85
|
-
: undefined;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Returns false only if the action type's `type` is known to be some other
|
|
89
|
-
* action: a string literal, or a union of them, other than DECLARE.
|
|
90
|
-
*/
|
|
91
|
-
function mayHaveDeclareType(actionType, node) {
|
|
92
|
-
const typeProperty = actionType.getProperty('type');
|
|
93
|
-
if (!typeProperty) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
const type = typeProperty.getTypeAtLocation(node);
|
|
97
|
-
const values = type.isUnion() ? type.getUnionTypes() : [type];
|
|
98
|
-
return !values.every((value) => value.isStringLiteral() && value.getLiteralValue() !== DECLARE_ACTION_TYPE);
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Returns true if the node may be the DECLARE action. An action not written
|
|
102
|
-
* inline (a variable, a call, a spread) may be, unless its type shows it is
|
|
103
|
-
* some other action.
|
|
104
|
-
*/
|
|
105
|
-
function mayBeDeclareAction(node) {
|
|
106
|
-
if (ts_morph_1.Node.isObjectLiteralExpression(node)) {
|
|
107
|
-
return isDeclareAction(node);
|
|
108
|
-
}
|
|
109
|
-
if (ts_morph_1.Node.isSpreadElement(node)) {
|
|
110
|
-
const arrayType = node.getExpression().getType();
|
|
111
|
-
const elementTypes = arrayType.isTuple()
|
|
112
|
-
? arrayType.getTupleElements()
|
|
113
|
-
: [arrayType.getArrayElementType()];
|
|
114
|
-
return elementTypes.some((elementType) => !elementType || mayHaveDeclareType(elementType, node));
|
|
115
|
-
}
|
|
116
|
-
return mayHaveDeclareType(node.getType(), node);
|
|
117
|
-
}
|
|
118
|
-
function isDeclareAction(literal) {
|
|
119
|
-
const type = findProperty(literal, 'type');
|
|
120
|
-
return (ts_morph_1.Node.isPropertyAssignment(type) &&
|
|
121
|
-
type.getInitializerOrThrow().getType().getLiteralValue() ===
|
|
122
|
-
DECLARE_ACTION_TYPE);
|
|
123
|
-
}
|
|
124
61
|
/**
|
|
125
62
|
* Finds the DECLARE action in `actions`, or logs why the config cannot be
|
|
126
63
|
* rewritten and returns undefined.
|
|
127
64
|
*/
|
|
128
65
|
function findDeclareAction(config) {
|
|
129
|
-
const actions = findProperty(config, ACTIONS_PROPERTY_NAME);
|
|
66
|
+
const actions = (0, utils_1.findProperty)(config, ACTIONS_PROPERTY_NAME);
|
|
130
67
|
const actionsValue = ts_morph_1.Node.isPropertyAssignment(actions)
|
|
131
68
|
? actions.getInitializer()
|
|
132
69
|
: undefined;
|
|
133
70
|
if (!ts_morph_1.Node.isArrayLiteralExpression(actionsValue)) {
|
|
134
|
-
console.warn(` [${location(config)}] Skipped: \`${ACTIONS_PROPERTY_NAME}\` is not an array literal. Move \`${DECLARATION_PROPERTY_NAME}\` onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
71
|
+
console.warn(` [${(0, utils_1.location)(config)}] Skipped: \`${ACTIONS_PROPERTY_NAME}\` is not an array literal. Move \`${DECLARATION_PROPERTY_NAME}\` onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
135
72
|
return undefined;
|
|
136
73
|
}
|
|
137
|
-
const candidates = actionsValue
|
|
74
|
+
const candidates = actionsValue
|
|
75
|
+
.getElements()
|
|
76
|
+
.filter((element) => (0, utils_1.mayBeAction)(element, DECLARE_ACTION_TYPE));
|
|
138
77
|
if (candidates.length !== 1 ||
|
|
139
78
|
!ts_morph_1.Node.isObjectLiteralExpression(candidates[0])) {
|
|
140
|
-
console.warn(` [${location(config)}] Skipped: could not find exactly one ${DECLARE_ACTION_TYPE} action written as an object literal in \`${ACTIONS_PROPERTY_NAME}\`. Move \`${DECLARATION_PROPERTY_NAME}\` onto it by hand.`);
|
|
79
|
+
console.warn(` [${(0, utils_1.location)(config)}] Skipped: could not find exactly one ${DECLARE_ACTION_TYPE} action written as an object literal in \`${ACTIONS_PROPERTY_NAME}\`. Move \`${DECLARATION_PROPERTY_NAME}\` onto it by hand.`);
|
|
141
80
|
return undefined;
|
|
142
81
|
}
|
|
143
82
|
const declareAction = candidates[0];
|
|
144
|
-
if (findProperty(declareAction, DECLARATION_PROPERTY_NAME)) {
|
|
145
|
-
console.warn(` [${location(config)}] Skipped: \`${DECLARATION_PROPERTY_NAME}\` is set both at the top level and on the ${DECLARE_ACTION_TYPE} action. Keep only the one on the action.`);
|
|
83
|
+
if ((0, utils_1.findProperty)(declareAction, DECLARATION_PROPERTY_NAME)) {
|
|
84
|
+
console.warn(` [${(0, utils_1.location)(config)}] Skipped: \`${DECLARATION_PROPERTY_NAME}\` is set both at the top level and on the ${DECLARE_ACTION_TYPE} action. Keep only the one on the action.`);
|
|
146
85
|
return undefined;
|
|
147
86
|
}
|
|
148
87
|
return declareAction;
|
|
149
88
|
}
|
|
150
|
-
/** The whitespace the line the node starts on is indented by. */
|
|
151
|
-
function indentationOf(node) {
|
|
152
|
-
var _a;
|
|
153
|
-
var _b;
|
|
154
|
-
const text = node.getSourceFile().getFullText();
|
|
155
|
-
const lineStart = text.lastIndexOf('\n', node.getStart() - 1) + 1;
|
|
156
|
-
return (_b = (_a = /^[ \t]*/.exec(text.slice(lineStart))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : '';
|
|
157
|
-
}
|
|
158
|
-
/** The text from the position to the end of its line, without the line break. */
|
|
159
|
-
function restOfLine(text, position) {
|
|
160
|
-
const lineEnd = text.indexOf('\n', position);
|
|
161
|
-
return text.slice(position, lineEnd === -1 ? text.length : lineEnd);
|
|
162
|
-
}
|
|
163
|
-
function isOnlyComments(text) {
|
|
164
|
-
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(text);
|
|
165
|
-
}
|
|
166
|
-
function layoutOf(property) {
|
|
167
|
-
var _a;
|
|
168
|
-
var _b;
|
|
169
|
-
const text = property.getSourceFile().getFullText();
|
|
170
|
-
const next = property.getNextSibling();
|
|
171
|
-
const hasComma = (next === null || next === void 0 ? void 0 : next.getKind()) === ts_morph_1.SyntaxKind.CommaToken;
|
|
172
|
-
const end = next && hasComma ? next.getEnd() : property.getEnd();
|
|
173
|
-
const trailing = restOfLine(text, end);
|
|
174
|
-
// A comment on the line the previous property ends on is that property's
|
|
175
|
-
const lineBreak = text
|
|
176
|
-
.slice(property.getFullStart(), property.getStart())
|
|
177
|
-
.indexOf('\n');
|
|
178
|
-
if (lineBreak === -1 || !isOnlyComments(trailing)) {
|
|
179
|
-
return {
|
|
180
|
-
ownsLines: false,
|
|
181
|
-
start: property.getStart(true),
|
|
182
|
-
end,
|
|
183
|
-
hasComma,
|
|
184
|
-
trailingComment: ''
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
const firstLine = property.getFullStart() + lineBreak + 1;
|
|
188
|
-
return {
|
|
189
|
-
ownsLines: true,
|
|
190
|
-
start: firstLine + ((_b = (_a = /^\s*/.exec(text.slice(firstLine))) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0),
|
|
191
|
-
end,
|
|
192
|
-
hasComma,
|
|
193
|
-
trailingComment: trailing.trimEnd()
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
function insertAt(position, text) {
|
|
197
|
-
return { start: position, end: position, text };
|
|
198
|
-
}
|
|
199
89
|
/**
|
|
200
90
|
* Returns the edits inserting the property into the DECLARE action after its
|
|
201
91
|
* `label`, or after its `type` when it has no `label`, in the action's own
|
|
@@ -207,13 +97,13 @@ function insertIntoDeclareAction(declareAction, property) {
|
|
|
207
97
|
// `type` is always there: it is how the DECLARE action was found
|
|
208
98
|
const anchor = (_a = declareAction.getProperty('label')) !== null && _a !== void 0 ? _a : declareAction.getPropertyOrThrow('type');
|
|
209
99
|
const text = property.getSourceFile().getFullText();
|
|
210
|
-
const anchorLayout = layoutOf(anchor);
|
|
211
|
-
const layout = layoutOf(property);
|
|
212
|
-
const indent = indentationOf(anchor);
|
|
100
|
+
const anchorLayout = (0, utils_1.layoutOf)(anchor);
|
|
101
|
+
const layout = (0, utils_1.layoutOf)(property);
|
|
102
|
+
const indent = (0, utils_1.indentationOf)(anchor);
|
|
213
103
|
// Copied as text so the value keeps its formatting and its comments, with
|
|
214
104
|
// continuation lines re-indented from the property's indentation to the
|
|
215
105
|
// anchor's
|
|
216
|
-
const oldIndent = indentationOf(property);
|
|
106
|
+
const oldIndent = (0, utils_1.indentationOf)(property);
|
|
217
107
|
const reindent = (lines) => lines
|
|
218
108
|
.split('\n')
|
|
219
109
|
.map((line, index) => index > 0 && line.startsWith(oldIndent)
|
|
@@ -222,25 +112,25 @@ function insertIntoDeclareAction(declareAction, property) {
|
|
|
222
112
|
.join('\n');
|
|
223
113
|
if (anchorLayout.ownsLines) {
|
|
224
114
|
const moved = reindent(text.slice(layout.start, property.getEnd()));
|
|
225
|
-
const position = anchorLayout.end + restOfLine(text, anchorLayout.end).length;
|
|
115
|
+
const position = anchorLayout.end + (0, utils_1.restOfLine)(text, anchorLayout.end).length;
|
|
226
116
|
const insertion = `\n${indent}${moved}${anchorLayout.hasComma ? ',' : ''}${layout.trailingComment}`;
|
|
227
117
|
if (anchorLayout.hasComma) {
|
|
228
|
-
return [insertAt(position, insertion)];
|
|
118
|
+
return [(0, utils_1.insertAt)(position, insertion)];
|
|
229
119
|
}
|
|
230
120
|
// The anchor was the last property: it needs a comma now
|
|
231
121
|
return position === anchor.getEnd()
|
|
232
|
-
? [insertAt(position, `,${insertion}`)]
|
|
233
|
-
: [insertAt(anchor.getEnd(), ','), insertAt(position, insertion)];
|
|
122
|
+
? [(0, utils_1.insertAt)(position, `,${insertion}`)]
|
|
123
|
+
: [(0, utils_1.insertAt)(anchor.getEnd(), ','), (0, utils_1.insertAt)(position, insertion)];
|
|
234
124
|
}
|
|
235
125
|
const droppedComments = text.slice(layout.start, property.getStart(true)).trim() !== '' ||
|
|
236
126
|
layout.trailingComment !== '';
|
|
237
127
|
if (droppedComments) {
|
|
238
|
-
console.warn(` [${location(property)}] The comments next to \`${DECLARATION_PROPERTY_NAME}\` were not moved with it. Move them onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
128
|
+
console.warn(` [${(0, utils_1.location)(property)}] The comments next to \`${DECLARATION_PROPERTY_NAME}\` were not moved with it. Move them onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
239
129
|
}
|
|
240
130
|
const isMultiLine = declareAction.getText().includes('\n');
|
|
241
131
|
const separator = isMultiLine ? `,\n${indent}` : ', ';
|
|
242
132
|
return [
|
|
243
|
-
insertAt(anchor.getEnd(), `${separator}${reindent(property.getText(true))}`)
|
|
133
|
+
(0, utils_1.insertAt)(anchor.getEnd(), `${separator}${reindent(property.getText(true))}`)
|
|
244
134
|
];
|
|
245
135
|
}
|
|
246
136
|
/**
|
|
@@ -250,16 +140,16 @@ function insertIntoDeclareAction(declareAction, property) {
|
|
|
250
140
|
function removeFromConfig(property) {
|
|
251
141
|
var _a;
|
|
252
142
|
const text = property.getSourceFile().getFullText();
|
|
253
|
-
const layout = layoutOf(property);
|
|
143
|
+
const layout = (0, utils_1.layoutOf)(property);
|
|
254
144
|
// The comma before the property, past any comments, which ts-morph lists
|
|
255
145
|
// as siblings
|
|
256
146
|
const previousComma = (_a = property
|
|
257
147
|
.getPreviousSiblings()
|
|
258
|
-
.find((sibling) => !isOnlyComments(sibling.getText()))) === null || _a === void 0 ? void 0 : _a.asKind(ts_morph_1.SyntaxKind.CommaToken);
|
|
148
|
+
.find((sibling) => !(0, utils_1.isOnlyComments)(sibling.getText()))) === null || _a === void 0 ? void 0 : _a.asKind(ts_morph_1.SyntaxKind.CommaToken);
|
|
259
149
|
if (layout.ownsLines) {
|
|
260
150
|
const lines = {
|
|
261
151
|
start: text.lastIndexOf('\n', layout.start - 1),
|
|
262
|
-
end: layout.end + restOfLine(text, layout.end).length,
|
|
152
|
+
end: layout.end + (0, utils_1.restOfLine)(text, layout.end).length,
|
|
263
153
|
text: ''
|
|
264
154
|
};
|
|
265
155
|
// The last property: drop the comma before it too
|
|
@@ -297,7 +187,7 @@ function findConfigLiteral(call) {
|
|
|
297
187
|
return config;
|
|
298
188
|
}
|
|
299
189
|
if (config.getType().getProperty(DECLARATION_PROPERTY_NAME)) {
|
|
300
|
-
console.warn(` [${location(config)}] Skipped: \`${DEFINE_CONFIG_NAME}\` is not given an object literal. If its config has a top-level \`${DECLARATION_PROPERTY_NAME}\`, move it onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
190
|
+
console.warn(` [${(0, utils_1.location)(config)}] Skipped: \`${DEFINE_CONFIG_NAME}\` is not given an object literal. If its config has a top-level \`${DECLARATION_PROPERTY_NAME}\`, move it onto the ${DECLARE_ACTION_TYPE} action by hand.`);
|
|
301
191
|
}
|
|
302
192
|
return undefined;
|
|
303
193
|
}
|
|
@@ -310,15 +200,13 @@ function findConfigLiteral(call) {
|
|
|
310
200
|
function moveDeclarationToDeclareAction(sourceFile) {
|
|
311
201
|
const configs = sourceFile
|
|
312
202
|
.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression)
|
|
313
|
-
.filter(isToolkitDefineConfigCall)
|
|
203
|
+
.filter(utils_1.isToolkitDefineConfigCall)
|
|
314
204
|
.map(findConfigLiteral)
|
|
315
205
|
.filter(ts_morph_1.Node.isObjectLiteralExpression);
|
|
316
|
-
// Collected as text edits and applied together, as editing a node through
|
|
317
|
-
// ts-morph invalidates nodes still waiting to be edited
|
|
318
206
|
const edits = [];
|
|
319
207
|
let changes = 0;
|
|
320
208
|
for (const config of configs) {
|
|
321
|
-
const declaration = findProperty(config, DECLARATION_PROPERTY_NAME);
|
|
209
|
+
const declaration = (0, utils_1.findProperty)(config, DECLARATION_PROPERTY_NAME);
|
|
322
210
|
if (!declaration) {
|
|
323
211
|
continue;
|
|
324
212
|
}
|
|
@@ -326,19 +214,14 @@ function moveDeclarationToDeclareAction(sourceFile) {
|
|
|
326
214
|
if (!declareAction) {
|
|
327
215
|
continue;
|
|
328
216
|
}
|
|
329
|
-
console.log(` [${location(declaration)}] Moved \`${declaration.getText()}\` onto the ${DECLARE_ACTION_TYPE} action`);
|
|
217
|
+
console.log(` [${(0, utils_1.location)(declaration)}] Moved \`${declaration.getText()}\` onto the ${DECLARE_ACTION_TYPE} action`);
|
|
330
218
|
edits.push(...insertIntoDeclareAction(declareAction, declaration), ...removeFromConfig(declaration));
|
|
331
219
|
changes++;
|
|
332
220
|
}
|
|
333
221
|
if (changes === 0) {
|
|
334
222
|
return 0;
|
|
335
223
|
}
|
|
336
|
-
|
|
337
|
-
// the ones still to apply untouched
|
|
338
|
-
const text = edits
|
|
339
|
-
.sort((a, b) => b.start - a.start)
|
|
340
|
-
.reduce((result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end), sourceFile.getFullText());
|
|
341
|
-
sourceFile.replaceWithText(text);
|
|
224
|
+
(0, utils_1.applyTextEdits)(sourceFile, edits);
|
|
342
225
|
return changes;
|
|
343
226
|
}
|
|
344
227
|
function main() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-declaration-through-helper.d.ts","sourceRoot":"","sources":["../../src/migrations/read-declaration-through-helper.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAIL,UAAU,EAGX,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"read-declaration-through-helper.d.ts","sourceRoot":"","sources":["../../src/migrations/read-declaration-through-helper.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,OAAO,EAIL,UAAU,EAGX,MAAM,UAAU,CAAA;AA8GjB;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAwDtE;AAED,iBAAe,IAAI,kBA6ClB;AAED,OAAO,EAAE,IAAI,EAAE,CAAA"}
|
|
@@ -54,10 +54,10 @@ exports.main = main;
|
|
|
54
54
|
*/
|
|
55
55
|
const ts_morph_1 = require("ts-morph");
|
|
56
56
|
const path_1 = __importDefault(require("path"));
|
|
57
|
+
const utils_1 = require("./utils");
|
|
57
58
|
const EVENT_CONFIG_TYPE_NAME = 'EventConfig';
|
|
58
59
|
const DECLARATION_PROPERTY_NAME = 'declaration';
|
|
59
60
|
const GET_DECLARATION_NAME = 'getDeclaration';
|
|
60
|
-
const TOOLKIT_EVENTS_MODULE = '@opencrvs/toolkit/events';
|
|
61
61
|
/**
|
|
62
62
|
* Returns true if the type is the `EventConfig` type published by the toolkit,
|
|
63
63
|
* as opposed to a type of the same name declared in the country config itself.
|
|
@@ -122,64 +122,6 @@ function warnSpreadDeclarations(sourceFile) {
|
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
|
-
/** Finds the value import of the toolkit's events module, if the file has one. */
|
|
126
|
-
function findToolkitImport(sourceFile) {
|
|
127
|
-
// A type-only import cannot carry a function, and a namespace import cannot
|
|
128
|
-
// be given named imports, so neither is extended
|
|
129
|
-
return sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE &&
|
|
130
|
-
!declaration.isTypeOnly() &&
|
|
131
|
-
!declaration.getNamespaceImport());
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Returns the name `getDeclaration` is called by in the file: its local alias
|
|
135
|
-
* when the file already imports it from the toolkit, `getDeclaration` when the
|
|
136
|
-
* name is free, or undefined when the file binds `getDeclaration` to
|
|
137
|
-
* something else.
|
|
138
|
-
*/
|
|
139
|
-
function findGetDeclarationName(sourceFile) {
|
|
140
|
-
var _a, _b;
|
|
141
|
-
var _c;
|
|
142
|
-
const imported = (_a = findToolkitImport(sourceFile)) === null || _a === void 0 ? void 0 : _a.getNamedImports().find((namedImport) => namedImport.getName() === GET_DECLARATION_NAME);
|
|
143
|
-
if (imported) {
|
|
144
|
-
return (_c = (_b = imported.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : GET_DECLARATION_NAME;
|
|
145
|
-
}
|
|
146
|
-
if (sourceFile.getLocal(GET_DECLARATION_NAME)) {
|
|
147
|
-
return undefined;
|
|
148
|
-
}
|
|
149
|
-
return GET_DECLARATION_NAME;
|
|
150
|
-
}
|
|
151
|
-
function addGetDeclarationImport(sourceFile) {
|
|
152
|
-
const toolkitImport = findToolkitImport(sourceFile);
|
|
153
|
-
if (!toolkitImport) {
|
|
154
|
-
const imports = sourceFile.getImportDeclarations();
|
|
155
|
-
const lastImport = imports[imports.length - 1];
|
|
156
|
-
// Inserted as text so the import keeps the country config's style
|
|
157
|
-
// (single quotes, no semicolon) rather than ts-morph's defaults
|
|
158
|
-
sourceFile.insertStatements(lastImport ? lastImport.getChildIndex() + 1 : 0, `import { ${GET_DECLARATION_NAME} } from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const alreadyImported = toolkitImport
|
|
162
|
-
.getNamedImports()
|
|
163
|
-
.some((namedImport) => namedImport.getName() === GET_DECLARATION_NAME);
|
|
164
|
-
if (alreadyImported) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const namedImports = toolkitImport.getNamedImports();
|
|
168
|
-
// ts-morph's addNamedImport collapses a multi-line import onto one line, so
|
|
169
|
-
// the import is rewritten keeping one name per line when it had them
|
|
170
|
-
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
171
|
-
toolkitImport.addNamedImport(GET_DECLARATION_NAME);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
const names = [
|
|
175
|
-
...namedImports.map((namedImport) => namedImport.getText()),
|
|
176
|
-
GET_DECLARATION_NAME
|
|
177
|
-
];
|
|
178
|
-
const specifiers = toolkitImport.getText().includes('\n')
|
|
179
|
-
? `{\n ${names.join(',\n ')}\n}`
|
|
180
|
-
: `{ ${names.join(', ')} }`;
|
|
181
|
-
toolkitImport.replaceWithText(`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
182
|
-
}
|
|
183
125
|
/**
|
|
184
126
|
* Rewrites every `x.declaration` read on an `EventConfig` in the file to
|
|
185
127
|
* `getDeclaration(x)`.
|
|
@@ -209,7 +151,7 @@ function rewriteDeclarationReads(sourceFile) {
|
|
|
209
151
|
if (reads.length === 0) {
|
|
210
152
|
return 0;
|
|
211
153
|
}
|
|
212
|
-
const getDeclarationName =
|
|
154
|
+
const getDeclarationName = (0, utils_1.findToolkitImportName)(sourceFile, GET_DECLARATION_NAME);
|
|
213
155
|
if (!getDeclarationName) {
|
|
214
156
|
console.warn(` [${location(reads[reads.length - 1])}] Skipped ${reads.length} read(s): the file already declares its own \`${GET_DECLARATION_NAME}\`. Rewrite them to use the toolkit's \`${GET_DECLARATION_NAME}()\` by hand.`);
|
|
215
157
|
return 0;
|
|
@@ -218,7 +160,7 @@ function rewriteDeclarationReads(sourceFile) {
|
|
|
218
160
|
console.log(` [${location(read)}] ${read.getText()} -> ${getDeclarationName}(${read.getExpression().getText()})`);
|
|
219
161
|
read.replaceWithText(`${getDeclarationName}(${read.getExpression().getText()})`);
|
|
220
162
|
}
|
|
221
|
-
|
|
163
|
+
(0, utils_1.addToolkitImport)(sourceFile, GET_DECLARATION_NAME);
|
|
222
164
|
return reads.length;
|
|
223
165
|
}
|
|
224
166
|
function main() {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers shared by the codemods, kept apart so a codemod can be removed
|
|
3
|
+
* without breaking the others.
|
|
4
|
+
*/
|
|
5
|
+
import { CallExpression, Node, ObjectLiteralExpression, SourceFile } from 'ts-morph';
|
|
6
|
+
export declare function location(node: Node): string;
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the call is to the `defineConfig` published by the toolkit,
|
|
9
|
+
* as opposed to a function of the same name declared in the country config.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isToolkitDefineConfigCall(call: CallExpression): boolean;
|
|
12
|
+
/** Finds a property given by name, whether written `key: value` or shorthand. */
|
|
13
|
+
export declare function findProperty(literal: ObjectLiteralExpression, name: string): import("ts-morph").PropertyAssignment | import("ts-morph").ShorthandPropertyAssignment | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the node may be the action of the given type. An action not
|
|
16
|
+
* written inline (a variable, a call, a spread) may be, unless its type shows
|
|
17
|
+
* it is some other action.
|
|
18
|
+
*/
|
|
19
|
+
export declare function mayBeAction(node: Node, actionType: string): boolean;
|
|
20
|
+
export declare function isAction(literal: ObjectLiteralExpression, actionType: string): boolean;
|
|
21
|
+
/** The whitespace the line the node starts on is indented by. */
|
|
22
|
+
export declare function indentationOf(node: Node): string;
|
|
23
|
+
/** The text from the position to the end of its line, without the line break. */
|
|
24
|
+
export declare function restOfLine(text: string, position: number): string;
|
|
25
|
+
export declare function isOnlyComments(text: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Where a property sits in its object literal. A property on lines of its own
|
|
28
|
+
* owns the comments on the lines above it and the comment after it on its last
|
|
29
|
+
* line, and they move with it.
|
|
30
|
+
*/
|
|
31
|
+
export interface PropertyLayout {
|
|
32
|
+
ownsLines: boolean;
|
|
33
|
+
/** The first comment above the property, or the property itself. */
|
|
34
|
+
start: number;
|
|
35
|
+
/** After the comma following the property, or the property when it has none. */
|
|
36
|
+
end: number;
|
|
37
|
+
hasComma: boolean;
|
|
38
|
+
/** The comment after the property on its last line, with the space before it. */
|
|
39
|
+
trailingComment: string;
|
|
40
|
+
}
|
|
41
|
+
export declare function layoutOf(property: Node): PropertyLayout;
|
|
42
|
+
export interface TextEdit {
|
|
43
|
+
start: number;
|
|
44
|
+
end: number;
|
|
45
|
+
text: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function insertAt(position: number, text: string): TextEdit;
|
|
48
|
+
/**
|
|
49
|
+
* Applies the edits to the file together, as editing a node through ts-morph
|
|
50
|
+
* invalidates nodes still waiting to be edited.
|
|
51
|
+
*/
|
|
52
|
+
export declare function applyTextEdits(sourceFile: SourceFile, edits: TextEdit[]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the name a toolkit export is called by in the file: its local alias
|
|
55
|
+
* when the file already imports it from the toolkit, its own name when the
|
|
56
|
+
* name is free, or undefined when the file binds that name to something else.
|
|
57
|
+
*/
|
|
58
|
+
export declare function findToolkitImportName(sourceFile: SourceFile, name: string): string | undefined;
|
|
59
|
+
/** Adds the toolkit export to the file's toolkit import, unless already imported. */
|
|
60
|
+
export declare function addToolkitImport(sourceFile: SourceFile, name: string): void;
|
|
61
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/migrations/utils.ts"],"names":[],"mappings":"AAWA;;;GAGG;AAEH,OAAO,EACL,cAAc,EACd,IAAI,EACJ,uBAAuB,EACvB,UAAU,EAGX,MAAM,UAAU,CAAA;AAMjB,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,UAMlC;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,cAAc,WAW7D;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,OAAO,EAAE,uBAAuB,EAAE,IAAI,EAAE,MAAM,sGAO1E;AA0BD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,WAkBzD;AAED,wBAAgB,QAAQ,CAAC,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,WAO5E;AAED,iEAAiE;AACjE,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,UAKvC;AAED,iFAAiF;AACjF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAIxD;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED;;;;GAIG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAA;IAClB,oEAAoE;IACpE,KAAK,EAAE,MAAM,CAAA;IACb,gFAAgF;IAChF,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,OAAO,CAAA;IACjB,iFAAiF;IACjF,eAAe,EAAE,MAAM,CAAA;CACxB;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,IAAI,GAAG,cAAc,CA+BvD;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;CACb;AAED,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,QAAQ,CAEjE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,QAYvE;AAcD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,sBAczE;AAED,qFAAqF;AACrF,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,QA4CpE"}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*
|
|
7
|
+
* OpenCRVS is also distributed under the terms of the Civil Registration
|
|
8
|
+
* & Healthcare Disclaimer located at http://opencrvs.org/license.
|
|
9
|
+
*
|
|
10
|
+
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
|
|
11
|
+
*/
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.location = location;
|
|
17
|
+
exports.isToolkitDefineConfigCall = isToolkitDefineConfigCall;
|
|
18
|
+
exports.findProperty = findProperty;
|
|
19
|
+
exports.mayBeAction = mayBeAction;
|
|
20
|
+
exports.isAction = isAction;
|
|
21
|
+
exports.indentationOf = indentationOf;
|
|
22
|
+
exports.restOfLine = restOfLine;
|
|
23
|
+
exports.isOnlyComments = isOnlyComments;
|
|
24
|
+
exports.layoutOf = layoutOf;
|
|
25
|
+
exports.insertAt = insertAt;
|
|
26
|
+
exports.applyTextEdits = applyTextEdits;
|
|
27
|
+
exports.findToolkitImportName = findToolkitImportName;
|
|
28
|
+
exports.addToolkitImport = addToolkitImport;
|
|
29
|
+
/**
|
|
30
|
+
* Helpers shared by the codemods, kept apart so a codemod can be removed
|
|
31
|
+
* without breaking the others.
|
|
32
|
+
*/
|
|
33
|
+
const ts_morph_1 = require("ts-morph");
|
|
34
|
+
const path_1 = __importDefault(require("path"));
|
|
35
|
+
const DEFINE_CONFIG_NAME = 'defineConfig';
|
|
36
|
+
const TOOLKIT_EVENTS_MODULE = '@opencrvs/toolkit/events';
|
|
37
|
+
function location(node) {
|
|
38
|
+
const relPath = path_1.default.relative(process.cwd(), node.getSourceFile().getFilePath());
|
|
39
|
+
return `${relPath}:${node.getStartLineNumber()}`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns true if the call is to the `defineConfig` published by the toolkit,
|
|
43
|
+
* as opposed to a function of the same name declared in the country config.
|
|
44
|
+
*/
|
|
45
|
+
function isToolkitDefineConfigCall(call) {
|
|
46
|
+
var _a;
|
|
47
|
+
const symbol = call.getExpression().getSymbol();
|
|
48
|
+
const resolved = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.getAliasedSymbol()) !== null && _a !== void 0 ? _a : symbol;
|
|
49
|
+
if ((resolved === null || resolved === void 0 ? void 0 : resolved.getName()) !== DEFINE_CONFIG_NAME) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return resolved
|
|
53
|
+
.getDeclarations()
|
|
54
|
+
.some((declaration) => declaration.getSourceFile().isDeclarationFile());
|
|
55
|
+
}
|
|
56
|
+
/** Finds a property given by name, whether written `key: value` or shorthand. */
|
|
57
|
+
function findProperty(literal, name) {
|
|
58
|
+
const property = literal.getProperty(name);
|
|
59
|
+
return ts_morph_1.Node.isPropertyAssignment(property) ||
|
|
60
|
+
ts_morph_1.Node.isShorthandPropertyAssignment(property)
|
|
61
|
+
? property
|
|
62
|
+
: undefined;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns false only if the action type's `type` is known to be some other
|
|
66
|
+
* action: a string literal, or a union of them, other than the given one.
|
|
67
|
+
*/
|
|
68
|
+
function mayHaveActionType(actionType, node, expectedActionType) {
|
|
69
|
+
const typeProperty = actionType.getProperty('type');
|
|
70
|
+
if (!typeProperty) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
const type = typeProperty.getTypeAtLocation(node);
|
|
74
|
+
const values = type.isUnion() ? type.getUnionTypes() : [type];
|
|
75
|
+
return !values.every((value) => value.isStringLiteral() && value.getLiteralValue() !== expectedActionType);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns true if the node may be the action of the given type. An action not
|
|
79
|
+
* written inline (a variable, a call, a spread) may be, unless its type shows
|
|
80
|
+
* it is some other action.
|
|
81
|
+
*/
|
|
82
|
+
function mayBeAction(node, actionType) {
|
|
83
|
+
if (ts_morph_1.Node.isObjectLiteralExpression(node)) {
|
|
84
|
+
return isAction(node, actionType);
|
|
85
|
+
}
|
|
86
|
+
if (ts_morph_1.Node.isSpreadElement(node)) {
|
|
87
|
+
const arrayType = node.getExpression().getType();
|
|
88
|
+
const elementTypes = arrayType.isTuple()
|
|
89
|
+
? arrayType.getTupleElements()
|
|
90
|
+
: [arrayType.getArrayElementType()];
|
|
91
|
+
return elementTypes.some((elementType) => !elementType || mayHaveActionType(elementType, node, actionType));
|
|
92
|
+
}
|
|
93
|
+
return mayHaveActionType(node.getType(), node, actionType);
|
|
94
|
+
}
|
|
95
|
+
function isAction(literal, actionType) {
|
|
96
|
+
const type = findProperty(literal, 'type');
|
|
97
|
+
return (ts_morph_1.Node.isPropertyAssignment(type) &&
|
|
98
|
+
type.getInitializerOrThrow().getType().getLiteralValue() === actionType);
|
|
99
|
+
}
|
|
100
|
+
/** The whitespace the line the node starts on is indented by. */
|
|
101
|
+
function indentationOf(node) {
|
|
102
|
+
var _a;
|
|
103
|
+
var _b;
|
|
104
|
+
const text = node.getSourceFile().getFullText();
|
|
105
|
+
const lineStart = text.lastIndexOf('\n', node.getStart() - 1) + 1;
|
|
106
|
+
return (_b = (_a = /^[ \t]*/.exec(text.slice(lineStart))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : '';
|
|
107
|
+
}
|
|
108
|
+
/** The text from the position to the end of its line, without the line break. */
|
|
109
|
+
function restOfLine(text, position) {
|
|
110
|
+
const lineEnd = text.indexOf('\n', position);
|
|
111
|
+
return text.slice(position, lineEnd === -1 ? text.length : lineEnd);
|
|
112
|
+
}
|
|
113
|
+
function isOnlyComments(text) {
|
|
114
|
+
return /^\s*(?:\/\/.*|\/\*.*?\*\/\s*)*$/.test(text);
|
|
115
|
+
}
|
|
116
|
+
function layoutOf(property) {
|
|
117
|
+
var _a;
|
|
118
|
+
var _b;
|
|
119
|
+
const text = property.getSourceFile().getFullText();
|
|
120
|
+
const next = property.getNextSibling();
|
|
121
|
+
const hasComma = (next === null || next === void 0 ? void 0 : next.getKind()) === ts_morph_1.SyntaxKind.CommaToken;
|
|
122
|
+
const end = next && hasComma ? next.getEnd() : property.getEnd();
|
|
123
|
+
const trailing = restOfLine(text, end);
|
|
124
|
+
// A comment on the line the previous property ends on is that property's
|
|
125
|
+
const lineBreak = text
|
|
126
|
+
.slice(property.getFullStart(), property.getStart())
|
|
127
|
+
.indexOf('\n');
|
|
128
|
+
if (lineBreak === -1 || !isOnlyComments(trailing)) {
|
|
129
|
+
return {
|
|
130
|
+
ownsLines: false,
|
|
131
|
+
start: property.getStart(true),
|
|
132
|
+
end,
|
|
133
|
+
hasComma,
|
|
134
|
+
trailingComment: ''
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const firstLine = property.getFullStart() + lineBreak + 1;
|
|
138
|
+
return {
|
|
139
|
+
ownsLines: true,
|
|
140
|
+
start: firstLine + ((_b = (_a = /^\s*/.exec(text.slice(firstLine))) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0),
|
|
141
|
+
end,
|
|
142
|
+
hasComma,
|
|
143
|
+
trailingComment: trailing.trimEnd()
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function insertAt(position, text) {
|
|
147
|
+
return { start: position, end: position, text };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Applies the edits to the file together, as editing a node through ts-morph
|
|
151
|
+
* invalidates nodes still waiting to be edited.
|
|
152
|
+
*/
|
|
153
|
+
function applyTextEdits(sourceFile, edits) {
|
|
154
|
+
// Applied from the end of the file, so each edit leaves the positions of
|
|
155
|
+
// the ones still to apply untouched
|
|
156
|
+
const text = [...edits]
|
|
157
|
+
.sort((a, b) => b.start - a.start)
|
|
158
|
+
.reduce((result, edit) => result.slice(0, edit.start) + edit.text + result.slice(edit.end), sourceFile.getFullText());
|
|
159
|
+
sourceFile.replaceWithText(text);
|
|
160
|
+
}
|
|
161
|
+
/** Finds the value import of the toolkit's events module, if the file has one. */
|
|
162
|
+
function findToolkitImport(sourceFile) {
|
|
163
|
+
// A type-only import cannot carry a function, and a namespace import cannot
|
|
164
|
+
// be given named imports, so neither is extended
|
|
165
|
+
return sourceFile.getImportDeclaration((declaration) => declaration.getModuleSpecifierValue() === TOOLKIT_EVENTS_MODULE &&
|
|
166
|
+
!declaration.isTypeOnly() &&
|
|
167
|
+
!declaration.getNamespaceImport());
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Returns the name a toolkit export is called by in the file: its local alias
|
|
171
|
+
* when the file already imports it from the toolkit, its own name when the
|
|
172
|
+
* name is free, or undefined when the file binds that name to something else.
|
|
173
|
+
*/
|
|
174
|
+
function findToolkitImportName(sourceFile, name) {
|
|
175
|
+
var _a, _b;
|
|
176
|
+
var _c;
|
|
177
|
+
const imported = (_a = findToolkitImport(sourceFile)) === null || _a === void 0 ? void 0 : _a.getNamedImports().find((namedImport) => namedImport.getName() === name);
|
|
178
|
+
if (imported) {
|
|
179
|
+
return (_c = (_b = imported.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : name;
|
|
180
|
+
}
|
|
181
|
+
if (sourceFile.getLocal(name)) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
return name;
|
|
185
|
+
}
|
|
186
|
+
/** Adds the toolkit export to the file's toolkit import, unless already imported. */
|
|
187
|
+
function addToolkitImport(sourceFile, name) {
|
|
188
|
+
const toolkitImport = findToolkitImport(sourceFile);
|
|
189
|
+
if (!toolkitImport) {
|
|
190
|
+
const imports = sourceFile.getImportDeclarations();
|
|
191
|
+
const lastImport = imports[imports.length - 1];
|
|
192
|
+
// Inserted as text so the import keeps the country config's style
|
|
193
|
+
// (single quotes, no semicolon) rather than ts-morph's defaults
|
|
194
|
+
sourceFile.insertStatements(lastImport ? lastImport.getChildIndex() + 1 : 0, `import { ${name} } from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const alreadyImported = toolkitImport
|
|
198
|
+
.getNamedImports()
|
|
199
|
+
.some((namedImport) => namedImport.getName() === name);
|
|
200
|
+
if (alreadyImported) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const namedImports = toolkitImport.getNamedImports();
|
|
204
|
+
// ts-morph's addNamedImport collapses a multi-line import onto one line, so
|
|
205
|
+
// the import is rewritten keeping one name per line when it had them
|
|
206
|
+
if (toolkitImport.getDefaultImport() || namedImports.length === 0) {
|
|
207
|
+
toolkitImport.addNamedImport(name);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const names = [
|
|
211
|
+
...namedImports.map((namedImport) => namedImport.getText()),
|
|
212
|
+
name
|
|
213
|
+
];
|
|
214
|
+
const specifiers = toolkitImport.getText().includes('\n')
|
|
215
|
+
? `{\n ${names.join(',\n ')}\n}`
|
|
216
|
+
: `{ ${names.join(', ')} }`;
|
|
217
|
+
toolkitImport.replaceWithText(`import ${specifiers} from '${TOOLKIT_EVENTS_MODULE}'`);
|
|
218
|
+
}
|