You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
2 years ago
|
"use strict";
|
||
|
|
||
|
const splitFilter = require("split-filter");
|
||
|
const assert = require("assert");
|
||
|
|
||
|
const NoChange = require("../astformer/actions/no-change");
|
||
|
const nixTypes = require("./_nix-types");
|
||
|
|
||
|
module.exports = {
|
||
|
name: "desugar-inherits",
|
||
|
visitors: {
|
||
|
NixAttributeSet: (node) => {
|
||
|
let [ inherits, regularBindings ] = splitFilter(node.bind, (binding) => binding.type === "NixInherit");
|
||
|
|
||
|
if (inherits.length === 0) {
|
||
|
return NoChange;
|
||
|
} else {
|
||
|
let tempCounter = 0;
|
||
|
let inherits_ = inherits.map((inherit) => {
|
||
|
return {
|
||
|
tempName: `$$jsNix$temp$${tempCounter++}`,
|
||
|
sourceExpression: inherit.expression,
|
||
|
names: inherit.attrs.attr.map((attribute) => {
|
||
|
assert(attribute.type === "NixAttributeIdentifier");
|
||
|
return attribute.name;
|
||
|
})
|
||
|
};
|
||
|
});
|
||
|
|
||
|
let letBindings = inherits_.map((inherit) => {
|
||
|
return nixTypes.NixBinding(
|
||
|
[ nixTypes.NixAttributeIdentifier(inherit.tempName) ],
|
||
|
inherit.sourceExpression
|
||
|
);
|
||
|
});
|
||
|
|
||
|
let body = {
|
||
|
... node,
|
||
|
bind: [
|
||
|
... regularBindings,
|
||
|
... inherits_.flatMap((inherit) => {
|
||
|
return inherit.names.map((name) => {
|
||
|
return nixTypes.NixBinding(
|
||
|
[ nixTypes.NixAttributeIdentifier(name) ],
|
||
|
nixTypes.NixAttributeSelection(
|
||
|
nixTypes.NixIdentifier(inherit.tempName),
|
||
|
[ nixTypes.NixAttributeIdentifier(name) ]
|
||
|
)
|
||
|
);
|
||
|
});
|
||
|
})
|
||
|
]
|
||
|
};
|
||
|
|
||
|
return nixTypes.NixLetIn(
|
||
|
letBindings,
|
||
|
body
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|