Skip to content

Commit 7b4e699

Browse files
committed
fix(-selector-builder): fetch namespace members and comments from docs
1 parent 15d033f commit 7b4e699

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

scripts/selector-declaration-builder.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ const {
55
dtsObjectTypeAlias,
66
convertTreeToString
77
} = docworksDts.dtsGenerator;
8+
const { convertOperationToFunction } = docworksDts.docworksToDtsConverters;
89

910
const QUERYABLE_TYPE = "TypeNameToSdkType";
1011
const $W = "$w";
12+
const $W_MEMBERS_KEY = "/** $W_NAMESPACE_MEMBERS */";
13+
const DECLARE_KEYWORD = "declare";
14+
function find$wService(services) {
15+
return services.find(
16+
service =>
17+
service.name === $W &&
18+
!Object.prototype.hasOwnProperty.call(service, "memberOf")
19+
);
20+
}
1121

1222
function getQueryableObjectType(services) {
1323
const isQueryableService = service =>
@@ -20,10 +30,50 @@ function getQueryableObjectType(services) {
2030
return dtsObjectTypeAlias(QUERYABLE_TYPE, properties);
2131
}
2232

33+
function extract$wComments(services) {
34+
const $wService = find$wService(services);
35+
const $wOperation = $wService.operations.find(
36+
operation => operation.name === $W
37+
);
38+
const [$w$wMembersComment] = $wService.operations
39+
.filter(o => o.name === $W)
40+
.map(operation => operation.docs.summary);
41+
return {
42+
$W_GLOBAL_DECLARATION_COMMENT: $wOperation.docs.summary,
43+
$W_NAMESPACE_COMMENT: $wService.docs.summary,
44+
$W_$W_DECLARATION_COMMENT: $w$wMembersComment
45+
};
46+
}
47+
48+
function extract$wMembers(services) {
49+
const $wService = find$wService(services);
50+
const $wMembersOperations = $wService.operations
51+
.filter(o => o.name !== $W)
52+
.map(operation => {
53+
return convertOperationToFunction($wService, operation, {
54+
documentationGenerator: ({ summary }) => summary
55+
});
56+
});
57+
58+
return convertTreeToString($wMembersOperations)
59+
.split(DECLARE_KEYWORD)
60+
.join("")
61+
.split("\n")
62+
.join("\n\t");
63+
}
64+
2365
function $wDeclarationBuilder(services) {
66+
const $wComments = extract$wComments(services);
67+
const $wMembers = extract$wMembers(services);
2468
const queryableType = getQueryableObjectType(services);
2569

26-
const page$w = fs.readFileSync("./src_types/pages/$w.d.ts", null, "utf-8");
70+
let page$w = fs.readFileSync("./src_types/pages/$w.d.ts", "utf-8");
71+
page$w = page$w.replace($W_MEMBERS_KEY, $wMembers);
72+
page$w = Object.keys($wComments).reduce(
73+
(dts, commnetKey) => dts.replace(commnetKey, $wComments[commnetKey]),
74+
page$w
75+
);
76+
2777
return [convertTreeToString({ queryableType }), page$w].join("\n");
2878
}
2979

src_types/pages/$w.d.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// the first part of this file is being generated by => scripts/selector-declaration-builder.js
22
// Run `npm run generate-dts` to generate it
3-
//todo:: try to improve intersectionArray
3+
44
type IntersectionArrayAndBase<T, P> = {
55
[K in keyof T]: K extends P ? T[K] : T[K] & T[K][];
66
};
@@ -15,42 +15,33 @@ type VaildSelectorsChars = WixElementSelector | LegalChars
1515

1616
type OnlyVaildSelectorsChars<S> =
1717
S extends ""
18-
? any
18+
? unknown
1919
: S extends `${VaildSelectorsChars}${infer Tail}`
2020
? OnlyVaildSelectorsChars<Tail>
2121
: never
2222

2323
type HasComma<S> =
2424
S extends `${CommaChar}${infer Tail}`
2525
? unknown
26-
: S extends `${VaildSelectorsChars}${infer Tail2}`
27-
? HasComma<Tail2>
26+
: S extends `${VaildSelectorsChars}${infer Tail}`
27+
? HasComma<Tail>
2828
: never
29+
2930
type MultiSelector<S> = S & OnlyVaildSelectorsChars<S> & HasComma<S>
3031
/**
31-
* Selects and returns elements from a page.
32+
* $W_GLOBAL_DECLARATION_COMMENT
3233
*/
3334
declare function $w<T extends WixElementSelector, S extends string>(selector: T | MultiSelector<S>):
3435
S extends keyof WixElements
3536
? WixElements[S]
3637
: any
3738
/**
38-
* The `$w` namespace contains everything you need in order to work
39-
* with your site's components.
39+
* $W_NAMESPACE_COMMENT
4040
*/
4141
declare namespace $w {
42+
/** $W_NAMESPACE_MEMBERS */
4243
/**
43-
* Gets a selector function for a specific context.
44-
*/
45-
function at(context: $w.Event.EventContext): $w.$w;
46-
47-
/**
48-
* Sets the function that runs when all the page elements have finished loading.
49-
*/
50-
function onReady(initFunction: $w.ReadyHandler): void;
51-
52-
/**
53-
* Selects and returns elements from a page.
44+
* $W_$W_DECLARATION_COMMENT
5445
*/
5546
type $w = <T extends WixElementSelector, S extends string>(selector: T | MultiSelector<S>) =>
5647
S extends keyof WixElements

test/it/__snapshots__/negative.spec.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`typescript - negative scenarios - configPaths flow should fail compilin
44

55
exports[`typescript - negative scenarios - configPaths flow should fail compiling test/it/code-samples/negative/backend-unknown-module folder 1`] = `"unknown.js (2,15): Cannot find module 'custom-modulezzz' or its corresponding type declarations.,unknown.js (3,11): Cannot find name 'wixUnknownModule'."`;
66

7-
exports[`typescript - negative scenarios - configPaths flow should fail compiling test/it/code-samples/negative/pages-$w-selector folder 1`] = `"index.js (7,18): Property 'zProperty' does not exist on type 'Button'.,index.js (10,6): Argument of type '\\"#appwidget1, AnotherSelector\\"' is not assignable to parameter of type '\\"Document\\" | \\"AccountNavBar\\" | \\"Anchor\\" | \\"Box\\" | \\"Button\\" | \\"Checkbox\\" | \\"CheckboxGroup\\" | \\"Column\\" | \\"ColumnStrip\\" | \\"Container\\" | \\"DatePicker\\" | \\"Dropdown\\" | \\"Footer\\" | \\"Gallery\\" | ... 43 more ... | \\"#wixChat1\\"'.,index.js (13,6): Argument of type 'string' is not assignable to parameter of type '\\"Document\\" | \\"AccountNavBar\\" | \\"Anchor\\" | \\"Box\\" | \\"Button\\" | \\"Checkbox\\" | \\"CheckboxGroup\\" | \\"Column\\" | \\"ColumnStrip\\" | \\"Container\\" | \\"DatePicker\\" | \\"Dropdown\\" | \\"Footer\\" | \\"Gallery\\" | ... 43 more ... | \\"#wixChat1\\"'."`;
7+
exports[`typescript - negative scenarios - configPaths flow should fail compiling test/it/code-samples/negative/pages-$w-selector folder 1`] = `"index.js (7,18): Property 'zProperty' does not exist on type 'Button'.,index.js (10,6): Argument of type '\\"#appwidget1, AnotherSelector\\"' is not assignable to parameter of type 'WixElementSelector'.,index.js (13,6): Argument of type 'string' is not assignable to parameter of type 'WixElementSelector'."`;
88

99
exports[`typescript - negative scenarios - configPaths flow should fail compiling test/it/code-samples/negative/pages-bad-usage-of-existing-prop-on-container-children folder 1`] = `"pageCode.js (2,19): Argument of type 'string' is not assignable to parameter of type 'MouseEventHandler'."`;
1010

@@ -28,7 +28,7 @@ exports[`typescript - negative scenarios - declarations flow should fail compili
2828

2929
exports[`typescript - negative scenarios - declarations flow should fail compiling test/it/code-samples/negative/backend-unknown-module folder 1`] = `"unknown.js (2,15): Cannot find module 'custom-modulezzz' or its corresponding type declarations.,unknown.js (3,11): Cannot find name 'wixUnknownModule'."`;
3030

31-
exports[`typescript - negative scenarios - declarations flow should fail compiling test/it/code-samples/negative/pages-$w-selector folder 1`] = `"index.js (7,18): Property 'zProperty' does not exist on type 'Button'.,index.js (10,6): Argument of type '\\"#appwidget1, AnotherSelector\\"' is not assignable to parameter of type '\\"Document\\" | \\"AccountNavBar\\" | \\"Anchor\\" | \\"Box\\" | \\"Button\\" | \\"Checkbox\\" | \\"CheckboxGroup\\" | \\"Column\\" | \\"ColumnStrip\\" | \\"Container\\" | \\"DatePicker\\" | \\"Dropdown\\" | \\"Footer\\" | \\"Gallery\\" | ... 43 more ... | \\"#wixChat1\\"'.,index.js (13,6): Argument of type 'string' is not assignable to parameter of type '\\"Document\\" | \\"AccountNavBar\\" | \\"Anchor\\" | \\"Box\\" | \\"Button\\" | \\"Checkbox\\" | \\"CheckboxGroup\\" | \\"Column\\" | \\"ColumnStrip\\" | \\"Container\\" | \\"DatePicker\\" | \\"Dropdown\\" | \\"Footer\\" | \\"Gallery\\" | ... 43 more ... | \\"#wixChat1\\"'."`;
31+
exports[`typescript - negative scenarios - declarations flow should fail compiling test/it/code-samples/negative/pages-$w-selector folder 1`] = `"index.js (7,18): Property 'zProperty' does not exist on type 'Button'.,index.js (10,6): Argument of type '\\"#appwidget1, AnotherSelector\\"' is not assignable to parameter of type 'WixElementSelector'.,index.js (13,6): Argument of type 'string' is not assignable to parameter of type 'WixElementSelector'."`;
3232

3333
exports[`typescript - negative scenarios - declarations flow should fail compiling test/it/code-samples/negative/pages-bad-usage-of-existing-prop-on-container-children folder 1`] = `"pageCode.js (2,19): Argument of type 'string' is not assignable to parameter of type 'MouseEventHandler'."`;
3434

0 commit comments

Comments
 (0)