Skip to content

Commit fb814aa

Browse files
committed
docs: add example and organize files a bit
1 parent a71ee5b commit fb814aa

File tree

9 files changed

+88
-44
lines changed

9 files changed

+88
-44
lines changed

README.md

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<!--@[h1([pkg.name]), paragraph([pkg.description])]-->
2+
23
# typescript-transform-macros
34

45
Typescript Transform Macros
6+
57
<!--/@-->
68

79
<!--@shields("npm", "prettier", "ConventionalCommits", "spacemacs")-->
10+
811
[![npm version](https://img.shields.io/npm/v/typescript-transform-macros.svg)](https://www.npmjs.com/package/typescript-transform-macros) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![Built with Spacemacs](https://raw.githubusercontent.com/syl20bnr/spacemacs/master/assets/spacemacs-badge.svg?sanitize=true)](http://spacemacs.org)
12+
913
<!--/@-->
1014

1115
Examples from <https://github.com/codemix/babel-plugin-macros>.
1216

1317
<!--@installation()-->
18+
1419
## Installation
1520

1621
```sh
1722
npm install --save-dev typescript-transform-macros
1823
```
24+
1925
<!--/@-->
2026

2127
## Usage with [ttypescript](https://github.com/cevek/ttypescript/)
@@ -40,25 +46,11 @@ declare function MACRO<T>(t: T): T;
4046

4147
_Input:_
4248

43-
<!--@snippet("./src/__fixtures/input.ts")-->
49+
<!--@snippet("./examples/ttypescript/index.ts")-->
50+
4451
```ts
4552
declare function MACRO<T>(t: T): T;
4653

47-
const FILTER = MACRO(
48-
<T>(
49-
inputConst: T[],
50-
visitor: (value: T, index?: number, input?: T[]) => boolean
51-
) => {
52-
const input = inputConst;
53-
const length = input.length;
54-
const result = [];
55-
for (let i = 0; i < length; i++) {
56-
if (visitor(input[i], i, input)) result.push(input[i]);
57-
}
58-
return result;
59-
}
60-
);
61-
6254
const MAP = MACRO(
6355
<T, L>(
6456
inputConst: T[],
@@ -74,34 +66,25 @@ const MAP = MACRO(
7466
}
7567
);
7668

77-
function demo() {
78-
return FILTER(MAP([1, 2, 3, 4], item => item + 1), v => v % 2 === 0);
79-
}
69+
console.log(MAP([1, 2, 3], n => 3 * n + 1));
8070
```
71+
8172
<!--/@-->
8273

8374
_Output:_
8475

85-
<!--@snippet("./src/__fixtures/expected.js")-->
76+
<!--@example("./examples/ttypescript/index.js")-->
77+
8678
```js
87-
function demo() {
88-
const __input4 = [1, 2, 3, 4];
89-
const __length4 = __input4.length;
90-
const __result4 = new Array(__length4);
91-
for (let __i4 = 0; __i4 < __length4; __i4++) {
92-
__result4[__i4] =
93-
__input4[__i4]
94-
+ 1;
95-
}
96-
const __input2 = __result4;
97-
const __length2 = __input2.length;
98-
const __result2 = [];
99-
for (let __i2 = 0; __i2 < __length2; __i2++) {
100-
if (__input2[__i2]
101-
% 2 === 0)
102-
__result2.push(__input2[__i2]);
103-
}
104-
return __result2;
79+
"use strict";
80+
const __input2 = [1, 2, 3];
81+
const __length2 = __input2.length;
82+
const __result2 = new Array(__length2);
83+
for (let __i2 = 0; __i2 < __length2; __i2++) {
84+
__result2[__i2] = 3 * __input2[__i2] + 1;
10585
}
86+
console.log(__result2);
87+
//> [ 4, 7, 10 ]
10688
```
89+
10790
<!--/@-->

examples/ttypescript/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

examples/ttypescript/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
const __input2 = [1, 2, 3];
3+
const __length2 = __input2.length;
4+
const __result2 = new Array(__length2);
5+
for (let __i2 = 0; __i2 < __length2; __i2++) {
6+
__result2[__i2] =
7+
3 *
8+
__input2[__i2] + 1;
9+
}
10+
console.log(__result2);

examples/ttypescript/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare function MACRO<T>(t: T): T;
2+
3+
const MAP = MACRO(
4+
<T, L>(
5+
inputConst: T[],
6+
visitor: (value: T, index?: number, input?: T[]) => L
7+
) => {
8+
const input = inputConst;
9+
const length = input.length;
10+
const result = new Array(length) as L[];
11+
for (let i = 0; i < length; i++) {
12+
result[i] = visitor(input[i], i, input);
13+
}
14+
return result;
15+
}
16+
);
17+
18+
console.log(MAP([1, 2, 3], n => 3 * n + 1));

examples/ttypescript/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "example-ttyepescript",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "ttsc",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"ttypescript": "^1.5.6",
15+
"typescript": "^3.3.3333",
16+
"typescript-transform-macros": "file:../.."
17+
}
18+
}

