Skip to content

Commit a0514f5

Browse files
authored
Merge pull request jenkins-x-plugins#3 from GoogleFeud/dev
Dev
2 parents 52f2da0 + 6186961 commit a0514f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3151
-557
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests/
2+
dist/
3+
test/

.eslintrc.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"@typescript-eslint"
1717
],
1818
"rules": {
19-
"@typescript-eslint/no-non-null-assertion": "off",
2019
"indent": [
2120
"error",
2221
4
@@ -33,5 +32,5 @@
3332
"error",
3433
"always"
3534
]
36-
},
35+
}
3736
}

.github/CONTRIBUTING.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
# Contributing
3+
4+
Thank you for contributing to ts-macros! Your help is appreciated by the author of this library and everyone using it!
5+
6+
## Table of Contents
7+
8+
- [How can I contribute?](#how-can-i-contribute)
9+
- [Bug Reports](#bug-reports)
10+
- [Feature Requests](#feature-requests)
11+
- [Pull Requests](#pull-requests)
12+
- [Setup](#setup)
13+
- [Testing](#testing)
14+
- [Finishing up](#finishing-up)
15+
16+
## How can I contribute?
17+
18+
### Bug Reports
19+
20+
Before reporting a bug, plese [search for issues with similar keywords to yours](https://github.com/GoogleFeud/ts-macros/issues?q=is%3Aissue+is%3Aopen). If an issue already exists for the bug then you can "bump" it by commenting on it. If it doesn't, then you can create one.
21+
22+
When writing a bug report:
23+
24+
- Use a clear and descriptive title for the issue.
25+
- Explain what you expected to see instead and why.
26+
27+
### Feature Requests
28+
29+
Suggestions are always welcome! Before writing a feature request, please [search for issues with similar keywords to yours](https://github.com/GoogleFeud/ts-macros/issues?q=is%3Aissue+is%3Aopen). If an issue already exists for the request then you can "bump" it by commenting on it. If it doesn't, then you can create one.
30+
31+
When writing a feature request:
32+
33+
- Use a clear and descriptive title for the issue.
34+
- Provide examples of how the feature will be useful.
35+
36+
### Pull Requests
37+
38+
Want to go straight into writing code? To get some inspiration you can look through the issues with the `bug` tag and find one you think you can tackle. If you are implementing a feature, please make sure an issue already exists for it before directly making a PR. If it doesn't, feel free to create one!
39+
40+
All future changes are made in the `dev` branch, so make sure to work in that branch!
41+
42+
#### Setup
43+
44+
- Fork this repository
45+
- Clone your fork
46+
- Install all dependencies: `npm i`
47+
- Build the project: `npm run build`
48+
- Run the tests to see if everything is running smoothly: `npm test`
49+
50+
#### Testing
51+
52+
ts-macros has integrated and snapshot testing implemented. To make sure any changes you've made have not changed the transformer for worse, run `npm test`. This will first run all integrated tests, which test the **transpiled code**, and then ask you to continue with the snapshot testing.
53+
54+
During snapshot testing, ts-macros compares the **trusted** transpiled integrated tests with the ones on your machine that have just been transpiled in the previous step. If any changes have been detected, it will ask you if you approve of these changes. If you notice some of the generated code is wrong or not up to standards, disprove the changes, make your fixes and run `npm test` again until the latest transpiled code matches the trusted version, or until you're satisfied with the generated code.
55+
56+
#### Finishing up
57+
58+
Once you're done working on an issue, you can submit a pull request to have your changes merged! Before submitting the request, make sure there are no linting errors (`npm lint`), all tests pass (`npm test`), and your branch is up to date (`git pull`).

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
test/

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
test/
3+
tests/
34
.github/
45
src/
56
docs/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ts-macros
22

3-
ts-docs is a custom typescript **transformer** which implements function macros. This library is heavily inspired by Rust's `macro_types!` macro.
3+
ts-docs is a custom typescript **transformer** which implements function macros. This library is heavily inspired by Rust's `macro_rules!` macro.
44

55
## Basic usage
66

docs/In-Depth/builtins.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,109 @@ $$send!(123, {});
190190
```
191191
``` --Result
192192
Error: In macro $$send: Expected string literal, found something else.
193+
```
194+
195+
## $$includes
196+
197+
Checks if `val` is included in the array literal.
198+
199+
```ts --Call
200+
$$includes!([1, 2, 3], 2);
201+
$$includes!([1, 2, 3], 4);
202+
```
203+
```ts --Result
204+
true;
205+
false;
206+
```
207+
208+
## $$ts
209+
210+
Turns the provided string into code. You should use this only when you can't accomplish something with other macros.
211+
212+
```ts --Macro
213+
type ClassInfo = { name: string, value: string };
214+
215+
export function $makeClasses(...info: Array<ClassInfo>) {
216+
+[(info: ClassInfo) => {
217+
$$ts!(`
218+
class ${info.name} {
219+
constructor() {
220+
this.value = ${info.value}
221+
}
222+
}
223+
`);
224+
}];
225+
}
226+
```
227+
```ts --Call
228+
$makeClasses!({name: "ClassA", value: "1"}, {name: "ClassB", value: "2"})
229+
```
230+
```ts --Result
231+
class ClassA {
232+
constructor() {
233+
this.value = 1;
234+
}
235+
}
236+
class ClassB {
237+
constructor() {
238+
this.value = 2;
239+
}
240+
}
241+
```
242+
243+
## $$escape
244+
245+
"Escapes" the code inside the arrow function by placing it in the parent block. This macro **cannot** be used outside any blocks.
246+
247+
```ts --Macro
248+
function $try(resultObj: Save<{ value?: number, is_err: () => boolean}>) {
249+
$$escape!(() => {
250+
if (resultObj.is_err()) {
251+
return resultObj;
252+
}
253+
});
254+
return resultObj.value;
255+
}
256+
```
257+
```ts --Call
258+
(() => {
259+
const a = $try!({ value: 123, is_err: () => false });
260+
$try!({is_err: () => true });
261+
});
262+
```
263+
```ts --Result
264+
(() => {
265+
let resultObj_1 = { value: 123, is_err: () => false };
266+
if (resultObj_1.is_err()) {
267+
return resultObj_1;
268+
}
269+
const a = resultObj_1.value;
270+
let resultObj_2 = { is_err: () => true };
271+
if (resultObj_2.is_err()) {
272+
return resultObj_2;
273+
}
274+
return resultObj_2.value;
275+
})();
276+
```
277+
278+
## $$propsOfType
279+
280+
Expands to an array with all the properties of a type.
281+
282+
```ts --Call
283+
console.log($$propsOfType!<{a: string, b: number}>());
284+
```
285+
```ts --Result
286+
console.log(["a", "b"]);
287+
```
288+
289+
## $$typeToString
290+
291+
Turns a type to a string literal.
292+
293+
```ts --Call
294+
295+
```
296+
```ts --Result
297+
193298
```

docs/In-Depth/expanding.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ order: 2
55

66
# Expanding macros
77

8-
Every macro **expands** into the code that it contains. How it'll expand depends entirely on how the macro is used. This page covers all the ways a macro can be expanded. Javascript has 3 main constructs: `Expression`, `ExpressionStatement` and `Statement`. Since macro calls are plain function calls, macros can never be used as a statement, but...
8+
Every macro **expands** into the code that it contains. How it'll expand depends entirely on how the macro is used. Javascript has 3 main constructs: `Expression`, `ExpressionStatement` and `Statement`. Since macro calls are plain function calls, macros can never be used as a statement, but...
99

1010
|> Expanded macros are **always** hygienic!
1111

@@ -47,7 +47,7 @@ If the macro expands to a single expression, then the macro call is directly rep
4747

4848
```ts --Macro
4949
function $push(array: Array<number>, ...nums: Array<number>) : number {
50-
return +[",", (nums: number) => array.push(nums)];
50+
return +["()", (nums: number) => array.push(nums)];
5151
}
5252
```
5353
```ts --Call
@@ -63,7 +63,7 @@ const newSize = (arr.push(1), arr.push(2), arr.push(3));
6363

6464
### Multiple expressions
6565

66-
If the macro expands to multiple expressions, or has a statement inside it's body, then the body is wrapped inside an IIFE (Immediately Invoked function expression) and the last expression gets returned.
66+
If the macro expands to multiple expressions, or has a statement inside it's body, then the body is wrapped inside an IIFE (Immediately Invoked function expression) and the last expression gets returned automatically.
6767

6868
```ts --Macro
6969
function $push(array: Array<number>, ...nums: Array<number>) : number {
@@ -81,4 +81,63 @@ const newSize = (() => {
8181
arr.push(2)
8282
return arr.push(3);
8383
})();
84+
```
85+
86+
#### Escaping the IIFE
87+
88+
If you want part of the code to be ran **outside** of the IIFE (for example you want to `return`, or `yield`, etc.) you can use the [[$$escape]] built-in macro. For example, here's a fully working macro which expands to a completely normal if statement, but it can be used as an expression:
89+
90+
```ts --Macro
91+
function $if<T>(comparison: any, then: () => T, _else?: () => T) {
92+
$$escape!(() => {
93+
var val;
94+
if (_else) {
95+
if (comparison) {
96+
val = $$inlineFunc!(then);
97+
} else {
98+
val = $$inlineFunc!(_else);
99+
}
100+
} else {
101+
if (comparison) {
102+
val = $$inlineFunc!(then);
103+
}
104+
}
105+
});
106+
return $$ident!("val");
107+
}
108+
```
109+
```ts --Call
110+
(() => {
111+
const variable: number = 54;
112+
console.log($if!<string>(1 === variable, () => {
113+
// Do something...
114+
console.log("1 === variable");
115+
return "A";
116+
}, () => {
117+
// Do something...
118+
console.log("1 !== variable");
119+
return "B";
120+
}));
121+
})();
122+
```
123+
```ts --Result
124+
(() => {
125+
const variable = 54;
126+
var val;
127+
{
128+
if (1 === variable) {
129+
val = (() => {
130+
console.log("1 === variable");
131+
return "A";
132+
})();
133+
}
134+
else {
135+
val = (() => {
136+
console.log("1 !== variable");
137+
return "B";
138+
})();
139+
}
140+
}
141+
console.log(val);
142+
})();
84143
```

docs/In-Depth/literals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $add!(5, 10);
1919
15
2020
```
2121

22-
This works for most binary operators: `+`, `-`, `/`, `*`, `&&`, `||`, `==`, `===`.
22+
This works for all binary and unary operators.
2323

2424
## Logic
2525

docs/In-Depth/markers.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,6 @@ order: 6
77

88
`Markers` make macro parameters behave differently. They don't alter the parameter's type, but it's behaviour.
99

10-
## AsRest
11-
12-
You can mark a parameter with the AsRest marker. The parameter will act exactly like a rest parameter, but instead of separating all values of the parameter with a comma, you put them all in an array. This way you can have multiple repetition arguments.
13-
14-
```ts --Macro
15-
import { AsRest } from "ts-macros"
16-
17-
// This wouldn't work if the type was just Array<number>
18-
function $random(nums: AsRest<Array<number>>) : Array<number> {
19-
+["[]", () => nums * Math.random() << 0]
20-
}
21-
```
22-
```ts --Call
23-
$random!([1, 2, 3]);
24-
```
25-
```ts --Result
26-
[1 * Math.random() << 0, 2 * Math.random() << 0, 3 * Math.random() << 0]
27-
```
28-
2910
## Accumulator
3011

3112
A parameter which increments every time the macro is called. You can only have one accumulator parameter per macro.

docs/In-Depth/overview.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ order: 1
55

66
# Overview
77

8-
ts-docs is a custom typescript **transformer** which implements function macros. This library is heavily inspired by Rust's `macro_types!` macro. Since it's a custom transformer, it can be plugged in into any tool which uses the `typescript` npm package.
8+
ts-docs is a custom typescript **transformer** which implements function macros. This library is heavily inspired by Rust's `macro_rules!` macro. Since it's a custom transformer, it can be plugged in into any tool which uses the `typescript` npm package.
99

1010

1111
## Basic usage
@@ -18,12 +18,10 @@ function $contains(value: unknown, ...possible: Array<unknown>) {
1818
}
1919
```
2020
```ts --Call
21-
const searchItem = "google";
2221
console.log($contains!(searchItem, "erwin", "tj"));
2322
```
2423
```ts --Result
25-
const searchItem = "google";
26-
console.log(false);
24+
console.log(searchItem === "erwin" || searchItem === "tj");
2725
```
2826

2927
Macros can also be **chained** with any javascript expression.
@@ -72,4 +70,8 @@ options: {
7270
before: [TsMacros(program)]
7371
}
7472
}
75-
```
73+
```
74+
75+
## Contributing
76+
77+
`ts-macros` is being maintained by a single person. Contributions are welcome and appreciated. Feel free to open an issue or create a pull request at https://github.com/GoogleFeud/ts-macros.

docs/In-Depth/parameters.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ for (let i = 0; i < [1, 2, 3, 4, 5].length; i++) {
2323
}
2424
```
2525

26-
## The fix
27-
2826
To avoid this, you can assign the literal to a variable.
2927

3028
```ts --Macro

0 commit comments

Comments
 (0)