Skip to content

Commit 9bf9e9a

Browse files
authored
Update export handling in TypeScript code merging and rendering (#146)
When rendering an individual goody, let's keep the `export` keyword since it highlights the goody's effect. When merging goodies together, it's assumed that this will be included as-is into a bigger piece of code so let's remove the exports there.
1 parent 16b0446 commit 9bf9e9a

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

tools/adventure-pack/goodies/typescript/Array.prototype.slidingWindows/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ArraySlice<T> {
2424

2525
[Symbol.iterator] = function* (
2626
this: ArraySlice<T>,
27-
): Generator<T, void, undefined> {
27+
): Generator<T, void, void> {
2828
for (let i = this.start; i <= this.end; ++i) {
2929
yield this.array[i];
3030
}
@@ -101,7 +101,7 @@ declare global {
101101
Array.prototype.slidingWindows = function* <T>(
102102
this: ReadonlyArray<T>,
103103
windowSize: number,
104-
): Generator<ArraySlice<T>, void, undefined> {
104+
): Generator<ArraySlice<T>, void, void> {
105105
for (
106106
let win: IndexableArraySlice<T> | null = ArraySlice.get(
107107
this,

tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Array.prototype.swap = function (i, j) {
202202
this[j] = tmp;
203203
};
204204
205-
export class BinaryHeap {
205+
class BinaryHeap {
206206
compareFn;
207207
items = [];
208208
@@ -460,7 +460,7 @@ exports[`App can equip single goody: JavaScript Iterator.prototype.max 1`] = `
460460
// Adventure Pack commit fake-commit-hash
461461
// Running at: https://example.com/
462462
463-
export function compareNatural(a, b) {
463+
function compareNatural(a, b) {
464464
if (
465465
(typeof a === "number" && typeof b === "number") ||
466466
(typeof a === "string" && typeof b === "string") ||
@@ -525,7 +525,7 @@ exports[`App can equip single goody: JavaScript Iterator.prototype.min 1`] = `
525525
// Adventure Pack commit fake-commit-hash
526526
// Running at: https://example.com/
527527
528-
export function compareNatural(a, b) {
528+
function compareNatural(a, b) {
529529
if (
530530
(typeof a === "number" && typeof b === "number") ||
531531
(typeof a === "string" && typeof b === "string") ||
@@ -801,7 +801,7 @@ exports[`App can equip single goody: JavaScript compareNatural 1`] = `
801801
// Adventure Pack commit fake-commit-hash
802802
// Running at: https://example.com/
803803
804-
export function compareNatural(a, b) {
804+
function compareNatural(a, b) {
805805
if (
806806
(typeof a === "number" && typeof b === "number") ||
807807
(typeof a === "string" && typeof b === "string") ||
@@ -945,7 +945,7 @@ class ArraySlice<T> {
945945
946946
[Symbol.iterator] = function* (
947947
this: ArraySlice<T>,
948-
): Generator<T, void, undefined> {
948+
): Generator<T, void, void> {
949949
for (let i = this.start; i <= this.end; ++i) {
950950
yield this.array[i];
951951
}
@@ -1006,7 +1006,7 @@ type IndexableArraySlice<T> = ArraySlice<T> & {
10061006
Array.prototype.slidingWindows = function* <T>(
10071007
this: ReadonlyArray<T>,
10081008
windowSize: number,
1009-
): Generator<ArraySlice<T>, void, undefined> {
1009+
): Generator<ArraySlice<T>, void, void> {
10101010
for (
10111011
let win: IndexableArraySlice<T> | null = ArraySlice.get(
10121012
this,
@@ -1060,7 +1060,7 @@ Array.prototype.swap = function <T>(this: T[], i: number, j: number): void {
10601060
this[j] = tmp;
10611061
};
10621062
1063-
export class BinaryHeap<T> {
1063+
class BinaryHeap<T> {
10641064
private readonly items: T[] = [];
10651065
10661066
constructor(private readonly compareFn: (a: T, b: T) => number) {}
@@ -1454,7 +1454,7 @@ declare global {
14541454
}
14551455
}
14561456
1457-
export function compareNatural<T>(a: T, b: T): number {
1457+
function compareNatural<T>(a: T, b: T): number {
14581458
if (
14591459
(typeof a === "number" && typeof b === "number") ||
14601460
(typeof a === "string" && typeof b === "string") ||
@@ -1541,7 +1541,7 @@ declare global {
15411541
}
15421542
}
15431543
1544-
export function compareNatural<T>(a: T, b: T): number {
1544+
function compareNatural<T>(a: T, b: T): number {
15451545
if (
15461546
(typeof a === "number" && typeof b === "number") ||
15471547
(typeof a === "string" && typeof b === "string") ||
@@ -1944,7 +1944,7 @@ exports[`App can equip single goody: TypeScript compareNatural 1`] = `
19441944
// Adventure Pack commit fake-commit-hash
19451945
// Running at: https://example.com/
19461946
1947-
export function compareNatural<T>(a: T, b: T): number {
1947+
function compareNatural<T>(a: T, b: T): number {
19481948
if (
19491949
(typeof a === "number" && typeof b === "number") ||
19501950
(typeof a === "string" && typeof b === "string") ||

tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ exports[`App can render goody: JavaScript Function.returnThis 1`] = `
237237
`;
238238

239239
exports[`App can render goody: JavaScript Iterator.prototype 1`] = `
240-
"const iteratorPrototype = Object.getPrototypeOf(
240+
"export const iteratorPrototype = Object.getPrototypeOf(
241241
Object.getPrototypeOf([].values()),
242242
);"
243243
`;
@@ -598,7 +598,7 @@ class ArraySlice<T> {
598598
599599
[Symbol.iterator] = function* (
600600
this: ArraySlice<T>,
601-
): Generator<T, void, undefined> {
601+
): Generator<T, void, void> {
602602
for (let i = this.start; i <= this.end; ++i) {
603603
yield this.array[i];
604604
}
@@ -659,7 +659,7 @@ type IndexableArraySlice<T> = ArraySlice<T> & {
659659
Array.prototype.slidingWindows = function* <T>(
660660
this: ReadonlyArray<T>,
661661
windowSize: number,
662-
): Generator<ArraySlice<T>, void, undefined> {
662+
): Generator<ArraySlice<T>, void, void> {
663663
for (
664664
let win: IndexableArraySlice<T> | null = ArraySlice.get(
665665
this,
@@ -775,7 +775,7 @@ Function.returnThis = function <T>(this: T): T {
775775
`;
776776
777777
exports[`App can render goody: TypeScript Iterator.prototype 1`] = `
778-
"const iteratorPrototype = Object.getPrototypeOf(
778+
"export const iteratorPrototype = Object.getPrototypeOf(
779779
Object.getPrototypeOf([].values()),
780780
) as Iterator<unknown, unknown, unknown>;"
781781
`;

tools/adventure-pack/src/app/mergeCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export async function mergeCode({
201201
"\n" +
202202
`// Adventure Pack commit ${commitHash}\n` +
203203
`// Running at: ${window.location.href}\n\n` +
204-
mergedCode +
204+
mergedCode.replaceAll(/^export\s+/gm, "") +
205205
"\n\n" +
206206
centerTextInComment({
207207
text: "END ADVENTURE PACK CODE",

tools/adventure-pack/src/scripts/package-goodies/typescript/readBaseGoody.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ export async function readBaseGoody(
5353
}
5454
});
5555

56-
sourceFile.getVariableDeclarations().forEach((decl) => {
57-
decl.getVariableStatementOrThrow().setIsExported(false);
58-
});
59-
6056
const updatedCode = await formatCode(sourceFile.getFullText());
6157

6258
return {

0 commit comments

Comments
 (0)