@openrewrite/rewrite 8.92.14 → 8.92.16
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/javascript/add-import.d.ts.map +1 -1
- package/dist/javascript/add-import.js +127 -52
- package/dist/javascript/add-import.js.map +1 -1
- package/dist/javascript/amd.d.ts.map +1 -1
- package/dist/javascript/amd.js +3 -2
- package/dist/javascript/amd.js.map +1 -1
- package/dist/javascript/binding.js +3 -3
- package/dist/javascript/binding.js.map +1 -1
- package/dist/javascript/index.d.ts +3 -0
- package/dist/javascript/index.d.ts.map +1 -1
- package/dist/javascript/index.js +3 -0
- package/dist/javascript/index.js.map +1 -1
- package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.d.ts.map +1 -1
- package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js +28 -32
- package/dist/javascript/migrate/es6/modernize-octal-escape-sequences.js.map +1 -1
- package/dist/javascript/preconditions.d.ts +11 -0
- package/dist/javascript/preconditions.d.ts.map +1 -1
- package/dist/javascript/preconditions.js +14 -0
- package/dist/javascript/preconditions.js.map +1 -1
- package/dist/javascript/search/index.d.ts +1 -0
- package/dist/javascript/search/index.d.ts.map +1 -1
- package/dist/javascript/search/index.js +1 -0
- package/dist/javascript/search/index.js.map +1 -1
- package/dist/javascript/search/is-vendored-or-bundled.d.ts +13 -0
- package/dist/javascript/search/is-vendored-or-bundled.d.ts.map +1 -0
- package/dist/javascript/search/is-vendored-or-bundled.js +93 -0
- package/dist/javascript/search/is-vendored-or-bundled.js.map +1 -0
- package/dist/rewrite-javascript-version.txt +1 -1
- package/package.json +1 -1
- package/src/javascript/add-import.ts +135 -53
- package/src/javascript/amd.ts +3 -2
- package/src/javascript/binding.ts +3 -3
- package/src/javascript/index.ts +3 -0
- package/src/javascript/migrate/es6/modernize-octal-escape-sequences.ts +28 -36
- package/src/javascript/preconditions.ts +19 -1
- package/src/javascript/search/index.ts +1 -0
- package/src/javascript/search/is-vendored-or-bundled.ts +59 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"add-import.d.ts","sourceRoot":"","sources":["../../src/javascript/add-import.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAoD,CAAC,EAAE,QAAQ,EAAkE,MAAM,SAAS,CAAC;AACxJ,OAAO,EAAC,EAAE,EAAM,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"add-import.d.ts","sourceRoot":"","sources":["../../src/javascript/add-import.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAC,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAoD,CAAC,EAAE,QAAQ,EAAkE,MAAM,SAAS,CAAC;AACxJ,OAAO,EAAC,EAAE,EAAM,MAAM,QAAQ,CAAC;AAW/B,MAAM,MAAM,SAAS,GAAG,GAAG,GAAG,GAAG,CAAC;AAElC,oBAAY,WAAW;IACnB,QAAQ,IAAA,CAAO,6BAA6B;IAC5C,YAAY,IAAA,CAAG,8BAA8B;IAC7C,UAAU,IAAA,CAAK,yBAAyB;IACxC,QAAQ,IAAA;CACX;AAED,MAAM,WAAW,gBAAgB;IAC7B;;;oCAGgC;IAChC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,CAAC;IAE3B;;;;;;;mDAO+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;;mDAI+C;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;mDAE+C;IAC/C,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;mDAC+C;IAC/C,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;2EACuE;IACvE,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;mDAE+C;IAC/C,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,kGAAkG;IAClG,KAAK,CAAC,EAAE,WAAW,CAAC;IAEpB;;+FAE2F;IA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AAC,CAAC,CAAC;IAIzF;;;OAGG;IACY,oBAAoB,CAAC,eAAe,EAAE,EAAE,CAAC,eAAe,EAAE,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IA8BtG;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAQzB,6EAA6E;IAC9D,uBAAuB,CAAC,kBAAkB,EAAE,EAAE,CAAC,kBAAkB,EAAE,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;CAgBlH"}
|
|
@@ -359,7 +359,7 @@ function moduleNameOf(module) {
|
|
|
359
359
|
}
|
|
360
360
|
restored += source.slice(cut);
|
|
361
361
|
const quote = restored.charAt(0);
|
|
362
|
-
return (quote === "'" || quote === '"') && restored.endsWith(quote) && restored.length > 1
|
|
362
|
+
return (quote === "'" || quote === '"' || quote === '`') && restored.endsWith(quote) && restored.length > 1
|
|
363
363
|
? restored.slice(1, -1)
|
|
364
364
|
: restored;
|
|
365
365
|
}
|
|
@@ -375,13 +375,33 @@ function quoteOf(expression) {
|
|
|
375
375
|
? quote
|
|
376
376
|
: undefined;
|
|
377
377
|
}
|
|
378
|
+
/**
|
|
379
|
+
* The value a Prettier configuration on `cu` gives `option`, or Prettier's own `fallback` where the
|
|
380
|
+
* configuration does not name it; `undefined` without a configuration in force. A configuration
|
|
381
|
+
* outranks the file's own evidence, since running Prettier would arrive at its answer anyway.
|
|
382
|
+
*/
|
|
383
|
+
function prettierOption(cu, option, fallback) {
|
|
384
|
+
const prettier = (0, prettier_format_1.getPrettierStyle)(cu);
|
|
385
|
+
if (!prettier || prettier.ignored) {
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
const value = prettier.config[option];
|
|
389
|
+
return typeof value === typeof fallback ? value : fallback;
|
|
390
|
+
}
|
|
378
391
|
/**
|
|
379
392
|
* Pick the quote character for a module specifier being added to `cu`, so that it agrees
|
|
380
|
-
* with the file it lands in.
|
|
381
|
-
*
|
|
393
|
+
* with the file it lands in. A Prettier configuration settles it; short of one, the caller's
|
|
394
|
+
* `requested` quote, then an existing specifier as the most direct precedent.
|
|
382
395
|
*/
|
|
383
|
-
async function detectQuote(cu) {
|
|
396
|
+
async function detectQuote(cu, requested) {
|
|
384
397
|
var _a, _b;
|
|
398
|
+
const singleQuote = prettierOption(cu, 'singleQuote', false);
|
|
399
|
+
if (singleQuote !== undefined) {
|
|
400
|
+
return singleQuote ? "'" : '"';
|
|
401
|
+
}
|
|
402
|
+
if (requested) {
|
|
403
|
+
return requested;
|
|
404
|
+
}
|
|
385
405
|
// Static imports are top-level, so the highest-ranked signal needs no traversal to find.
|
|
386
406
|
for (const statement of cu.statements) {
|
|
387
407
|
const element = statement.element;
|
|
@@ -438,23 +458,83 @@ async function detectQuote(cu) {
|
|
|
438
458
|
if (specifierQuote) {
|
|
439
459
|
return specifierQuote;
|
|
440
460
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
461
|
+
return double > single ? '"' : "'";
|
|
462
|
+
}
|
|
463
|
+
/** Top-level statements that end in `;` in a file that uses semicolons at all. */
|
|
464
|
+
const semicolonTerminated = new Set([
|
|
465
|
+
tree_1.JS.Kind.Import, tree_1.JS.Kind.ExportDeclaration, tree_1.JS.Kind.ExportAssignment, tree_1.JS.Kind.TypeDeclaration,
|
|
466
|
+
tree_1.JS.Kind.ExpressionStatement, java_1.J.Kind.VariableDeclarations, java_1.J.Kind.MethodInvocation, java_1.J.Kind.Assignment
|
|
467
|
+
]);
|
|
468
|
+
/**
|
|
469
|
+
* Whether an import statement added to `cu` should end in `;`. A Prettier configuration settles
|
|
470
|
+
* it; short of one, the file's imports, then its other statements that would take one.
|
|
471
|
+
*/
|
|
472
|
+
function detectSemi(cu) {
|
|
473
|
+
const semi = prettierOption(cu, 'semi', true);
|
|
474
|
+
if (semi !== undefined) {
|
|
475
|
+
return semi;
|
|
476
|
+
}
|
|
477
|
+
const tally = (kinds) => {
|
|
478
|
+
let score = 0;
|
|
479
|
+
for (const statement of cu.statements) {
|
|
480
|
+
if (statement.element && kinds(statement.element.kind)) {
|
|
481
|
+
score += (0, markers_1.findMarker)(statement, java_1.J.Markers.Semicolon) ? 1 : -1;
|
|
482
|
+
}
|
|
446
483
|
}
|
|
484
|
+
return score;
|
|
485
|
+
};
|
|
486
|
+
const score = cu.statements.some(s => { var _a; return ((_a = s.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.Import; })
|
|
487
|
+
? tally(kind => kind === tree_1.JS.Kind.Import)
|
|
488
|
+
: tally(kind => semicolonTerminated.has(kind));
|
|
489
|
+
return score >= 0;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Whether a brace list added to `cu` is written `{ x }` rather than `{x}`. A Prettier configuration
|
|
493
|
+
* settles it; short of one, the file's own single-line import lists, then the style in force.
|
|
494
|
+
*/
|
|
495
|
+
function detectBraceSpacing(cu) {
|
|
496
|
+
var _a, _b, _c;
|
|
497
|
+
const bracketSpacing = prettierOption(cu, 'bracketSpacing', true);
|
|
498
|
+
if (bracketSpacing !== undefined) {
|
|
499
|
+
return bracketSpacing;
|
|
447
500
|
}
|
|
448
|
-
|
|
501
|
+
let score = 0;
|
|
502
|
+
for (const statement of cu.statements) {
|
|
503
|
+
const namedBindings = ((_a = statement.element) === null || _a === void 0 ? void 0 : _a.kind) === tree_1.JS.Kind.Import
|
|
504
|
+
? (_b = statement.element.importClause) === null || _b === void 0 ? void 0 : _b.namedBindings
|
|
505
|
+
: undefined;
|
|
506
|
+
if ((namedBindings === null || namedBindings === void 0 ? void 0 : namedBindings.kind) !== tree_1.JS.Kind.NamedImports) {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const first = (_c = namedBindings.elements.elements[0]) === null || _c === void 0 ? void 0 : _c.element;
|
|
510
|
+
// A list laid out one per line says nothing about how a one-line list is spaced.
|
|
511
|
+
if (first && !first.prefix.whitespace.includes('\n')) {
|
|
512
|
+
score += first.prefix.whitespace.length > 0 ? 1 : -1;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (score !== 0) {
|
|
516
|
+
return score > 0;
|
|
517
|
+
}
|
|
518
|
+
return (0, style_1.getStyle)(style_1.StyleKind.SpacesStyle, cu).within.es6ImportExportBraces;
|
|
449
519
|
}
|
|
450
520
|
/**
|
|
451
|
-
* Lays out the import statement `AddImport` wrote, so that
|
|
452
|
-
*
|
|
521
|
+
* Lays out the import statement `AddImport` wrote, so that a Prettier configuration's print width,
|
|
522
|
+
* say, reaches it. Its quote, brace spacing and semicolon were already chosen to match the file.
|
|
453
523
|
*/
|
|
454
|
-
async function formatImport(cu, id, p) {
|
|
524
|
+
async function formatImport(cu, id, p, braceSpacing) {
|
|
525
|
+
// The formatter would otherwise re-space the braces by the style in force, over the file's own evidence.
|
|
526
|
+
const spaces = (0, style_1.getStyle)(style_1.StyleKind.SpacesStyle, cu);
|
|
527
|
+
const styles = braceSpacing === undefined || braceSpacing === spaces.within.es6ImportExportBraces ? undefined : [{
|
|
528
|
+
kind: markers_1.MarkersKind.NamedStyles,
|
|
529
|
+
id: (0, uuid_1.randomId)(),
|
|
530
|
+
name: "org.openrewrite.javascript.AddImport",
|
|
531
|
+
displayName: "Brace spacing detected by AddImport",
|
|
532
|
+
tags: [],
|
|
533
|
+
styles: [Object.assign(Object.assign({}, spaces), { within: Object.assign(Object.assign({}, spaces.within), { es6ImportExportBraces: braceSpacing }) })]
|
|
534
|
+
}];
|
|
455
535
|
return await new class extends visitor_1.JavaScriptVisitor {
|
|
456
536
|
async visitImportDeclaration(jsImport, p) {
|
|
457
|
-
return jsImport.id === id ? await (0, format_1.autoFormat)(jsImport, p, undefined, this.cursor.parent) : jsImport;
|
|
537
|
+
return jsImport.id === id ? await (0, format_1.autoFormat)(jsImport, p, undefined, this.cursor.parent, styles) : jsImport;
|
|
458
538
|
}
|
|
459
539
|
}().visit(cu, p);
|
|
460
540
|
}
|
|
@@ -687,14 +767,12 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
687
767
|
const newImport = await this.createImportStatement(compilationUnit, insertionIndex, p);
|
|
688
768
|
const withImport = await this.produceJavaScript(compilationUnit, p, async (draft) => {
|
|
689
769
|
// Insert the import at the appropriate position
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
id: (0, uuid_1.randomId)()
|
|
694
|
-
});
|
|
770
|
+
const semicolonMarkers = detectSemi(compilationUnit)
|
|
771
|
+
? (0, markers_1.markers)({ kind: java_1.J.Markers.Semicolon, id: (0, uuid_1.randomId)() })
|
|
772
|
+
: markers_1.emptyMarkers;
|
|
695
773
|
if (insertionIndex === 0) {
|
|
696
774
|
// Insert at the beginning
|
|
697
|
-
// The `after` space should be empty since semicolon is printed after it
|
|
775
|
+
// The `after` space should be empty since any semicolon is printed after it
|
|
698
776
|
// The spacing comes from updating the next statement's prefix
|
|
699
777
|
const updatedStatements = compilationUnit.statements.length > 0
|
|
700
778
|
? [
|
|
@@ -732,7 +810,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
732
810
|
}
|
|
733
811
|
}
|
|
734
812
|
});
|
|
735
|
-
return formatImport(withImport, newImport.id, p);
|
|
813
|
+
return formatImport(withImport, newImport.id, p, detectBraceSpacing(compilationUnit));
|
|
736
814
|
}
|
|
737
815
|
/**
|
|
738
816
|
* Try to merge the new member into an existing import from the same module
|
|
@@ -951,6 +1029,9 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
951
1029
|
* or extracting it from the FQN.
|
|
952
1030
|
*/
|
|
953
1031
|
getModuleFromClassType(classType) {
|
|
1032
|
+
if (java_1.Type.isFunctionType(classType)) {
|
|
1033
|
+
return undefined;
|
|
1034
|
+
}
|
|
954
1035
|
// Traverse owningClass chain to find the root
|
|
955
1036
|
let current = classType;
|
|
956
1037
|
while (current.owningClass && java_1.Type.isClass(current.owningClass)) {
|
|
@@ -1063,6 +1144,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1063
1144
|
const targetModule = this.module;
|
|
1064
1145
|
let found = false;
|
|
1065
1146
|
const self = this;
|
|
1147
|
+
const matchesModule = (name) => name !== undefined && (name === expectedDeclaringType || name === targetModule);
|
|
1066
1148
|
// If no existing imports from this module, look for unresolved references
|
|
1067
1149
|
// If there ARE existing imports, look for references with the expected declaring type
|
|
1068
1150
|
const collector = new class extends visitor_1.JavaScriptVisitor {
|
|
@@ -1074,12 +1156,13 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1074
1156
|
// We have an expected declaring type - check for exact match
|
|
1075
1157
|
if (type && java_1.Type.isMethod(type)) {
|
|
1076
1158
|
const declaringTypeName = java_1.Type.FullyQualified.getFullyQualifiedName(type.declaringType);
|
|
1077
|
-
if (declaringTypeName
|
|
1159
|
+
if (matchesModule(declaringTypeName)) {
|
|
1078
1160
|
found = true;
|
|
1079
1161
|
}
|
|
1080
1162
|
}
|
|
1081
1163
|
else if (type && java_1.Type.isClass(type)) {
|
|
1082
|
-
if (self.classTypeMatchesModule(type, expectedDeclaringType)
|
|
1164
|
+
if (self.classTypeMatchesModule(type, expectedDeclaringType) ||
|
|
1165
|
+
self.classTypeMatchesModule(type, targetModule)) {
|
|
1083
1166
|
found = true;
|
|
1084
1167
|
}
|
|
1085
1168
|
}
|
|
@@ -1087,7 +1170,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1087
1170
|
const ownerTypeName = fieldType.owner
|
|
1088
1171
|
? java_1.Type.FullyQualified.getFullyQualifiedName(fieldType.owner)
|
|
1089
1172
|
: undefined;
|
|
1090
|
-
if (ownerTypeName
|
|
1173
|
+
if (matchesModule(ownerTypeName)) {
|
|
1091
1174
|
found = true;
|
|
1092
1175
|
}
|
|
1093
1176
|
}
|
|
@@ -1133,24 +1216,16 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1133
1216
|
return super.visitIdentifier(identifier, p);
|
|
1134
1217
|
}
|
|
1135
1218
|
async visitMethodInvocation(methodInvocation, p) {
|
|
1136
|
-
if (methodInvocation.methodType && methodInvocation.methodType.name === targetName
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
if (declaringTypeName === expectedDeclaringType) {
|
|
1140
|
-
found = true;
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1219
|
+
if (expectedDeclaringType && methodInvocation.methodType && methodInvocation.methodType.name === targetName &&
|
|
1220
|
+
matchesModule(java_1.Type.FullyQualified.getFullyQualifiedName(methodInvocation.methodType.declaringType))) {
|
|
1221
|
+
found = true;
|
|
1143
1222
|
}
|
|
1144
1223
|
return super.visitMethodInvocation(methodInvocation, p);
|
|
1145
1224
|
}
|
|
1146
1225
|
async visitFunctionCall(functionCall, p) {
|
|
1147
|
-
if (functionCall.methodType && functionCall.methodType.name === targetName
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
if (declaringTypeName === expectedDeclaringType) {
|
|
1151
|
-
found = true;
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1226
|
+
if (expectedDeclaringType && functionCall.methodType && functionCall.methodType.name === targetName &&
|
|
1227
|
+
matchesModule(java_1.Type.FullyQualified.getFullyQualifiedName(functionCall.methodType.declaringType))) {
|
|
1228
|
+
found = true;
|
|
1154
1229
|
}
|
|
1155
1230
|
return super.visitFunctionCall(functionCall, p);
|
|
1156
1231
|
}
|
|
@@ -1158,13 +1233,9 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1158
1233
|
const type = fieldAccess.type;
|
|
1159
1234
|
if (type && java_1.Type.isMethod(type)) {
|
|
1160
1235
|
const methodType = type;
|
|
1161
|
-
if (methodType.name === targetName
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
if (declaringTypeName === expectedDeclaringType) {
|
|
1165
|
-
found = true;
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1236
|
+
if (expectedDeclaringType && methodType.name === targetName &&
|
|
1237
|
+
matchesModule(java_1.Type.FullyQualified.getFullyQualifiedName(methodType.declaringType))) {
|
|
1238
|
+
found = true;
|
|
1168
1239
|
}
|
|
1169
1240
|
}
|
|
1170
1241
|
return super.visitFieldAccess(fieldAccess, p);
|
|
@@ -1177,7 +1248,6 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1177
1248
|
* Create a new import statement
|
|
1178
1249
|
*/
|
|
1179
1250
|
async createImportStatement(compilationUnit, insertionIndex, p) {
|
|
1180
|
-
var _a;
|
|
1181
1251
|
// Determine the appropriate prefix (spacing before the import)
|
|
1182
1252
|
const prefix = this.determineImportPrefix(compilationUnit, insertionIndex);
|
|
1183
1253
|
// Whitespace throughout follows the parser's placement, so that formatting the result is a
|
|
@@ -1185,7 +1255,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1185
1255
|
// Note: value is the unquoted module name; valueSource and unicodeEscapes are its printed form
|
|
1186
1256
|
let valueSource = this.moduleValueSource;
|
|
1187
1257
|
if (valueSource === undefined) {
|
|
1188
|
-
const quote = (
|
|
1258
|
+
const quote = await detectQuote(compilationUnit, this.quoteStyle);
|
|
1189
1259
|
valueSource = `${quote}${this.module}${quote}`;
|
|
1190
1260
|
}
|
|
1191
1261
|
const moduleSpecifier = {
|
|
@@ -1268,9 +1338,7 @@ class AddImport extends visitor_1.JavaScriptVisitor {
|
|
|
1268
1338
|
}
|
|
1269
1339
|
else {
|
|
1270
1340
|
// Named import: import { member } from 'module'
|
|
1271
|
-
|
|
1272
|
-
const spacesStyle = (0, style_1.getStyle)(style_1.StyleKind.SpacesStyle, compilationUnit);
|
|
1273
|
-
const braceSpace = spacesStyle.within.es6ImportExportBraces ? java_1.singleSpace : java_1.emptySpace;
|
|
1341
|
+
const braceSpace = detectBraceSpacing(compilationUnit) ? java_1.singleSpace : java_1.emptySpace;
|
|
1274
1342
|
const importSpec = this.createImportSpecifier();
|
|
1275
1343
|
// Apply brace spacing: the space after { is in the specifier's prefix,
|
|
1276
1344
|
// and the space before } is in the rightPadded's after
|
|
@@ -1435,7 +1503,7 @@ exports.AddImport = AddImport;
|
|
|
1435
1503
|
function importBinds(jsImport, module, member) {
|
|
1436
1504
|
var _a, _b;
|
|
1437
1505
|
const specifier = (_a = jsImport.moduleSpecifier) === null || _a === void 0 ? void 0 : _a.element;
|
|
1438
|
-
if ((specifier === null || specifier === void 0 ? void 0 : specifier.kind) !== java_1.J.Kind.Literal || specifier
|
|
1506
|
+
if ((specifier === null || specifier === void 0 ? void 0 : specifier.kind) !== java_1.J.Kind.Literal || moduleNameOf(specifier) !== module) {
|
|
1439
1507
|
return undefined;
|
|
1440
1508
|
}
|
|
1441
1509
|
const importClause = jsImport.importClause;
|
|
@@ -1569,7 +1637,7 @@ function aliasing(local, member) {
|
|
|
1569
1637
|
* Drops the binding for `member` from `jsImport`'s clause — the default, the namespace alias, or
|
|
1570
1638
|
* one entry of the named list, whichever `member` names — keeping everything else the clause
|
|
1571
1639
|
* binds. `ElementRemovalFormatter` carries the dropped binding's prefix onto whatever prints
|
|
1572
|
-
* next,
|
|
1640
|
+
* next, and a dropped last entry's trailing space onto the new last, as `RemoveImport` does.
|
|
1573
1641
|
*/
|
|
1574
1642
|
function removeBinding(jsImport, member) {
|
|
1575
1643
|
var _a;
|
|
@@ -1619,6 +1687,12 @@ function removeBinding(jsImport, member) {
|
|
|
1619
1687
|
// last member; the caller drops the whole statement when no default remains either.
|
|
1620
1688
|
return Object.assign(Object.assign({}, jsImport), { importClause: Object.assign(Object.assign({}, importClause), { namedBindings: undefined }) });
|
|
1621
1689
|
}
|
|
1690
|
+
// The space before `}` and any trailing comma belong to the last entry, so they move onto the
|
|
1691
|
+
// new last entry when the old one is dropped.
|
|
1692
|
+
const originalLast = namedImports.elements.elements[namedImports.elements.elements.length - 1];
|
|
1693
|
+
if (namedSpecifierImports(originalLast.element.specifier, key)) {
|
|
1694
|
+
kept[kept.length - 1] = Object.assign(Object.assign({}, kept[kept.length - 1]), { after: originalLast.after, markers: originalLast.markers });
|
|
1695
|
+
}
|
|
1622
1696
|
const updatedNamedImports = Object.assign(Object.assign({}, namedImports), { elements: Object.assign(Object.assign({}, namedImports.elements), { elements: kept }) });
|
|
1623
1697
|
return Object.assign(Object.assign({}, jsImport), { importClause: Object.assign(Object.assign({}, importClause), { namedBindings: updatedNamedImports }) });
|
|
1624
1698
|
}
|
|
@@ -1684,6 +1758,7 @@ class RebindImport extends visitor_1.JavaScriptVisitor {
|
|
|
1684
1758
|
const originalSource = literal.valueSource || `"${this.from.module}"`;
|
|
1685
1759
|
const quoteChar = originalSource.startsWith("'") ? "'" : '"';
|
|
1686
1760
|
literal.valueSource = `${quoteChar}${this.to.module}${quoteChar}`;
|
|
1761
|
+
literal.unicodeEscapes = undefined;
|
|
1687
1762
|
// A default or namespace import carries its local name on the clause itself; a named
|
|
1688
1763
|
// one states the member alongside it, in the specifier.
|
|
1689
1764
|
if (key === undefined || key === '*') {
|