Skip to content

Instantly share code, notes, and snippets.

@IhostVlad
Created October 24, 2017 10:10
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 IhostVlad/9310188edbdbc9f62dc3107417cc8fe4 to your computer and use it in GitHub Desktop.
Save IhostVlad/9310188edbdbc9f62dc3107417cc8fe4 to your computer and use it in GitHub Desktop.
Babel plugin adds meta-information to all objects in source code, including file name, line number and original text, where object had been declated
module.exports = function ({ types: t }) {
const WRAP_NAME = '__WRAP_OBJECT_CREATION_WITH_SOURCE_MAP__';
const SOURCE_NAME = '__SOURCE_DELCARATION__';
return {
visitor: {
'ObjectExpression|ArrayExpression|NewExpression': function (path, state) {
const wrappedParent = path.findParent(
parent =>
parent.node &&
parent.node.callee &&
parent.node.callee.id &&
parent.node.callee.id.name === WRAP_NAME
);
if (wrappedParent) {
const skip =
wrappedParent.node.arguments.indexOf(path.node) > -1 ||
wrappedParent.node.callee.body.body[0].block.body[0].expression
.arguments[2] === path.node;
if (skip) return;
}
const sourceInfo = {
sourceCode: 'Source code is not available',
filename: 'Source file name not available',
startLine: NaN,
startColumn: NaN,
endLine: NaN,
endColumn: NaN
};
try {
sourceInfo.filename = path.hub.file.opts.filename;
sourceInfo.sourceCode = path.hub.file.code.substring(
path.node.start,
path.node.end
);
sourceInfo.startLine = path.node.loc.start.line;
sourceInfo.startColumn = path.node.loc.start.column;
sourceInfo.endLine = path.node.loc.end.line;
sourceInfo.endColumn = path.node.loc.end.column;
} catch (err) {}
path.replaceWith(
t.conditionalExpression(
t.booleanLiteral(false),
t.booleanLiteral(false),
t.callExpression(
t.functionExpression(
t.identifier(WRAP_NAME),
[t.identifier('inputObject'), t.identifier('pathInfo')],
t.blockStatement([
t.tryStatement(
t.blockStatement([
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.identifier('Object'),
t.identifier('defineProperty')
),
[
t.identifier('inputObject'),
t.stringLiteral(SOURCE_NAME),
t.objectExpression([
t.objectProperty(
t.identifier('value'),
t.identifier('pathInfo')
)
])
]
)
)
]),
t.catchClause(t.identifier('err'), t.blockStatement([]))
),
t.returnStatement(t.identifier('inputObject'))
]),
false,
false
),
[
path.node,
t.objectExpression([
t.objectProperty(
t.identifier('sourceCode'),
t.stringLiteral(sourceInfo.sourceCode)
),
t.objectProperty(
t.identifier('filename'),
t.stringLiteral(sourceInfo.filename)
),
t.objectProperty(
t.identifier('startLine'),
t.numericLiteral(sourceInfo.startLine)
),
t.objectProperty(
t.identifier('startColumn'),
t.numericLiteral(sourceInfo.startColumn)
),
t.objectProperty(
t.identifier('endLine'),
t.numericLiteral(sourceInfo.endLine)
),
t.objectProperty(
t.identifier('endColumn'),
t.numericLiteral(sourceInfo.endColumn)
)
])
]
)
)
);
}
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment