Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/no-unused-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module.exports = {
}
if (node.source.value === '@ember/object/computed') {
for (const spec of node.specifiers) {
if (spec.type === 'ImportDefaultSpecifier') {
if (spec.type !== 'ImportSpecifier') {
continue;
}
const name = spec.imported.name;
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ function getModuleProperties(moduleNode, scopeManager) {
* Get alias name of default ember import.
*
* @param {ImportDeclaration} importDeclaration node to parse
* @return {String} import name
* @return {String|null} local name of the default import, or null
*/
function getEmberImportAliasName(importDeclaration) {
if (!importDeclaration.source) {
Expand All @@ -691,7 +691,10 @@ function getEmberImportAliasName(importDeclaration) {
if (importDeclaration.source.value !== 'ember') {
return null;
}
return importDeclaration.specifiers[0].local.name;
const defaultSpecifier = importDeclaration.specifiers.find(
(specifier) => specifier.type === 'ImportDefaultSpecifier'
);
return defaultSpecifier ? defaultSpecifier.local.name : null;
}

function isSingleLineFn(property, importedEmberName, importedObserverName) {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils/new-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function buildFix(node, modulesData) {
const moduleName = node.source.value;

function fix(fixer) {
// A namespace import has no single new module to point to.
if (node.specifiers.some((specifier) => specifier.type === 'ImportNamespaceSpecifier')) {
return null;
}

const newImports = buildNewImports(node, modulesData, moduleName);

const lines = buildLines(newImports);
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-ember-testing-in-module-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ruleTester = new RuleTester({

ruleTester.run('no-ember-testing-in-module-scope', rule, {
valid: [
"import 'ember';",
`
import Ember from 'ember';

Expand Down Expand Up @@ -41,6 +42,17 @@ ruleTester.run('no-ember-testing-in-module-scope', rule, {
'const testing = true;',
],
invalid: [
{
// The alias comes from the default import, not from a named one.
code: `
import { run } from 'ember';
import Ember from 'ember';

if (Ember.testing) {}
`,
output: null,
errors: [{ message: ERROR_MESSAGES[0], type: 'MemberExpression' }],
},
{
code: `
import Random1 from 'random1';
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-old-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const eslintTester = new RuleTester({
eslintTester.run('no-old-shims', rule, {
valid: ["import Ember from 'ember';", "import RSVP from 'rsvp';"],
invalid: [
{
// A namespace import has no single new module to point to, so no autofix.
code: "import * as Application from 'ember-application';",
output: null,
errors: [{ message: "Don't use import paths from ember-cli-shims" }],
},
{
code: "import Component from 'ember-component';",
output: "import Component from '@ember/component';",
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-unused-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const emberObjectUses2 = generateEmberObjectUseCasesFor('unrelatedProp').join(''

ruleTester.run('no-unused-services', rule, {
valid: [
`${SERVICE_IMPORT} import * as macros from '@ember/object/computed'; class MyClass { @service ${SERVICE_NAME}; foo() { this.${SERVICE_NAME}.bar(); } }`,
...generateValid(),
...generateValid(NEW_SERVICE_IMPORT),
...generateMacroUseCasesFor(SERVICE_NAME),
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/use-ember-data-rfc-395-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ ruleTester.run('use-ember-data-rfc-395-imports', rule, {
],

invalid: [
{
// A namespace import has no single new module to point to, so no autofix.
code: "import * as DSModel from 'ember-data/model';",
output: null,
errors: [{ type: 'ImportDeclaration' }],
},
{
code: `import DS from 'ember-data';

Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/use-ember-get-and-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const RuleTester = require('eslint').RuleTester;
const eslintTester = new RuleTester();
eslintTester.run('use-ember-get-and-set', rule, {
valid: [
{
code: "import 'ember';\nthis.foo = 1;",
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
},
'get(this, "test")',
'get(controller, "test")',
'set(this, "test", someValue)',
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/utils/ember-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,20 @@ describe('getEmberImportAliasName', () => {
const node = babelESLintParse("import foo from 'ember'").body[0];
expect(emberUtils.getEmberImportAliasName(node)).toBe('foo');
});

it('should get the default import name when named imports come first', () => {
const node = babelESLintParse("import foo, { run } from 'ember'").body[0];
expect(emberUtils.getEmberImportAliasName(node)).toBe('foo');
});

it('should return null without a default import', () => {
expect(emberUtils.getEmberImportAliasName(babelESLintParse("import 'ember'").body[0])).toBe(
null
);
expect(
emberUtils.getEmberImportAliasName(babelESLintParse("import { run } from 'ember'").body[0])
).toBe(null);
});
});

describe('isEmberObjectImplementingUnknownProperty', () => {
Expand Down
Loading