Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
multiline
const EOL = require('os').EOL;
const reCommentContents = /\/\*([\s\S]*?)\*\//;
// Node wraps modules in a function so we can't use the native .isToplevel() method
const reTopLevel = /function \(exports, require/;
const multiline = () => {
const _ = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const stack = new Error().stack.slice(1);
Error.prepareStackTrace = _;
const frame = stack[0];
let fnString = arguments.callee.toString();
// let fnString = frame.getFunction().toString();
if (reTopLevel.test(fnString)) {
// When it's top level we need to remove everything
// before the function so we don't match the wrong comment
fnString = fnString.split(EOL)
.slice(frame.getLineNumber() - 1).join(EOL)
.slice(frame.getColumnNumber() - 1);
}
return fnString.match(reCommentContents)[1];
};
const SQL = multiline(/*
SELECT `name`, `privilege`
FROM `user`
*/);
const HTML = multiline(/*
<!doctype html>
<html>
<body>
<h1>Hello world!</h1>
</body>
</html>
*/);
console.log(SQL);
console.log(HTML);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.