Last active
November 12, 2018 21:39
-
-
Save jiangzhuo/15b16ebeafdcff7b595641ab8dc7c345 to your computer and use it in GitHub Desktop.
multiline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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