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.

28 lines
892 B
JavaScript

2 years ago
"use strict";
const types = require("@babel/types");
2 years ago
const splitFilter = require("split-filter");
2 years ago
module.exports = {
name: "literals",
visitors: {
NixIntegerLiteral: (node) => types.numericLiteral(parseInt(node.value)),
NixFloatLiteral: (node) => types.numericLiteral(parseFloat(node.value)),
2 years ago
NixStringLiteral: (node, { defer }) => {
if (node.parts.length === 1 && node.parts[0].type === "NixInterpolationLiteral") {
// Fast case; just a simple, non-interpolated string literal
return types.stringLiteral(node.parts[0].value);
} else {
2 years ago
return defer((node) => {
let [ literals, expressions ] = splitFilter(node.parts, (part) => part.type === "NixInterpolationLiteral");
return types.templateLiteral(
literals.map((node) => types.templateElement({ raw: node.value })),
expressions.map((node) => node.expression)
);
});
}
}
2 years ago
}
};