Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): Safe access to leadingComments #26

Merged
merged 1 commit into from Aug 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/__tests__/index.js
Expand Up @@ -210,6 +210,16 @@ pluginTester({
module.exports = smth.UNDEFINED;
`,
},
{
snapshot: false,
code: '// @preval',
},
{
snapshot: false,
code: `
// @preval
/* comment */`,
},

// please add a file for your use-case
// in the `fixtures` directory and make
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Expand Up @@ -11,7 +11,8 @@ function prevalPlugin({types: t, template, transformFromAst}) {
name: 'preval',
visitor: {
Program(path, {file: {opts: {filename}}}) {
const comments = path.node.body[0].leadingComments || []
const firstNode = path.node.body[0] || {}
const comments = firstNode.leadingComments || []
const isPreval = comments.some(isPrevalComment)

if (!isPreval) {
Expand Down Expand Up @@ -47,7 +48,9 @@ function prevalPlugin({types: t, template, transformFromAst}) {
}
const string = path.get('quasi').evaluate().value
if (!string) {
throw new Error('Unable to determine the value of your preval string')
throw new Error(
'Unable to determine the value of your preval string',
)
}
const replacement = getReplacement({string, filename})
path.replaceWith(replacement)
Expand Down Expand Up @@ -168,7 +171,7 @@ function looksLike(a, b) {

function isPrimitive(val) {
// eslint-disable-next-line
return val == null || /^[sbn]/.test(typeof val)
return val == null || /^[sbn]/.test(typeof val);
}

/*
Expand Down