Browse Source

Cleanup

master
Sven Slootweg 2 months ago
parent
commit
41cb6b101d
  1. 3
      src/transformers/attribute-sets.js
  2. 72
      src/transformers/functions.js
  3. 49
      src/transformers/index.js
  4. 7
      src/transformers/templates/call-lazy-wrapper.js
  5. 10
      src/transformers/templates/lazy-wrapper.js

3
src/transformers/attribute-sets.js

@ -3,12 +3,9 @@
const assert = require("assert");
const types = require("@babel/types");
const template = require("@babel/template").default;
const splitFilter = require("split-filter");
const unpackExpression = require("./util/unpack-expression");
const templateExpression = require("./util/template-expression");
const NoChange = require("../astformer/actions/no-change");
const printAst = require("../print-ast");
const lazyWrapper = require("./templates/lazy-wrapper");
const callLazyWrapper = require("./templates/call-lazy-wrapper");
const objectLiteral = require("./templates/object-literal");

72
src/transformers/functions.js

@ -4,17 +4,17 @@ const asExpression = require("as-expression");
const assert = require("assert");
const types = require("@babel/types");
const template = require("@babel/template").default;
const unpackExpression = require("./util/unpack-expression");
const lazyWrapper = require("./templates/lazy-wrapper");
const callLazyWrapper = require("./templates/call-lazy-wrapper");
const templateExpression = require("./util/template-expression");
let tmplFunctionDefinitionUniversal = template(`
let tmplFunctionDefinitionUniversal = templateExpression(`
((%%universal%%) => {
return %%body%%;
})
`);
let tmplFunctionDefinitionWithFormals = template(`(
let tmplFunctionDefinitionWithFormals = templateExpression(`(
(%%universal%%) => {
%%handleArguments%%;
@ -26,7 +26,7 @@ let tmplHandleArgument = template(`
const %%name%% = $handleArgument(%%nameString%%, %%universal%%, %%defaultArg%%);
`);
let tmplFunctionCall = template(`
let tmplFunctionCall = templateExpression(`
(%%function%%(%%arg%%))
`);
@ -66,44 +66,40 @@ module.exports = {
setContext([ "universal", "formals" ], "identifierType", "define");
return defer((node) => {
return unpackExpression(
functionDefinition({
universal: asExpression(() => {
if (node.universal != null) {
assert(node.universal.type === "Identifier");
return node.universal.name;
}
}),
formals: asExpression(() => {
if (node.formals != null) {
assert(node.formals.type === "NixUnpackedAttributes");
return node.formals.formal.map((formal) => {
assert(formal.type === "NixUnpackedAttribute");
assert(formal.name.type === "Identifier");
return {
name: formal.name.name,
// NOTE: This unwrap-and-rewrap dance is necessary to make out-of-order references work, like in eval-okay-scope-4.nix
default: (formal.default != null)
? lazyWrapper(callLazyWrapper(formal.default))
: undefined
};
});
}
}),
body: node.body
})
);
return functionDefinition({
universal: asExpression(() => {
if (node.universal != null) {
assert(node.universal.type === "Identifier");
return node.universal.name;
}
}),
formals: asExpression(() => {
if (node.formals != null) {
assert(node.formals.type === "NixUnpackedAttributes");
return node.formals.formal.map((formal) => {
assert(formal.type === "NixUnpackedAttribute");
assert(formal.name.type === "Identifier");
return {
name: formal.name.name,
// NOTE: This unwrap-and-rewrap dance is necessary to make out-of-order references work, like in eval-okay-scope-4.nix
default: (formal.default != null)
? lazyWrapper(callLazyWrapper(formal.default))
: undefined
};
});
}
}),
body: node.body
});
});
},
NixFunctionCall: (_node, { defer }) => {
return defer((node) => {
return unpackExpression(
tmplFunctionCall({
function: node.function,
arg: lazyWrapper(node.argument)
})
);
return tmplFunctionCall({
function: node.function,
arg: lazyWrapper(node.argument)
});
});
}
}

49
src/transformers/index.js

@ -3,17 +3,8 @@
const assert = require("assert");
const types = require("@babel/types");
const template = require("@babel/template").default;
const unreachable = require("@joepie91/unreachable")("jsnix");
const NoChange = require("../astformer/actions/no-change");
const ConsumeNode = require("../astformer/actions/consume-node");
const RemoveNode = require("../astformer/actions/remove-node");
const asExpression = require("as-expression");
const unpackExpression = require("./util/unpack-expression");
const printAst = require("../print-ast");
const templateExpression = require("./util/template-expression");
// FIXME: Auto-generate argument list based on exposed API surface?
let tmplModule = template(`
@ -22,44 +13,10 @@ let tmplModule = template(`
};
`);
let tmplObjectDynamic = template(`
(() => {
let $$nixJS_object = {};
%%entries%%
return $$nixJS_object;
})()
`);
let tmplObjectDynamicEntry = template(`
if ($$nixJS_object[%%key%%] !== undefined) { throw new Error(\`Duplicate key '\${%%key%%}' in attribute set\`); }
$$nixJS_object[%%key%%] = %%wrappedExpression%%;
`);
let tmplIdentifierReference = template(`(
let tmplIdentifierReference = templateExpression(`(
%%name%%()
)`);
function generateDynamicObject(bindings, recursive) {
let generatedBindings = bindings.map((binding) => {
let wrapper = (recursive)
? tmplLazyWrapperRecursive
: tmplLazyWrapper;
let key = isDynamicBinding(binding)
? binding.attrpath.attr[0]
: binding.attrpath.attr[0].name;
return tmplObjectDynamicEntry({
key: key,
wrappedExpression: wrapper({ expression: binding.expression })
});
});
return tmplObjectDynamic({ entries: generatedBindings.flat() })
}
let trivial = {
name: "trivial-transforms",
visitors: {
@ -78,7 +35,7 @@ let trivial = {
if (getContext("identifierType") === "define") {
return types.identifier(node.name);
} else { // reference
return unpackExpression(tmplIdentifierReference({ name: node.name }));
return tmplIdentifierReference({ name: node.name });
}
},
NixBinaryOperation: (_node, { defer }) => {

7
src/transformers/templates/call-lazy-wrapper.js

@ -1,12 +1,11 @@
"use strict";
const template = require("@babel/template").default;
const unpackExpression = require("../util/unpack-expression");
const templateExpression = require("../util/template-expression");
let tmplCallLazy = template(`(
let tmplCallLazy = templateExpression(`(
%%wrapper%%()
)`);
module.exports = function callLazyWrapper(wrapper) {
return unpackExpression(tmplCallLazy({ wrapper }));
return tmplCallLazy({ wrapper });
};

10
src/transformers/templates/lazy-wrapper.js

@ -1,15 +1,13 @@
"use strict";
const template = require("@babel/template").default;
const templateExpression = require("../util/template-expression");
const unpackExpression = require("../util/unpack-expression");
let tmplLazyWrapper = template(`(
let tmplLazyWrapper = templateExpression(`(
$memoize(() => %%expression%%)
)`);
module.exports = function lazyWrapper(expression) {
return unpackExpression(tmplLazyWrapper({
return tmplLazyWrapper({
expression: expression
}));
});
};

Loading…
Cancel
Save