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 duplicate variable declaration #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions launch-darkly-variation-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,19 @@ module.exports = function launchDarklyVariationHelperPlugin({ types: t }) {

let { parent, type } = _findParent(parentCallExpression, t);

switch (type) {
case 'computed-property': {
let dependentKey = `${SERVICE_PROPERTY_NAME}.${key}`;
let fn = parent;
if (type === 'computed-property') {
let dependentKey = `${SERVICE_PROPERTY_NAME}.${key}`;

if (_shouldInjectDependentKey(key, parent, t)) {
parent.node.arguments.unshift(t.stringLiteral(dependentKey));
}

let fn = parent.get('arguments').find(a => t.isFunctionExpression(a));
if (_shouldInjectDependentKey(key, parent, t)) {
parent.node.arguments.unshift(t.stringLiteral(dependentKey));
}

if (fn && !_containsServiceDeclaration(fn, t)) {
_insertServiceDeclaration(fn, t);
}
fn = parent.get('arguments').find(a => t.isFunctionExpression(a));
}

return;
}
case 'function': {
_insertServiceDeclaration(parent, t);
return;
}
if (fn && !_containsServiceDeclaration(fn, t)) {
_insertServiceDeclaration(fn, t);
}
}
},
Expand Down
49 changes: 49 additions & 0 deletions nodetests/launch-darkly-variation-helper-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,55 @@ pluginTester({
]
});

pluginTester({
plugin,
title: 'New new module imports',
snapshot: false,
filename: __filename,
tests: [
{
title: 'Import computed from @ember/object',
code: `
import Component from '@ember/component';
import { variation } from 'ember-launch-darkly';

export default Component.extend({
boo() {
if(variation('bar')) {
return null;
}

if(variation('baz')) {
return null;
}
}
});
`,
output: `
import { default as launchDarklyService } from 'ember-service/inject';
import Component from '@ember/component';


export default Component.extend({
launchDarkly: launchDarklyService(),

boo() {
const launchDarkly = this.get('launchDarkly');

if (launchDarkly.get('bar')) {
return null;
}

if (launchDarkly.get('baz')) {
return null;
}
}
});
`
}
]
});

pluginTester({
plugin,
title: 'Code that should not be transformed',
Expand Down