-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast.macro.js
More file actions
97 lines (86 loc) · 2.75 KB
/
Copy pathcast.macro.js
File metadata and controls
97 lines (86 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { createMacro } = require('babel-plugin-macros');
module.exports = createMacro(cast);
/**
* @example
* ```js
* import { table } from 'data-manager';
* import cast, { derived } from 'data-manager/cast.macro';
*
* const today = new Date();
* const population = table('data/population.csv', cast({
* year: year => new Date(Number(year), 0, 1),
* state: String,
* population: Number,
* age: derived(row => today - new Date(Number(row.year), 0, 1))
* }))
*
* // transforms at build-time into:
*
* const population = table('data/population.csv', (() => {
* const mapping = {
* year: year => new Date(Number(year), 0, 1),
* state: String,
* population: Number,
* age: derived(row => today - new Date(Number(row.year), 0, 1))
* };
*
* return (row, index, rows) => {
* return {
* year: mapping.year(row.year),
* state: mapping.state(row.state),
* population: mapping.population(row.population),
* age: mapping.age(row, index, rows)
* };
* }
* })()
* ```
* @param {object} mapping by field name
* @returns {function}
*/
function cast({ references, state, babel: { template, types: t } }) {
const paths = references.default;
const derived_nodes = (references.derived || []).map(path => path.parent);
if (!paths || !paths.length) return;
const buildCast = template(`(() => {
const mapping = MAPPING;
return (row, index, rows) => {
return CAST;
};
})()`);
for (const identifier_path of paths) {
const section_path = identifier_path.parentPath;
const mapping = section_path.get('arguments.0');
const properties = mapping.node.properties;
// Create cast object
//
// key: mapping.key(row.key)
// or for `derived`:
// key: mapping.key(row, index, rows)
const cast_properties = properties.map((property, index) => {
const key = property.key;
const computed = !t.isIdentifier(key);
const derived = derived_nodes.includes(property.value);
const value = t.callExpression(
t.memberExpression(t.identifier('mapping'), key, computed),
derived
? [t.identifier('row'), t.identifier('index'), t.identifier('rows')]
: [t.memberExpression(t.identifier('row'), key, computed)]
);
return t.objectProperty(key, value);
});
const CAST = t.objectExpression(cast_properties);
// Removed `derived` from mapping
properties.forEach((property, index) => {
if (derived_nodes.includes(property.value)) {
const path = mapping.get(`properties.${index}.value`);
path.replaceWith(property.value.arguments[0]);
}
});
const MAPPING = mapping.node;
const cast = buildCast({
MAPPING,
CAST
});
section_path.replaceWith(cast);
}
}