Skip to content

Commit d80c070

Browse files
authored
fix: noWrapBeforeImport must wrap first import if no code is before (#141)
1 parent 788705d commit d80c070

File tree

7 files changed

+96
-8
lines changed

7 files changed

+96
-8
lines changed

packages/plugin/__test__/__snapshots__/test.js.snap

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,28 @@ sap.ui.define(["./foo"], function (__Foo) {
14801480
});"
14811481
`;
14821482
1483+
exports[`min-wrap min-wrap-test.js 1`] = `
1484+
"sap.ui.define(["../../model/someFile", "../schema/otherFile"], function (____model_someFile, ___schema_otherFile) {
1485+
"use strict";
1486+
1487+
const getElementPath = ____model_someFile["getElementPath"];
1488+
const createSchema = ___schema_otherFile["createSchema"];
1489+
getElementPath(createSchema(), "elementSchema");
1490+
});"
1491+
`;
1492+
1493+
exports[`min-wrap min-wrap-test-wrap.js 1`] = `
1494+
""use strict";
1495+
1496+
const x = 1;
1497+
sap.ui.define(["../../model/someFile", "../schema/otherFile"], function (____model_someFile, ___schema_otherFile) {
1498+
const getElementPath = ____model_someFile["getElementPath"];
1499+
const createSchema = ___schema_otherFile["createSchema"];
1500+
getElementPath(createSchema(), "elementSchema");
1501+
console.log(x);
1502+
});"
1503+
`;
1504+
14831505
exports[`min-wrap min-wrap-ui5.js 1`] = `
14841506
""use strict";
14851507
@@ -2109,6 +2131,46 @@ exports[`typescript ts-export-type.ts 1`] = `
21092131
21102132
exports[`typescript ts-export-type-only.ts 1`] = `""`;
21112133
2134+
exports[`typescript ts-export-type-reexport.ts 1`] = `
2135+
"sap.ui.define(["./ts-export-type-reexport-enum"], function (___ts_export_type_reexport_enum) {
2136+
"use strict";
2137+
2138+
var __exports = {
2139+
__esModule: true
2140+
};
2141+
function extendExports(exports, obj) {
2142+
obj && Object.keys(obj).forEach(function (key) {
2143+
if (key === "default" || key === "__esModule") return;
2144+
Object.defineProperty(exports, key, {
2145+
enumerable: true,
2146+
get: function get() {
2147+
return obj[key];
2148+
}
2149+
});
2150+
});
2151+
}
2152+
extendExports(__exports, ___ts_export_type_reexport_enum);
2153+
return __exports;
2154+
});"
2155+
`;
2156+
2157+
exports[`typescript ts-export-type-reexport-enum.ts 1`] = `
2158+
"sap.ui.define([], function () {
2159+
"use strict";
2160+
2161+
var FemRendererModel = function (FemRendererModel) {
2162+
FemRendererModel["AppModel"] = "AppModel";
2163+
FemRendererModel["RouteModel"] = "RouteModel";
2164+
return FemRendererModel;
2165+
}(FemRendererModel || {});
2166+
var __exports = {
2167+
__esModule: true
2168+
};
2169+
__exports.FemRendererModel = FemRendererModel;
2170+
return __exports;
2171+
});"
2172+
`;
2173+
21122174
exports[`typescript ts-index.ts 1`] = `
21132175
"sap.ui.define(["./_private_/index"], function (____private__index) {
21142176
"use strict";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const x = 1; // This should not be part of sap-ui-define
2+
3+
import { getElementPath } from "../../model/someFile";
4+
import { createSchema } from "../schema/otherFile";
5+
6+
getElementPath(createSchema(), "elementSchema");
7+
console.log(x);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { getElementPath } from "../../model/someFile";
2+
import { createSchema } from "../schema/otherFile";
3+
4+
getElementPath(createSchema(), "elementSchema");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum FemRendererModel {
2+
AppModel = "AppModel",
3+
RouteModel = "RouteModel",
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type RouterContext = {
2+
mock: boolean;
3+
service: string;
4+
path: string;
5+
};
6+
7+
export * from "./ts-export-type-reexport-enum";

packages/plugin/src/modules/helpers/wrapper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ export function wrap(visitor, programNode, opts) {
9292
const fullBody = body;
9393
const newBody = [];
9494

95+
// If there is no lastBeforeWrapping, the first import is not marked
96+
if (!fullBody.find((item) => item.lastBeforeWrapping)) {
97+
reachedFirstImport = true;
98+
}
99+
95100
for (const item of fullBody) {
96101
if (reachedFirstImport) {
97102
newBody.push(item);

packages/plugin/src/modules/visitor.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,13 @@ export const ModuleTransformVisitor = {
203203
}
204204

205205
// this is the very first import in noWrapBeforeImport mode and there are sibling nodes before this import
206-
if (
207-
opts.noWrapBeforeImport &&
208-
!this.firstImportMarked &&
209-
path.inList &&
210-
path.key > 0
211-
) {
212-
// mark the direct predecessor as the last one to exclude from wrapping
213-
path.getSibling(path.key - 1).node.lastBeforeWrapping = true;
206+
if (opts.noWrapBeforeImport && !this.firstImportMarked && path.inList) {
207+
// for the first element there is a special case as we can't mark the previous one
208+
// therefore we do not mark anything to make clear that there's nothing to exclude
209+
if (path.key > 0) {
210+
// mark the direct predecessor as the last one to exclude from wrapping
211+
path.getSibling(path.key - 1).node.lastBeforeWrapping = true;
212+
}
214213
this.firstImportMarked = true;
215214
}
216215

0 commit comments

Comments
 (0)