-
Notifications
You must be signed in to change notification settings - Fork 2
feat: update object-to-ast.js and helpers.js to TS #4
Conversation
const stringified = stringify(object) | ||
const variableDeclarationNode = babel.template(`var x = ${stringified}`, { | ||
preserveComments: true, | ||
placeholderPattern: false, | ||
...fileOptions.parserOpts, | ||
sourceType: 'module', | ||
})() | ||
})() as babelCore.types.VariableDeclaration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worked out where declarations
is defined and it makes sens based on what is being passed the babel.template()
(i.e. a declaration of var x ...
).
I went for a straight up cast here given the template is essentially hard coded to be a VariableDeclaration
, but I've got the voice in the back of my head telling me this is wrong and I should use a type guard.
This type guard works:
function isVariableDeclaration(node: babelCore.types.Statement | babelCore.types.Statement[]): node is babelCore.types.VariableDeclaration {
return !Array.isArray(node) && node.type === 'VariableDeclaration'
}
However, I'm not sure what code to put on the failing side of it:
if (isVariableDeclaration(variableDeclarationNode)) {
return variableDeclarationNode.declarations[0].init // knows the type now without casting :)
}
// what now? Error? You can't get here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I really don't like the casting..........
This is what I have so far. There are some issues which I'll comment in the files.
Fixes #3
Fixes #6