From f32f5ddb75591c99eeaa1550a04ba57604a02eb1 Mon Sep 17 00:00:00 2001 From: Ludovic SCHMIEDER Date: Mon, 10 Feb 2020 12:02:52 +0100 Subject: [PATCH] fix: handle services/controllers names cuted (strings with '+' operator) --- .../__testfixtures__/inject.input.js | 20 +++++++++++++++++-- .../__testfixtures__/inject.output.js | 10 ++++++++++ .../convert-module-for-to-setup-test/index.js | 13 ++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.input.js b/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.input.js index e5f413c..a8e72d7 100644 --- a/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.input.js +++ b/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.input.js @@ -1,12 +1,12 @@ import { moduleFor, test } from 'ember-qunit'; - + moduleFor('service:foo-bar', 'Unit | Service | FooBar', { }); test('it exists', function(assert) { this.inject.service('foo'); this.inject.service('foo', { as: 'bar' }); -}); +}); test('it works for controllers', function(assert) { this.inject.controller('foo'); @@ -16,3 +16,19 @@ test('it works for controllers', function(assert) { test('handles dasherized names', function(assert) { this.inject.service('foo-bar'); }); + +test('handle cuted (long) services names', function(assert) { + this.inject.service('foo-bar-with-a' + + '-very-long-name'); + this.inject.service('foo-bar-with-a' + + '-very-long-name', { as: 'foo-bar-with-a' + + '-very-long-name' }); +}); + +test('handle cuted (long) controllers names', function(assert) { + this.inject.controller('foo-bar-with-a' + + '-very-long-name'); + this.inject.controller('foo-bar-with-a' + + '-very-long-name', { as: 'foo-bar-with-a' + + '-very-long-name' }); +}); diff --git a/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.output.js b/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.output.js index 145056f..7587705 100644 --- a/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.output.js +++ b/transforms/convert-module-for-to-setup-test/__testfixtures__/inject.output.js @@ -17,4 +17,14 @@ module('Unit | Service | FooBar', function(hooks) { test('handles dasherized names', function(assert) { this['foo-bar'] = this.owner.lookup('service:foo-bar'); }); + + test('handle cuted (long) services names', function(assert) { + this['foo-bar-with-a-very-long-name'] = this.owner.lookup('service:foo-bar-with-a-very-long-name'); + this['foo-bar-with-a-very-long-name'] = this.owner.lookup('service:foo-bar-with-a-very-long-name'); + }); + + test('handle cuted (long) controllers names', function(assert) { + this['foo-bar-with-a-very-long-name'] = this.owner.lookup('controller:foo-bar-with-a-very-long-name'); + this['foo-bar-with-a-very-long-name'] = this.owner.lookup('controller:foo-bar-with-a-very-long-name'); + }); }); diff --git a/transforms/convert-module-for-to-setup-test/index.js b/transforms/convert-module-for-to-setup-test/index.js index 193d972..8435c03 100755 --- a/transforms/convert-module-for-to-setup-test/index.js +++ b/transforms/convert-module-for-to-setup-test/index.js @@ -861,6 +861,12 @@ module.exports = function(file, api) { } } + function flatten(node) { + const isBE = node.type === 'BinaryExpression'; + const isPLUS = node.operator === '+'; + return isBE && isPLUS ? [...flatten(node.left), ...flatten(node.right)] : [node]; + } + function updateInjectCalls(ctx) { ctx .find(j.CallExpression, { @@ -878,13 +884,16 @@ module.exports = function(file, api) { }) .forEach(p => { let injectType = p.node.callee.property.name; - let injectedName = p.node.arguments[0].value; + let injectedName = flatten(p.node.arguments[0]) + .map(node => node.value) + .join(''); let localName = injectedName; if (p.node.arguments[1]) { let options = p.node.arguments[1]; let as = options.properties.find(property => property.key.name === 'as'); if (as) { - localName = as.value.value; + let flattenValue = flatten(as.value); + localName = flattenValue.map(node => node.value).join(''); } } let property = j.identifier(localName);