examples/ttypescript/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "commonjs",
5+
"strict": true,
6+
"plugins": [{ "transform": "typescript-transform-macros" }],
7+
"esModuleInterop": true
8+
}
9+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function demo() {
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`transformer should compile 1`] = `
4+
"function demo() {
25
const __input4 = [1, 2, 3, 4];
36
const __length4 = __input4.length;
47
const __result4 = new Array(__length4);
@@ -17,3 +20,5 @@ function demo() {
1720
}
1821
return __result2;
1922
}
23+
"
24+
`;

src/index.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import * as ts from "typescript";
2-
import { readFileSync } from "fs";
32
import { resolve } from "path";
43
import transformer from "./";
54

65
describe("transformer", () => {
76
it("should compile", () => {
87
const inputFile = resolve(__dirname, "__fixtures/input.ts");
9-
const expectedFile = resolve(__dirname, "__fixtures/expected.js");
108
const result = compile(inputFile);
11-
const expected = readFileSync(expectedFile).toString();
12-
expect(result).toBe(expected);
9+
expect(result).toMatchSnapshot();
1310
});
1411
});
1512

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Transformer {
1313
const postExtract = ts.visitNode(node, this.extractMacros);
1414
return ts.visitNode(postExtract, this.resolveMacros);
1515
}
16+
// Removes macro definition from code and save them for later
1617
private extractMacros = (node: ts.Node): ts.Node | undefined => {
1718
if (ts.isVariableStatement(node)) {
1819
const firstDeclaration = node.declarationList.declarations[0]; // TODO maybe check for more
@@ -35,6 +36,7 @@ class Transformer {
3536
}
3637
return ts.visitEachChild(node, this.extractMacros, this.context);
3738
};
39+
// Search for macros calls and replace them with the macros
3840
private resolveMacros = (node: ts.Node): ts.Node | undefined => {
3941
if (ts.isBlock(node) || ts.isSourceFile(node)) {
4042
const newBlock = this.replaceMacros(node, this.rootMacros);
@@ -54,6 +56,7 @@ class Transformer {
5456
}
5557
return ts.visitEachChild(node, this.resolveMacros, this.context);
5658
};
59+
// Prefix macros variables to avoid name collision, returns "return" expression
5760
private fixMacros = (
5861
node: ts.Block
5962
): [ts.Expression | undefined, ts.Block] => {
@@ -84,12 +87,12 @@ class Transformer {
8487
const resultNode = ts.visitNode(node, visit);
8588
return [result, resultNode];
8689
};
90+
// Actually replace the macros in the code
8791
private replaceMacros = (
8892
block: ts.BlockLike,
8993
macros: Macro[]
9094
): ts.Statement[] => {
9195
const visit = (child: ts.Node): ts.Node => {
92-
// TODO check if visit enought children
9396
if (ts.isBlock(child)) {
9497
return ts.createBlock(this.replaceMacros(child, macros));
9598
}

0 commit comments

Comments
 (0)