@elg/tscodegen 0.1.1 → 0.3.0
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/.circleci/config.yml +38 -0
- package/CHANGELOG.md +35 -0
- package/README.md +10 -5
- package/coverage/coverage-final.json +6 -0
- package/dist/CodeBuilder.d.ts +4 -1
- package/dist/CodeBuilder.js +41 -33
- package/dist/CodeFile.d.ts +10 -0
- package/dist/CodeFile.js +42 -28
- package/dist/codelock.d.ts +2 -1
- package/dist/codelock.js +11 -13
- package/dist/index.js +6 -2
- package/dist/sections/docblock.js +4 -5
- package/dist/sections/manual.js +3 -4
- package/dist/types/ManualSectionMap.d.ts +1 -1
- package/eslint.config.mjs +49 -0
- package/package.json +28 -21
- package/test-reports/junit/junit.xml +3 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
version: 2.1
|
|
2
|
+
|
|
3
|
+
jobs:
|
|
4
|
+
build:
|
|
5
|
+
working_directory: ~/tscodegen
|
|
6
|
+
docker:
|
|
7
|
+
- image: cimg/node:lts
|
|
8
|
+
steps:
|
|
9
|
+
- checkout
|
|
10
|
+
- restore_cache:
|
|
11
|
+
keys:
|
|
12
|
+
- v3-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
|
|
13
|
+
- v3-node-{{ arch }}-{{ .Branch }}-
|
|
14
|
+
- v3-node-{{ arch }}-
|
|
15
|
+
- run:
|
|
16
|
+
name: Install Dependencies
|
|
17
|
+
command: yarn --frozen-lockfile --cache-folder ~/.cache/yarn --non-interactive
|
|
18
|
+
- save_cache:
|
|
19
|
+
key: v3-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
|
|
20
|
+
paths:
|
|
21
|
+
- ~/.cache/yarn
|
|
22
|
+
- node_modules
|
|
23
|
+
- run:
|
|
24
|
+
name: Typecheck Code
|
|
25
|
+
command: yarn typecheck
|
|
26
|
+
- run:
|
|
27
|
+
name: Run Linters
|
|
28
|
+
command: yarn lint
|
|
29
|
+
- run:
|
|
30
|
+
name: Test code
|
|
31
|
+
environment:
|
|
32
|
+
NODE_ENV: test
|
|
33
|
+
command: |
|
|
34
|
+
set -e
|
|
35
|
+
yarn test
|
|
36
|
+
yarn codecov --disable=gcov -f ./coverage/coverage-final.json
|
|
37
|
+
- store_test_results:
|
|
38
|
+
path: test-reports
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.3.0] - 2026-03-02
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- `CodeBuilder.format` now accepts an optional `prettierOptions` parameter to customize Prettier formatting without resolving config from disk.
|
|
13
|
+
|
|
14
|
+
### Breaking
|
|
15
|
+
|
|
16
|
+
- **Prettier 3 only:** `peerDependencies.prettier` is now `3.x` (was `2.x || 3.x`).
|
|
17
|
+
- Added `@prettier/sync` as a runtime dependency for Prettier 3 compatibility.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- Bumped dev dependencies to latest: TypeScript 5.x, ESLint 10.x, Jest 30.x, Prettier 3.x, and related tooling.
|
|
22
|
+
- TypeScript target updated from ES2015 to ES2018.
|
|
23
|
+
- Migrated ESLint to flat config (`eslint.config.mjs`).
|
|
24
|
+
|
|
25
|
+
## [0.2.0] - 2020-06-08
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- `CodeFile.lock`, a new manual lock step that allows you to customize the
|
|
30
|
+
lock docstring.
|
|
31
|
+
|
|
32
|
+
### Changed
|
|
33
|
+
|
|
34
|
+
- **BREAKNG:** `CodeFile.build` no longer locks the file automatically. Call
|
|
35
|
+
`CodeFile.lock` after calling `build` to get the old behavior.
|
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# tscodegen
|
|
2
2
|
|
|
3
|
+
[](https://circleci.com/gh/taneliang/tscodegen)
|
|
4
|
+
[](https://codecov.io/gh/taneliang/tscodegen)
|
|
5
|
+
[](https://codeclimate.com/github/taneliang/tscodegen/maintainability)
|
|
6
|
+
|
|
3
7
|
[tscodegen](https://www.npmjs.com/package/@elg/tscodegen) is a minimal
|
|
4
8
|
TypeScript port of [Facebook's Hack
|
|
5
9
|
Codegen](https://hhvm.github.io/hack-codegen). It provides a fluent API that
|
|
@@ -67,17 +71,18 @@ new CodeFile("./file.ts")
|
|
|
67
71
|
.addBlock("class Steam extends Water", (builder) =>
|
|
68
72
|
builder
|
|
69
73
|
.addBlock("constructor()", (builder) =>
|
|
70
|
-
builder.addLine("this.boil();")
|
|
74
|
+
builder.addLine("this.boil();"),
|
|
71
75
|
)
|
|
72
76
|
.addLine()
|
|
73
77
|
.addBlock("boil()", (b) =>
|
|
74
78
|
b.addManualSection("boil_body", (builder) =>
|
|
75
|
-
builder.add("this.temp = 100;")
|
|
76
|
-
)
|
|
77
|
-
)
|
|
79
|
+
builder.add("this.temp = 100;"),
|
|
80
|
+
),
|
|
81
|
+
),
|
|
78
82
|
)
|
|
79
|
-
.format()
|
|
83
|
+
.format(),
|
|
80
84
|
)
|
|
85
|
+
.lock()
|
|
81
86
|
.saveToFile();
|
|
82
87
|
```
|
|
83
88
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{"/Users/e-liang/a/tscodegen/src/CodeFile.ts": {"path":"/Users/e-liang/a/tscodegen/src/CodeFile.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":44}},"2":{"start":{"line":3,"column":0},"end":{"line":3,"column":67}},"3":{"start":{"line":4,"column":0},"end":{"line":4,"column":58}},"4":{"start":{"line":10,"column":2},"end":{"line":10,"column":35}},"5":{"start":{"line":11,"column":2},"end":{"line":11,"column":29}},"6":{"start":{"line":12,"column":2},"end":{"line":12,"column":21}},"7":{"start":{"line":13,"column":2},"end":{"line":13,"column":46}},"8":{"start":{"line":16,"column":4},"end":{"line":16,"column":42}},"9":{"start":{"line":17,"column":4},"end":{"line":23,"column":null}},"10":{"start":{"line":18,"column":6},"end":{"line":18,"column":76}},"11":{"start":{"line":19,"column":6},"end":{"line":19,"column":54}},"12":{"start":{"line":20,"column":6},"end":{"line":22,"column":31}},"13":{"start":{"line":30,"column":4},"end":{"line":30,"column":42}},"14":{"start":{"line":40,"column":20},"end":{"line":41,"column":null}},"15":{"start":{"line":43,"column":4},"end":{"line":43,"column":44}},"16":{"start":{"line":44,"column":4},"end":{"line":44,"column":62}},"17":{"start":{"line":45,"column":4},"end":{"line":45,"column":16}},"18":{"start":{"line":58,"column":4},"end":{"line":62,"column":6}},"19":{"start":{"line":63,"column":4},"end":{"line":63,"column":16}},"20":{"start":{"line":70,"column":4},"end":{"line":70,"column":30}},"21":{"start":{"line":81,"column":4},"end":{"line":84,"column":null}},"22":{"start":{"line":82,"column":6},"end":{"line":82,"column":74}},"23":{"start":{"line":83,"column":6},"end":{"line":83,"column":54}},"24":{"start":{"line":9,"column":0},"end":{"line":9,"column":13}}},"fnMap":{"0":{"name":"(anonymous_3)","decl":{"start":{"line":15,"column":2},"end":{"line":15,"column":14}},"loc":{"start":{"line":15,"column":36},"end":{"line":24,"column":3}}},"1":{"name":"(anonymous_4)","decl":{"start":{"line":29,"column":2},"end":{"line":29,"column":8}},"loc":{"start":{"line":29,"column":8},"end":{"line":31,"column":3}}},"2":{"name":"(anonymous_5)","decl":{"start":{"line":39,"column":2},"end":{"line":39,"column":7}},"loc":{"start":{"line":39,"column":61},"end":{"line":46,"column":3}}},"3":{"name":"(anonymous_6)","decl":{"start":{"line":57,"column":2},"end":{"line":57,"column":6}},"loc":{"start":{"line":57,"column":25},"end":{"line":64,"column":3}}},"4":{"name":"(anonymous_7)","decl":{"start":{"line":69,"column":2},"end":{"line":69,"column":10}},"loc":{"start":{"line":69,"column":10},"end":{"line":71,"column":3}}},"5":{"name":"(anonymous_8)","decl":{"start":{"line":80,"column":2},"end":{"line":80,"column":12}},"loc":{"start":{"line":80,"column":26},"end":{"line":85,"column":3}}}},"branchMap":{"0":{"loc":{"start":{"line":17,"column":4},"end":{"line":23,"column":null}},"type":"if","locations":[{"start":{"line":17,"column":4},"end":{"line":23,"column":null}},{"start":{"line":17,"column":4},"end":{"line":23,"column":null}}]},"1":{"loc":{"start":{"line":22,"column":7},"end":{"line":22,"column":9}},"type":"cond-expr","locations":[{"start":{"line":22,"column":7},"end":{"line":22,"column":9}},{"start":{"line":22,"column":7},"end":{"line":22,"column":30}}]},"2":{"loc":{"start":{"line":20,"column":10},"end":{"line":22,"column":9}},"type":"binary-expr","locations":[{"start":{"line":20,"column":10},"end":{"line":22,"column":9}},{"start":{"line":22,"column":7},"end":{"line":22,"column":9}}]},"3":{"loc":{"start":{"line":57,"column":23},"end":{"line":57,"column":25}},"type":"default-arg","locations":[{"start":{"line":57,"column":23},"end":{"line":57,"column":25}}]},"4":{"loc":{"start":{"line":58,"column":33},"end":{"line":58,"column":null}},"type":"cond-expr","locations":[{"start":{"line":58,"column":33},"end":{"line":58,"column":null}},{"start":{"line":60,"column":37},"end":{"line":60,"column":42}}]},"5":{"loc":{"start":{"line":58,"column":33},"end":{"line":58,"column":null}},"type":"binary-expr","locations":[{"start":{"line":58,"column":33},"end":{"line":58,"column":null}},{"start":{"line":58,"column":33},"end":{"line":58,"column":null}}]},"6":{"loc":{"start":{"line":80,"column":21},"end":{"line":80,"column":26}},"type":"default-arg","locations":[{"start":{"line":80,"column":21},"end":{"line":80,"column":26}}]},"7":{"loc":{"start":{"line":81,"column":4},"end":{"line":84,"column":null}},"type":"if","locations":[{"start":{"line":81,"column":4},"end":{"line":84,"column":null}},{"start":{"line":81,"column":4},"end":{"line":84,"column":null}}]},"8":{"loc":{"start":{"line":81,"column":8},"end":{"line":81,"column":13}},"type":"binary-expr","locations":[{"start":{"line":81,"column":8},"end":{"line":81,"column":13}},{"start":{"line":81,"column":17},"end":{"line":81,"column":66}}]}},"s":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0},"f":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0},"b":{"0":[0,0],"1":[0,0],"2":[0,0],"3":[0],"4":[0,0],"5":[0,0],"6":[0],"7":[0,0],"8":[0,0]}}
|
|
2
|
+
,"/Users/e-liang/a/tscodegen/src/codelock.ts": {"path":"/Users/e-liang/a/tscodegen/src/codelock.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":null}},"2":{"start":{"line":7,"column":0},"end":{"line":7,"column":56}},"3":{"start":{"line":21,"column":19},"end":{"line":21,"column":46}},"4":{"start":{"line":22,"column":2},"end":{"line":24,"column":null}},"5":{"start":{"line":23,"column":4},"end":{"line":23,"column":21}},"6":{"start":{"line":26,"column":24},"end":{"line":26,"column":44}},"7":{"start":{"line":27,"column":2},"end":{"line":29,"column":null}},"8":{"start":{"line":28,"column":4},"end":{"line":28,"column":21}},"9":{"start":{"line":32,"column":19},"end":{"line":32,"column":58}},"10":{"start":{"line":34,"column":27},"end":{"line":35,"column":76}},"11":{"start":{"line":36,"column":2},"end":{"line":41,"column":null}},"12":{"start":{"line":37,"column":4},"end":{"line":40,"column":6}},"13":{"start":{"line":43,"column":29},"end":{"line":45,"column":11}},"14":{"start":{"line":46,"column":2},"end":{"line":51,"column":null}},"15":{"start":{"line":47,"column":4},"end":{"line":50,"column":6}},"16":{"start":{"line":53,"column":2},"end":{"line":53,"column":19}},"17":{"start":{"line":19,"column":0},"end":{"line":19,"column":16}},"18":{"start":{"line":65,"column":23},"end":{"line":67,"column":10}},"19":{"start":{"line":68,"column":2},"end":{"line":71,"column":22}},"20":{"start":{"line":89,"column":15},"end":{"line":89,"column":55}},"21":{"start":{"line":93,"column":2},"end":{"line":103,"column":null}},"22":{"start":{"line":94,"column":4},"end":{"line":98,"column":41}},"23":{"start":{"line":100,"column":4},"end":{"line":102,"column":32}},"24":{"start":{"line":105,"column":2},"end":{"line":105,"column":52}},"25":{"start":{"line":84,"column":0},"end":{"line":84,"column":16}},"26":{"start":{"line":115,"column":24},"end":{"line":115,"column":51}},"27":{"start":{"line":116,"column":2},"end":{"line":118,"column":null}},"28":{"start":{"line":117,"column":4},"end":{"line":117,"column":17}},"29":{"start":{"line":119,"column":2},"end":{"line":125,"column":4}},"30":{"start":{"line":114,"column":0},"end":{"line":114,"column":16}}},"fnMap":{"0":{"name":"getCodelockInfo","decl":{"start":{"line":19,"column":16},"end":{"line":19,"column":31}},"loc":{"start":{"line":19,"column":50},"end":{"line":54,"column":1}}},"1":{"name":"computeHash","decl":{"start":{"line":64,"column":9},"end":{"line":64,"column":20}},"loc":{"start":{"line":64,"column":69},"end":{"line":72,"column":1}}},"2":{"name":"lockCode","decl":{"start":{"line":84,"column":16},"end":{"line":84,"column":24}},"loc":{"start":{"line":87,"column":20},"end":{"line":106,"column":1}}},"3":{"name":"verifyLock","decl":{"start":{"line":114,"column":16},"end":{"line":114,"column":26}},"loc":{"start":{"line":114,"column":45},"end":{"line":126,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":22,"column":2},"end":{"line":24,"column":null}},"type":"if","locations":[{"start":{"line":22,"column":2},"end":{"line":24,"column":null}},{"start":{"line":22,"column":2},"end":{"line":24,"column":null}}]},"1":{"loc":{"start":{"line":27,"column":2},"end":{"line":29,"column":null}},"type":"if","locations":[{"start":{"line":27,"column":2},"end":{"line":29,"column":null}},{"start":{"line":27,"column":2},"end":{"line":29,"column":null}}]},"2":{"loc":{"start":{"line":35,"column":68},"end":{"line":35,"column":70}},"type":"cond-expr","locations":[{"start":{"line":35,"column":68},"end":{"line":35,"column":70}},{"start":{"line":35,"column":68},"end":{"line":35,"column":76}}]},"3":{"loc":{"start":{"line":34,"column":27},"end":{"line":35,"column":70}},"type":"binary-expr","locations":[{"start":{"line":34,"column":27},"end":{"line":35,"column":70}},{"start":{"line":35,"column":68},"end":{"line":35,"column":70}}]},"4":{"loc":{"start":{"line":36,"column":2},"end":{"line":41,"column":null}},"type":"if","locations":[{"start":{"line":36,"column":2},"end":{"line":41,"column":null}},{"start":{"line":36,"column":2},"end":{"line":41,"column":null}}]},"5":{"loc":{"start":{"line":45,"column":3},"end":{"line":45,"column":5}},"type":"cond-expr","locations":[{"start":{"line":45,"column":3},"end":{"line":45,"column":5}},{"start":{"line":45,"column":3},"end":{"line":45,"column":11}}]},"6":{"loc":{"start":{"line":43,"column":29},"end":{"line":45,"column":5}},"type":"binary-expr","locations":[{"start":{"line":43,"column":29},"end":{"line":45,"column":5}},{"start":{"line":45,"column":3},"end":{"line":45,"column":5}}]},"7":{"loc":{"start":{"line":46,"column":2},"end":{"line":51,"column":null}},"type":"if","locations":[{"start":{"line":46,"column":2},"end":{"line":51,"column":null}},{"start":{"line":46,"column":2},"end":{"line":51,"column":null}}]},"8":{"loc":{"start":{"line":66,"column":32},"end":{"line":66,"column":57}},"type":"cond-expr","locations":[{"start":{"line":66,"column":32},"end":{"line":66,"column":57}},{"start":{"line":66,"column":60},"end":{"line":66,"column":64}}]},"9":{"loc":{"start":{"line":87,"column":18},"end":{"line":87,"column":20}},"type":"default-arg","locations":[{"start":{"line":87,"column":18},"end":{"line":87,"column":20}}]},"10":{"loc":{"start":{"line":93,"column":2},"end":{"line":103,"column":null}},"type":"if","locations":[{"start":{"line":93,"column":2},"end":{"line":103,"column":null}},{"start":{"line":93,"column":2},"end":{"line":103,"column":null}}]},"11":{"loc":{"start":{"line":116,"column":2},"end":{"line":118,"column":null}},"type":"if","locations":[{"start":{"line":116,"column":2},"end":{"line":118,"column":null}},{"start":{"line":116,"column":2},"end":{"line":118,"column":null}}]}},"s":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":0},"f":{"0":0,"1":0,"2":0,"3":0},"b":{"0":[0,0],"1":[0,0],"2":[0,0],"3":[0,0],"4":[0,0],"5":[0,0],"6":[0,0],"7":[0,0],"8":[0,0],"9":[0],"10":[0,0],"11":[0,0]}}
|
|
3
|
+
,"/Users/e-liang/a/tscodegen/src/index.ts": {"path":"/Users/e-liang/a/tscodegen/src/index.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":30}},"2":{"start":{"line":3,"column":0},"end":{"line":3,"column":41}}},"fnMap":{},"branchMap":{},"s":{"0":0,"1":0,"2":0},"f":{},"b":{}}
|
|
4
|
+
,"/Users/e-liang/a/tscodegen/src/sections/docblock.ts": {"path":"/Users/e-liang/a/tscodegen/src/sections/docblock.ts","statementMap":{"0":{"start":{"line":1,"column":28},"end":{"line":1,"column":70}},"1":{"start":{"line":13,"column":30},"end":{"line":13,"column":71}},"2":{"start":{"line":14,"column":2},"end":{"line":16,"column":null}},"3":{"start":{"line":15,"column":4},"end":{"line":15,"column":21}},"4":{"start":{"line":18,"column":22},"end":{"line":18,"column":53}},"5":{"start":{"line":19,"column":2},"end":{"line":27,"column":4}},"6":{"start":{"line":23,"column":21},"end":{"line":23,"column":55}},"7":{"start":{"line":12,"column":0},"end":{"line":12,"column":16}},"8":{"start":{"line":39,"column":2},"end":{"line":41,"column":null}},"9":{"start":{"line":40,"column":4},"end":{"line":40,"column":16}},"10":{"start":{"line":42,"column":2},"end":{"line":42,"column":47}},"11":{"start":{"line":38,"column":0},"end":{"line":38,"column":16}},"12":{"start":{"line":51,"column":42},"end":{"line":54,"column":15}},"13":{"start":{"line":53,"column":19},"end":{"line":53,"column":43}},"14":{"start":{"line":55,"column":19},"end":{"line":55,"column":65}},"15":{"start":{"line":56,"column":2},"end":{"line":56,"column":18}},"16":{"start":{"line":50,"column":0},"end":{"line":50,"column":16}},"17":{"start":{"line":70,"column":2},"end":{"line":70,"column":69}},"18":{"start":{"line":66,"column":0},"end":{"line":66,"column":16}}},"fnMap":{"0":{"name":"getFileDocblock","decl":{"start":{"line":12,"column":16},"end":{"line":12,"column":31}},"loc":{"start":{"line":12,"column":44},"end":{"line":28,"column":1}}},"1":{"name":"(anonymous_1)","decl":{"start":{"line":23,"column":11},"end":{"line":23,"column":12}},"loc":{"start":{"line":23,"column":21},"end":{"line":23,"column":55}}},"2":{"name":"removeFileDocblock","decl":{"start":{"line":38,"column":16},"end":{"line":38,"column":34}},"loc":{"start":{"line":38,"column":47},"end":{"line":43,"column":1}}},"3":{"name":"createDocblock","decl":{"start":{"line":50,"column":16},"end":{"line":50,"column":30}},"loc":{"start":{"line":50,"column":54},"end":{"line":57,"column":1}}},"4":{"name":"(anonymous_4)","decl":{"start":{"line":53,"column":9},"end":{"line":53,"column":10}},"loc":{"start":{"line":53,"column":19},"end":{"line":53,"column":43}}},"5":{"name":"prependFileDocblock","decl":{"start":{"line":66,"column":16},"end":{"line":66,"column":35}},"loc":{"start":{"line":68,"column":25},"end":{"line":71,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":13,"column":63},"end":{"line":13,"column":65}},"type":"cond-expr","locations":[{"start":{"line":13,"column":63},"end":{"line":13,"column":65}},{"start":{"line":13,"column":63},"end":{"line":13,"column":71}}]},"1":{"loc":{"start":{"line":13,"column":30},"end":{"line":13,"column":65}},"type":"binary-expr","locations":[{"start":{"line":13,"column":30},"end":{"line":13,"column":65}},{"start":{"line":13,"column":63},"end":{"line":13,"column":65}}]},"2":{"loc":{"start":{"line":14,"column":2},"end":{"line":16,"column":null}},"type":"if","locations":[{"start":{"line":14,"column":2},"end":{"line":16,"column":null}},{"start":{"line":14,"column":2},"end":{"line":16,"column":null}}]},"3":{"loc":{"start":{"line":39,"column":2},"end":{"line":41,"column":null}},"type":"if","locations":[{"start":{"line":39,"column":2},"end":{"line":41,"column":null}},{"start":{"line":39,"column":2},"end":{"line":41,"column":null}}]}},"s":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0},"f":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0},"b":{"0":[0,0],"1":[0,0],"2":[0,0],"3":[0,0]}}
|
|
5
|
+
,"/Users/e-liang/a/tscodegen/src/sections/manual.ts": {"path":"/Users/e-liang/a/tscodegen/src/sections/manual.ts","statementMap":{"0":{"start":{"line":4,"column":2},"end":{"line":4,"column":94}},"1":{"start":{"line":11,"column":2},"end":{"line":15,"column":null}},"2":{"start":{"line":12,"column":4},"end":{"line":14,"column":6}},"3":{"start":{"line":17,"column":29},"end":{"line":17,"column":47}},"4":{"start":{"line":18,"column":2},"end":{"line":19,"column":71}},"5":{"start":{"line":21,"column":2},"end":{"line":21,"column":101}},"6":{"start":{"line":6,"column":0},"end":{"line":6,"column":16}},"7":{"start":{"line":25,"column":21},"end":{"line":25,"column":54}},"8":{"start":{"line":26,"column":43},"end":{"line":26,"column":45}},"9":{"start":{"line":27,"column":2},"end":{"line":32,"column":5}},"10":{"start":{"line":28,"column":4},"end":{"line":30,"column":null}},"11":{"start":{"line":29,"column":6},"end":{"line":29,"column":13}},"12":{"start":{"line":31,"column":4},"end":{"line":31,"column":64}},"13":{"start":{"line":33,"column":2},"end":{"line":33,"column":24}},"14":{"start":{"line":24,"column":0},"end":{"line":24,"column":16}},"15":{"start":{"line":42,"column":2},"end":{"line":44,"column":4}},"16":{"start":{"line":43,"column":4},"end":{"line":43,"column":39}},"17":{"start":{"line":41,"column":0},"end":{"line":41,"column":16}}},"fnMap":{"0":{"name":"createManualSection","decl":{"start":{"line":6,"column":16},"end":{"line":6,"column":35}},"loc":{"start":{"line":8,"column":21},"end":{"line":22,"column":1}}},"1":{"name":"extractManualSections","decl":{"start":{"line":24,"column":16},"end":{"line":24,"column":37}},"loc":{"start":{"line":24,"column":50},"end":{"line":34,"column":1}}},"2":{"name":"(anonymous_2)","decl":{"start":{"line":27,"column":26},"end":{"line":27,"column":27}},"loc":{"start":{"line":27,"column":54},"end":{"line":32,"column":3}}},"3":{"name":"emptyManualSections","decl":{"start":{"line":41,"column":16},"end":{"line":41,"column":35}},"loc":{"start":{"line":41,"column":48},"end":{"line":45,"column":1}}},"4":{"name":"(anonymous_4)","decl":{"start":{"line":42,"column":42},"end":{"line":42,"column":43}},"loc":{"start":{"line":43,"column":4},"end":{"line":43,"column":39}}}},"branchMap":{"0":{"loc":{"start":{"line":11,"column":2},"end":{"line":15,"column":null}},"type":"if","locations":[{"start":{"line":11,"column":2},"end":{"line":15,"column":null}},{"start":{"line":11,"column":2},"end":{"line":15,"column":null}}]},"1":{"loc":{"start":{"line":11,"column":6},"end":{"line":11,"column":29}},"type":"binary-expr","locations":[{"start":{"line":11,"column":6},"end":{"line":11,"column":29}},{"start":{"line":11,"column":33},"end":{"line":11,"column":54}}]},"2":{"loc":{"start":{"line":19,"column":38},"end":{"line":19,"column":65}},"type":"cond-expr","locations":[{"start":{"line":19,"column":38},"end":{"line":19,"column":65}},{"start":{"line":19,"column":68},"end":{"line":19,"column":70}}]},"3":{"loc":{"start":{"line":28,"column":4},"end":{"line":30,"column":null}},"type":"if","locations":[{"start":{"line":28,"column":4},"end":{"line":30,"column":null}},{"start":{"line":28,"column":4},"end":{"line":30,"column":null}}]}},"s":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0},"f":{"0":0,"1":0,"2":0,"3":0,"4":0},"b":{"0":[0,0],"1":[0,0],"2":[0,0],"3":[0,0]}}
|
|
6
|
+
}
|
package/dist/CodeBuilder.d.ts
CHANGED
|
@@ -36,8 +36,11 @@ export declare class CodeBuilder {
|
|
|
36
36
|
addManualSection(sectionKey: string, sectionBuilder: (manualSectionBuilder: CodeBuilder) => CodeBuilder): this;
|
|
37
37
|
/**
|
|
38
38
|
* Formats the stored code with Prettier.
|
|
39
|
+
*
|
|
40
|
+
* @param prettierOptions Optional Prettier configuration object. If provided,
|
|
41
|
+
* this will be used directly and no config will be resolved from disk.
|
|
39
42
|
*/
|
|
40
|
-
format(): this;
|
|
43
|
+
format(prettierOptions?: import("prettier").Options): this;
|
|
41
44
|
/**
|
|
42
45
|
* Returns the stored code.
|
|
43
46
|
*/
|
package/dist/CodeBuilder.js
CHANGED
|
@@ -1,45 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver,
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return value;
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
8
7
|
};
|
|
9
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver,
|
|
10
|
-
if (!
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return privateMap.get(receiver);
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
14
12
|
};
|
|
15
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
15
|
};
|
|
18
|
-
var
|
|
16
|
+
var _CodeBuilder_gennedCode, _CodeBuilder_hasManualSections, _CodeBuilder_existingManualSections;
|
|
19
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
18
|
exports.CodeBuilder = void 0;
|
|
21
|
-
const
|
|
19
|
+
const sync_1 = __importDefault(require("@prettier/sync"));
|
|
22
20
|
const manual_1 = require("./sections/manual");
|
|
23
21
|
const docblock_1 = require("./sections/docblock");
|
|
24
22
|
class CodeBuilder {
|
|
25
23
|
constructor(manualSections) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
__classPrivateFieldSet(this,
|
|
24
|
+
_CodeBuilder_gennedCode.set(this, "");
|
|
25
|
+
_CodeBuilder_hasManualSections.set(this, false);
|
|
26
|
+
_CodeBuilder_existingManualSections.set(this, void 0);
|
|
27
|
+
__classPrivateFieldSet(this, _CodeBuilder_existingManualSections, manualSections, "f");
|
|
30
28
|
}
|
|
31
29
|
/**
|
|
32
30
|
* Simply appends `code`.
|
|
33
31
|
*/
|
|
34
32
|
add(code) {
|
|
35
|
-
__classPrivateFieldSet(this,
|
|
33
|
+
__classPrivateFieldSet(this, _CodeBuilder_gennedCode, __classPrivateFieldGet(this, _CodeBuilder_gennedCode, "f") + code, "f");
|
|
36
34
|
return this;
|
|
37
35
|
}
|
|
38
36
|
/**
|
|
39
37
|
* Appends `code` and a newline. Call without arguments to insert a newline.
|
|
40
38
|
*/
|
|
41
39
|
addLine(code = "") {
|
|
42
|
-
__classPrivateFieldSet(this,
|
|
40
|
+
__classPrivateFieldSet(this, _CodeBuilder_gennedCode, __classPrivateFieldGet(this, _CodeBuilder_gennedCode, "f") + (code + "\n"), "f");
|
|
43
41
|
return this;
|
|
44
42
|
}
|
|
45
43
|
/**
|
|
@@ -49,7 +47,7 @@ class CodeBuilder {
|
|
|
49
47
|
* @param docblockContent Plain docblock content (i.e. without "*"s at the start of each line)
|
|
50
48
|
*/
|
|
51
49
|
addDocblock(docblockContent) {
|
|
52
|
-
return this.addLine(docblock_1.createDocblock(docblockContent));
|
|
50
|
+
return this.addLine((0, docblock_1.createDocblock)(docblockContent));
|
|
53
51
|
}
|
|
54
52
|
/**
|
|
55
53
|
* Add a block of code, i.e. code with braces around them.
|
|
@@ -59,8 +57,8 @@ class CodeBuilder {
|
|
|
59
57
|
* in the block.
|
|
60
58
|
*/
|
|
61
59
|
addBlock(codeBeforeBlock, blockBuilder) {
|
|
62
|
-
const builtBlockBuilder = blockBuilder(new CodeBuilder(__classPrivateFieldGet(this,
|
|
63
|
-
__classPrivateFieldSet(this,
|
|
60
|
+
const builtBlockBuilder = blockBuilder(new CodeBuilder(__classPrivateFieldGet(this, _CodeBuilder_existingManualSections, "f")));
|
|
61
|
+
__classPrivateFieldSet(this, _CodeBuilder_hasManualSections, __classPrivateFieldGet(this, _CodeBuilder_hasManualSections, "f") || builtBlockBuilder.hasManualSections(), "f");
|
|
64
62
|
return this.add(codeBeforeBlock)
|
|
65
63
|
.addLine(" {")
|
|
66
64
|
.addLine(builtBlockBuilder.toString())
|
|
@@ -76,35 +74,45 @@ class CodeBuilder {
|
|
|
76
74
|
*/
|
|
77
75
|
addManualSection(sectionKey, sectionBuilder) {
|
|
78
76
|
let sectionContent;
|
|
79
|
-
if (__classPrivateFieldGet(this,
|
|
80
|
-
sectionContent = __classPrivateFieldGet(this,
|
|
77
|
+
if (__classPrivateFieldGet(this, _CodeBuilder_existingManualSections, "f")[sectionKey]) {
|
|
78
|
+
sectionContent = __classPrivateFieldGet(this, _CodeBuilder_existingManualSections, "f")[sectionKey];
|
|
81
79
|
}
|
|
82
80
|
else {
|
|
83
|
-
sectionContent = sectionBuilder(new CodeBuilder(__classPrivateFieldGet(this,
|
|
81
|
+
sectionContent = sectionBuilder(new CodeBuilder(__classPrivateFieldGet(this, _CodeBuilder_existingManualSections, "f"))).toString();
|
|
84
82
|
}
|
|
85
|
-
__classPrivateFieldSet(this,
|
|
86
|
-
return this.addLine(manual_1.createManualSection(sectionKey, sectionContent));
|
|
83
|
+
__classPrivateFieldSet(this, _CodeBuilder_hasManualSections, true, "f");
|
|
84
|
+
return this.addLine((0, manual_1.createManualSection)(sectionKey, sectionContent));
|
|
87
85
|
}
|
|
88
86
|
/**
|
|
89
87
|
* Formats the stored code with Prettier.
|
|
88
|
+
*
|
|
89
|
+
* @param prettierOptions Optional Prettier configuration object. If provided,
|
|
90
|
+
* this will be used directly and no config will be resolved from disk.
|
|
90
91
|
*/
|
|
91
|
-
format() {
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
format(prettierOptions) {
|
|
93
|
+
var _a;
|
|
94
|
+
let options = prettierOptions;
|
|
95
|
+
if (!options) {
|
|
96
|
+
options = (_a = sync_1.default.resolveConfig(process.cwd())) !== null && _a !== void 0 ? _a : {};
|
|
97
|
+
}
|
|
98
|
+
__classPrivateFieldSet(this, _CodeBuilder_gennedCode, sync_1.default.format(__classPrivateFieldGet(this, _CodeBuilder_gennedCode, "f"), {
|
|
99
|
+
...options,
|
|
100
|
+
parser: "typescript",
|
|
101
|
+
}), "f");
|
|
94
102
|
return this;
|
|
95
103
|
}
|
|
96
104
|
/**
|
|
97
105
|
* Returns the stored code.
|
|
98
106
|
*/
|
|
99
107
|
toString() {
|
|
100
|
-
return __classPrivateFieldGet(this,
|
|
108
|
+
return __classPrivateFieldGet(this, _CodeBuilder_gennedCode, "f");
|
|
101
109
|
}
|
|
102
110
|
/**
|
|
103
111
|
* Whether this built code contains at least one manual section.
|
|
104
112
|
*/
|
|
105
113
|
hasManualSections() {
|
|
106
|
-
return __classPrivateFieldGet(this,
|
|
114
|
+
return __classPrivateFieldGet(this, _CodeBuilder_hasManualSections, "f");
|
|
107
115
|
}
|
|
108
116
|
}
|
|
109
117
|
exports.CodeBuilder = CodeBuilder;
|
|
110
|
-
|
|
118
|
+
_CodeBuilder_gennedCode = new WeakMap(), _CodeBuilder_hasManualSections = new WeakMap(), _CodeBuilder_existingManualSections = new WeakMap();
|
package/dist/CodeFile.d.ts
CHANGED
|
@@ -16,6 +16,16 @@ export declare class CodeFile {
|
|
|
16
16
|
* @param builderBuilder A builder that builds a replacement source.
|
|
17
17
|
*/
|
|
18
18
|
build(builderBuilder: (builder: CodeBuilder) => CodeBuilder): this;
|
|
19
|
+
/**
|
|
20
|
+
* Prepend a codelock docblock to the file.
|
|
21
|
+
*
|
|
22
|
+
* Recommended to be called after `build`.
|
|
23
|
+
*
|
|
24
|
+
* NOTE: This will prepend a docblock even if the file already has one.
|
|
25
|
+
*
|
|
26
|
+
* @param customComment Comment to be interpolated into the lock docblock.
|
|
27
|
+
*/
|
|
28
|
+
lock(customComment?: string): this;
|
|
19
29
|
/**
|
|
20
30
|
* Returns the in-memory representation of the file's source code.
|
|
21
31
|
*/
|
package/dist/CodeFile.js
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver,
|
|
3
|
-
if (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return value;
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
8
7
|
};
|
|
9
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver,
|
|
10
|
-
if (!
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return privateMap.get(receiver);
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
14
12
|
};
|
|
15
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
16
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
17
15
|
};
|
|
18
|
-
var
|
|
16
|
+
var _CodeFile_sourceFilePath, _CodeFile_originalFileContents, _CodeFile_fileContents, _CodeFile_manualSectionsAllowed;
|
|
19
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
18
|
exports.CodeFile = void 0;
|
|
21
19
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -27,19 +25,23 @@ const manual_1 = require("./sections/manual");
|
|
|
27
25
|
*/
|
|
28
26
|
class CodeFile {
|
|
29
27
|
constructor(sourceFilePath) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
var _a;
|
|
29
|
+
_CodeFile_sourceFilePath.set(this, void 0);
|
|
30
|
+
_CodeFile_originalFileContents.set(this, "");
|
|
31
|
+
_CodeFile_fileContents.set(this, "");
|
|
32
|
+
_CodeFile_manualSectionsAllowed.set(this, void 0);
|
|
33
|
+
__classPrivateFieldSet(this, _CodeFile_sourceFilePath, sourceFilePath, "f");
|
|
34
34
|
if (fs_1.default.existsSync(sourceFilePath)) {
|
|
35
|
-
__classPrivateFieldSet(this,
|
|
35
|
+
__classPrivateFieldSet(this, _CodeFile_originalFileContents, fs_1.default.readFileSync(sourceFilePath, "utf-8"), "f");
|
|
36
|
+
__classPrivateFieldSet(this, _CodeFile_fileContents, __classPrivateFieldGet(this, _CodeFile_originalFileContents, "f"), "f");
|
|
37
|
+
__classPrivateFieldSet(this, _CodeFile_manualSectionsAllowed, (_a = (0, codelock_1.getCodelockInfo)(__classPrivateFieldGet(this, _CodeFile_fileContents, "f"))) === null || _a === void 0 ? void 0 : _a.manualSectionsAllowed, "f");
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
/**
|
|
39
41
|
* Verifies that the codelock in the generated file is present and valid.
|
|
40
42
|
*/
|
|
41
43
|
verify() {
|
|
42
|
-
return codelock_1.verifyLock(__classPrivateFieldGet(this,
|
|
44
|
+
return (0, codelock_1.verifyLock)(__classPrivateFieldGet(this, _CodeFile_fileContents, "f"));
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Replace the file's code (this class's in-memory representation at least)
|
|
@@ -48,18 +50,30 @@ class CodeFile {
|
|
|
48
50
|
* @param builderBuilder A builder that builds a replacement source.
|
|
49
51
|
*/
|
|
50
52
|
build(builderBuilder) {
|
|
51
|
-
const builder = builderBuilder(new CodeBuilder_1.CodeBuilder(manual_1.extractManualSections(__classPrivateFieldGet(this,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const builder = builderBuilder(new CodeBuilder_1.CodeBuilder((0, manual_1.extractManualSections)(__classPrivateFieldGet(this, _CodeFile_fileContents, "f"))));
|
|
54
|
+
__classPrivateFieldSet(this, _CodeFile_fileContents, builder.toString(), "f");
|
|
55
|
+
__classPrivateFieldSet(this, _CodeFile_manualSectionsAllowed, builder.hasManualSections(), "f");
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Prepend a codelock docblock to the file.
|
|
60
|
+
*
|
|
61
|
+
* Recommended to be called after `build`.
|
|
62
|
+
*
|
|
63
|
+
* NOTE: This will prepend a docblock even if the file already has one.
|
|
64
|
+
*
|
|
65
|
+
* @param customComment Comment to be interpolated into the lock docblock.
|
|
66
|
+
*/
|
|
67
|
+
lock(customComment = "") {
|
|
68
|
+
var _a;
|
|
69
|
+
__classPrivateFieldSet(this, _CodeFile_fileContents, (0, codelock_1.lockCode)(__classPrivateFieldGet(this, _CodeFile_fileContents, "f"), (_a = __classPrivateFieldGet(this, _CodeFile_manualSectionsAllowed, "f")) !== null && _a !== void 0 ? _a : false, customComment), "f");
|
|
56
70
|
return this;
|
|
57
71
|
}
|
|
58
72
|
/**
|
|
59
73
|
* Returns the in-memory representation of the file's source code.
|
|
60
74
|
*/
|
|
61
75
|
toString() {
|
|
62
|
-
return __classPrivateFieldGet(this,
|
|
76
|
+
return __classPrivateFieldGet(this, _CodeFile_fileContents, "f");
|
|
63
77
|
}
|
|
64
78
|
/**
|
|
65
79
|
* Save to `sourceFilePath` if the in-memory representation of the code has
|
|
@@ -69,11 +83,11 @@ class CodeFile {
|
|
|
69
83
|
* changes.
|
|
70
84
|
*/
|
|
71
85
|
saveToFile(force = false) {
|
|
72
|
-
if (force || __classPrivateFieldGet(this,
|
|
73
|
-
fs_1.default.writeFileSync(__classPrivateFieldGet(this,
|
|
74
|
-
__classPrivateFieldSet(this,
|
|
86
|
+
if (force || __classPrivateFieldGet(this, _CodeFile_originalFileContents, "f") !== __classPrivateFieldGet(this, _CodeFile_fileContents, "f")) {
|
|
87
|
+
fs_1.default.writeFileSync(__classPrivateFieldGet(this, _CodeFile_sourceFilePath, "f"), __classPrivateFieldGet(this, _CodeFile_fileContents, "f"), "utf-8");
|
|
88
|
+
__classPrivateFieldSet(this, _CodeFile_originalFileContents, __classPrivateFieldGet(this, _CodeFile_fileContents, "f"), "f");
|
|
75
89
|
}
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
92
|
exports.CodeFile = CodeFile;
|
|
79
|
-
|
|
93
|
+
_CodeFile_sourceFilePath = new WeakMap(), _CodeFile_originalFileContents = new WeakMap(), _CodeFile_fileContents = new WeakMap(), _CodeFile_manualSectionsAllowed = new WeakMap();
|
package/dist/codelock.d.ts
CHANGED
|
@@ -15,9 +15,10 @@ export declare function getCodelockInfo(lockedCode: string): CodelockInfo | unde
|
|
|
15
15
|
*
|
|
16
16
|
* @param code Code to be locked.
|
|
17
17
|
* @param manualSectionsAllowed Whether generated code can contain manual sections.
|
|
18
|
+
* @param customContent A custom comment to insert into the docblock.
|
|
18
19
|
* @returns Locked code, i.e. code with prepended codelock file docblock.
|
|
19
20
|
*/
|
|
20
|
-
export declare function lockCode(code: string, manualSectionsAllowed: boolean): string;
|
|
21
|
+
export declare function lockCode(code: string, manualSectionsAllowed: boolean, customContent?: string): string;
|
|
21
22
|
/**
|
|
22
23
|
* Verify that the codelock in the source file is valid.
|
|
23
24
|
*
|
package/dist/codelock.js
CHANGED
|
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.getCodelockInfo = getCodelockInfo;
|
|
7
|
+
exports.lockCode = lockCode;
|
|
8
|
+
exports.verifyLock = verifyLock;
|
|
7
9
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
10
|
const docblock_1 = require("./sections/docblock");
|
|
9
11
|
const manual_1 = require("./sections/manual");
|
|
@@ -15,7 +17,7 @@ const manual_1 = require("./sections/manual");
|
|
|
15
17
|
function getCodelockInfo(lockedCode) {
|
|
16
18
|
var _a, _b;
|
|
17
19
|
// TODO: get docblock, and if it exists, retrieve hash
|
|
18
|
-
const docblock = docblock_1.getFileDocblock(lockedCode);
|
|
20
|
+
const docblock = (0, docblock_1.getFileDocblock)(lockedCode);
|
|
19
21
|
if (!docblock) {
|
|
20
22
|
return undefined;
|
|
21
23
|
}
|
|
@@ -41,7 +43,6 @@ function getCodelockInfo(lockedCode) {
|
|
|
41
43
|
}
|
|
42
44
|
return undefined;
|
|
43
45
|
}
|
|
44
|
-
exports.getCodelockInfo = getCodelockInfo;
|
|
45
46
|
/**
|
|
46
47
|
* Computes the codelock hash for a source file. Will not unlock if locked.
|
|
47
48
|
*
|
|
@@ -51,9 +52,7 @@ exports.getCodelockInfo = getCodelockInfo;
|
|
|
51
52
|
* @returns Lock hash for `code`.
|
|
52
53
|
*/
|
|
53
54
|
function computeHash(code, shouldEmptyManualSections) {
|
|
54
|
-
const hashableCode = (shouldEmptyManualSections
|
|
55
|
-
? manual_1.emptyManualSections(code)
|
|
56
|
-
: code).trim();
|
|
55
|
+
const hashableCode = (shouldEmptyManualSections ? (0, manual_1.emptyManualSections)(code) : code).trim();
|
|
57
56
|
return crypto_1.default
|
|
58
57
|
.createHash("shake128", { outputLength: 24 })
|
|
59
58
|
.update(hashableCode)
|
|
@@ -66,26 +65,26 @@ function computeHash(code, shouldEmptyManualSections) {
|
|
|
66
65
|
*
|
|
67
66
|
* @param code Code to be locked.
|
|
68
67
|
* @param manualSectionsAllowed Whether generated code can contain manual sections.
|
|
68
|
+
* @param customContent A custom comment to insert into the docblock.
|
|
69
69
|
* @returns Locked code, i.e. code with prepended codelock file docblock.
|
|
70
70
|
*/
|
|
71
|
-
function lockCode(code, manualSectionsAllowed) {
|
|
71
|
+
function lockCode(code, manualSectionsAllowed, customContent = "") {
|
|
72
72
|
const hash = computeHash(code, manualSectionsAllowed);
|
|
73
73
|
let docblockContent;
|
|
74
74
|
if (manualSectionsAllowed) {
|
|
75
75
|
docblockContent = `This file is generated with manually editable sections. Only make
|
|
76
76
|
modifications between BEGIN MANUAL SECTION and END MANUAL SECTION
|
|
77
77
|
designators.
|
|
78
|
-
|
|
78
|
+
${customContent}
|
|
79
79
|
@generated-editable Codelock<<${hash}>>`;
|
|
80
80
|
}
|
|
81
81
|
else {
|
|
82
82
|
docblockContent = `This file is generated. Do not modify it manually.
|
|
83
|
-
|
|
83
|
+
${customContent}
|
|
84
84
|
@generated Codelock<<${hash}>>`;
|
|
85
85
|
}
|
|
86
|
-
return docblock_1.prependFileDocblock(code, docblockContent);
|
|
86
|
+
return (0, docblock_1.prependFileDocblock)(code, docblockContent);
|
|
87
87
|
}
|
|
88
|
-
exports.lockCode = lockCode;
|
|
89
88
|
/**
|
|
90
89
|
* Verify that the codelock in the source file is valid.
|
|
91
90
|
*
|
|
@@ -98,6 +97,5 @@ function verifyLock(lockedCode) {
|
|
|
98
97
|
return false;
|
|
99
98
|
}
|
|
100
99
|
return (codeblockInfo.hash ===
|
|
101
|
-
computeHash(docblock_1.removeFileDocblock(lockedCode), codeblockInfo.manualSectionsAllowed));
|
|
100
|
+
computeHash((0, docblock_1.removeFileDocblock)(lockedCode), codeblockInfo.manualSectionsAllowed));
|
|
102
101
|
}
|
|
103
|
-
exports.verifyLock = verifyLock;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
8
12
|
}));
|
|
9
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
17
|
__exportStar(require("./CodeFile"), exports);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getFileDocblock = getFileDocblock;
|
|
4
|
+
exports.removeFileDocblock = removeFileDocblock;
|
|
5
|
+
exports.createDocblock = createDocblock;
|
|
6
|
+
exports.prependFileDocblock = prependFileDocblock;
|
|
4
7
|
const docblockMatchRegExp = /^\/\*\*\n(?<contents>( \*.*\n)*?) \*\/\n/;
|
|
5
8
|
/**
|
|
6
9
|
* Get file docblock from `code`, the contents of a source file.
|
|
@@ -26,7 +29,6 @@ function getFileDocblock(code) {
|
|
|
26
29
|
// Remove trailing \n
|
|
27
30
|
.trim());
|
|
28
31
|
}
|
|
29
|
-
exports.getFileDocblock = getFileDocblock;
|
|
30
32
|
/**
|
|
31
33
|
* Removes the file docblock from the start of a file.
|
|
32
34
|
*
|
|
@@ -41,7 +43,6 @@ function removeFileDocblock(code) {
|
|
|
41
43
|
}
|
|
42
44
|
return code.replace(docblockMatchRegExp, "");
|
|
43
45
|
}
|
|
44
|
-
exports.removeFileDocblock = removeFileDocblock;
|
|
45
46
|
/**
|
|
46
47
|
* Creates a docblock from `docblockContent`.
|
|
47
48
|
*
|
|
@@ -55,7 +56,6 @@ function createDocblock(docblockContent) {
|
|
|
55
56
|
const docblock = `/**\n${docblockContentWithLeadingStars}\n */`;
|
|
56
57
|
return docblock;
|
|
57
58
|
}
|
|
58
|
-
exports.createDocblock = createDocblock;
|
|
59
59
|
/**
|
|
60
60
|
* Prepends `code` with `docblockContent`. Assumes `code` does not already have
|
|
61
61
|
* a file docblock.
|
|
@@ -66,4 +66,3 @@ exports.createDocblock = createDocblock;
|
|
|
66
66
|
function prependFileDocblock(code, docblockContent) {
|
|
67
67
|
return `${createDocblock(docblockContent)}\n\n${code.trimStart()}`;
|
|
68
68
|
}
|
|
69
|
-
exports.prependFileDocblock = prependFileDocblock;
|
package/dist/sections/manual.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createManualSection = createManualSection;
|
|
4
|
+
exports.extractManualSections = extractManualSections;
|
|
5
|
+
exports.emptyManualSections = emptyManualSections;
|
|
4
6
|
const sectionMatchRegExp = /\/\* BEGIN MANUAL SECTION (?<key>\S+) \*\/(?<code>(.|\n)+?|)\/\* END MANUAL SECTION \*\//gm;
|
|
5
7
|
function createManualSection(sectionKey, sectionCode) {
|
|
6
8
|
// Ensure section key is non-empty and contains no whitespaces
|
|
@@ -12,7 +14,6 @@ function createManualSection(sectionKey, sectionCode) {
|
|
|
12
14
|
processedSectionCode.length > 0 ? `${processedSectionCode}\n` : "";
|
|
13
15
|
return `/* BEGIN MANUAL SECTION ${sectionKey} */\n${processedSectionCode}/* END MANUAL SECTION */`;
|
|
14
16
|
}
|
|
15
|
-
exports.createManualSection = createManualSection;
|
|
16
17
|
function extractManualSections(code) {
|
|
17
18
|
const allMatches = code.matchAll(sectionMatchRegExp);
|
|
18
19
|
const manualSections = {};
|
|
@@ -24,7 +25,6 @@ function extractManualSections(code) {
|
|
|
24
25
|
});
|
|
25
26
|
return manualSections;
|
|
26
27
|
}
|
|
27
|
-
exports.extractManualSections = extractManualSections;
|
|
28
28
|
/**
|
|
29
29
|
* Removes all code between manual section designators.
|
|
30
30
|
*
|
|
@@ -33,4 +33,3 @@ exports.extractManualSections = extractManualSections;
|
|
|
33
33
|
function emptyManualSections(code) {
|
|
34
34
|
return code.replace(sectionMatchRegExp, (matchedString, sectionKey) => createManualSection(sectionKey, ""));
|
|
35
35
|
}
|
|
36
|
-
exports.emptyManualSections = emptyManualSections;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { defineConfig } from "eslint/config";
|
|
2
|
+
import typescriptEslint from "@typescript-eslint/eslint-plugin";
|
|
3
|
+
import jest from "eslint-plugin-jest";
|
|
4
|
+
import prettier from "eslint-plugin-prettier";
|
|
5
|
+
import globals from "globals";
|
|
6
|
+
import tsParser from "@typescript-eslint/parser";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import js from "@eslint/js";
|
|
10
|
+
import { FlatCompat } from "@eslint/eslintrc";
|
|
11
|
+
|
|
12
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
13
|
+
const __dirname = path.dirname(__filename);
|
|
14
|
+
const compat = new FlatCompat({
|
|
15
|
+
baseDirectory: __dirname,
|
|
16
|
+
recommendedConfig: js.configs.recommended,
|
|
17
|
+
allConfig: js.configs.all,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export default defineConfig([
|
|
21
|
+
{
|
|
22
|
+
extends: compat.extends(
|
|
23
|
+
"eslint:recommended",
|
|
24
|
+
"plugin:@typescript-eslint/recommended",
|
|
25
|
+
"plugin:jest/recommended",
|
|
26
|
+
"plugin:jest/style",
|
|
27
|
+
"plugin:prettier/recommended",
|
|
28
|
+
),
|
|
29
|
+
|
|
30
|
+
plugins: {
|
|
31
|
+
"@typescript-eslint": typescriptEslint,
|
|
32
|
+
jest,
|
|
33
|
+
prettier,
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
languageOptions: {
|
|
37
|
+
globals: {
|
|
38
|
+
...globals.node,
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
parser: tsParser,
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
rules: {
|
|
45
|
+
"prettier/prettier": "error",
|
|
46
|
+
"jest/valid-title": ["error", { ignoreTypeOfDescribeName: true }],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elg/tscodegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "TypeScript string-based code generation tool, with hashed files and manually editable sections",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,32 +14,39 @@
|
|
|
14
14
|
"clean": "rm -rf dist",
|
|
15
15
|
"build": "tsc --project tsconfig.dist.json",
|
|
16
16
|
"typecheck": "tsc --noEmit",
|
|
17
|
-
"test": "jest",
|
|
17
|
+
"test": "jest --coverage",
|
|
18
18
|
"lint": "yarn lint:code && yarn lint:misc",
|
|
19
|
-
"lint:code": "eslint
|
|
20
|
-
"lint:code:fix": "
|
|
21
|
-
"lint:misc
|
|
22
|
-
"lint:misc": "yarn lint:misc
|
|
23
|
-
"lint:misc:fix": "yarn lint:misc:base --write",
|
|
19
|
+
"lint:code": "eslint src ./*.js ./*.mjs",
|
|
20
|
+
"lint:code:fix": "yarn lint:code --fix",
|
|
21
|
+
"lint:misc": "prettier --check './*.{js,mjs,json,md}'",
|
|
22
|
+
"lint:misc:fix": "yarn lint:misc --write",
|
|
24
23
|
"prepare": "yarn clean && yarn build"
|
|
25
24
|
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@prettier/sync": "^0.6.1"
|
|
27
|
+
},
|
|
26
28
|
"peerDependencies": {
|
|
27
|
-
"prettier": "
|
|
29
|
+
"prettier": "3.x"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"eslint": "^
|
|
35
|
-
"eslint
|
|
36
|
-
"
|
|
37
|
-
"eslint
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"prettier": "^
|
|
41
|
-
"
|
|
42
|
-
"
|
|
32
|
+
"@eslint/eslintrc": "^3.3.4",
|
|
33
|
+
"@eslint/js": "^10.0.1",
|
|
34
|
+
"@types/jest": "^30.0.0",
|
|
35
|
+
"@types/mock-fs": "^4.13.4",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
37
|
+
"@typescript-eslint/parser": "^8.56.1",
|
|
38
|
+
"codecov": "^3.8.3",
|
|
39
|
+
"eslint": "^10.0.2",
|
|
40
|
+
"eslint-config-prettier": "^10.1.8",
|
|
41
|
+
"eslint-plugin-jest": "^29.15.0",
|
|
42
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
43
|
+
"globals": "^17.4.0",
|
|
44
|
+
"jest": "^30.2.0",
|
|
45
|
+
"jest-junit": "^16.0.0",
|
|
46
|
+
"mock-fs": "^5.5.0",
|
|
47
|
+
"prettier": "^3.8.1",
|
|
48
|
+
"ts-jest": "^29.4.6",
|
|
49
|
+
"typescript": "^5.9.3"
|
|
43
50
|
},
|
|
44
51
|
"keywords": [
|
|
45
52
|
"codegen",
|