Skip to content

Instantly share code, notes, and snippets.

@jiangzhuo
Last active November 12, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiangzhuo/15b16ebeafdcff7b595641ab8dc7c345 to your computer and use it in GitHub Desktop.
Save jiangzhuo/15b16ebeafdcff7b595641ab8dc7c345 to your computer and use it in GitHub Desktop.
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