Skip to content

Commit

Permalink
Workaround: apply loc type guarantee with patch
Browse files Browse the repository at this point in the history
The types are generated by jsdoc, which does not support nontrivial type
expressions and [does not seem likely to do so in the future][1]. Using
different tooling would be a hassle.

So just apply these minimal type changes with a patch when building types.

If future changes cause the jsdoc type output to conflict with this patch,
then that will not go unnoticed as the build:types run-script will fail.
If/when that happens, one can just manually edit `dist/fx.d.ts` and regenerate
the patch with `git diff dist/fx.d.ts > convey-loc-guarantee-in-types.patch`.

[1]: jsdoc/jsdoc#1917
  • Loading branch information
gthb committed Oct 8, 2023
1 parent 928ba31 commit 1ecb0ce
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
59 changes: 59 additions & 0 deletions convey-loc-guarantee-in-types.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
diff --git a/dist/fx.d.ts b/dist/fx.d.ts
index a4485ca..23f8bc2 100644
--- a/dist/fx.d.ts
+++ b/dist/fx.d.ts
@@ -66,12 +66,12 @@ export declare function addA1RangeBounds(range: RangeA1): RangeA1;
* @param [context.workbookName=''] An implied workbook name ('report.xlsx')
* @returns The input array with the enchanced tokens
*/
-export declare function addTokenMeta(tokenlist: Array<Token>, context?: {
+export declare function addTokenMeta<T extends Token>(tokenlist: Array<T>, context?: {
/** An implied sheet name ('Sheet1') */
sheetName?: string;
/** An implied workbook name ('report.xlsx') */
workbookName?: string;
-}): Array<TokenEnhanced>;
+}): Array<T & TokenEnhanced>;

/**
* Normalizes A1 style ranges and structured references in a formula or list of
@@ -252,7 +252,7 @@ export declare function mergeRefTokens(tokenlist: Array<Token>): Array<Token>;
* @param [options.xlsx=false] Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md)
* @returns An AST of nodes
*/
-export declare function parse(formula: (string | Array<Token>), options?: {
+export declare function parse<WithLoc extends boolean = true>(formula: (string | Array<Token>), options?: {
/** Enable parsing names as well as ranges. */
allowNamed?: boolean;
/** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
@@ -266,7 +266,7 @@ export declare function parse(formula: (string | Array<Token>), options?: {
/** Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. */
r1c1?: boolean;
/** Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` */
- withLocation?: boolean;
+ withLocation?: WithLoc;
/** Switches to the `[1]Sheet1!A1` or `[1]!name` prefix syntax form for external workbooks. See: [Prefixes.md](./Prefixes.md) */
xlsx?: boolean;
}): object;
@@ -509,7 +509,7 @@ declare function toR1C1(range: RangeR1C1): string;
* @param [options.xlsx=false] Enables a `[1]Sheet1!A1` or `[1]!name` syntax form for external workbooks found only in XLSX files.
* @returns An AST of nodes
*/
-export declare function tokenize(formula: string, options?: {
+export declare function tokenize<WithLoc extends boolean = true>(formula: string, options?: {
/** Enables the recognition of ternary ranges in the style of `A1:A` or `A1:1`. These are supported by Google Sheets but not Excel. See: References.md. */
allowTernary?: boolean;
/** Should ranges be returned as whole references (`Sheet1!A1:B2`) or as separate tokens for each part: (`Sheet1`,`!`,`A1`,`:`,`B2`). This is the same as calling [`mergeRefTokens`](#mergeRefTokens) */
@@ -519,10 +519,10 @@ export declare function tokenize(formula: string, options?: {
/** Ranges are expected to be in the R1C1 style format rather than the more popular A1 style. */
r1c1?: boolean;
/** Nodes will include source position offsets to the tokens: `{ loc: [ start, end ] }` */
- withLocation?: boolean;
+ withLocation?: WithLoc;
/** Enables a `[1]Sheet1!A1` or `[1]!name` syntax form for external workbooks found only in XLSX files. */
xlsx?: boolean;
-}): Array<Token>;
+}): WithLoc extends false ? Array<Token> : Array<Token & { loc: number[] }>;

/**
* Translates ranges in a formula or list of tokens from relative R1C1 syntax to
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"version": "npm run build",
"lint": "eslint lib/*.js",
"test": "tape lib/*.spec.js | tap-min",
"build:types": "jsdoc -c tsd.json lib>dist/fx.d.ts",
"build:types": "jsdoc -c tsd.json lib>dist/fx.d.ts && patch dist/fx.d.ts convey-loc-guarantee-in-types.patch",
"build:docs": "echo '# _Fx_ API\n'>docs/API.md; jsdoc -t node_modules/@borgar/jsdoc-tsmd -d console lib>>docs/API.md",
"build": "NODE_ENV=production rollup -c"
},
Expand Down

0 comments on commit 1ecb0ce

Please sign in to comment.