Skip to content

Commit

Permalink
Merge pull request #821 from bmish/improve-module-filepath-detection
Browse files Browse the repository at this point in the history
Avoid some false positives when detecting if a file is an Ember component, controller, etc
  • Loading branch information
bmish authored May 20, 2020
2 parents 56c8a60 + 6dc0d57 commit f1d0b9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,18 @@ function isDSModel(node, filePath) {
}

function isModuleByFilePath(filePath, module) {
const fileNameJs = `${module}.js`;
const fileNameTs = `${module}.ts`;
const folderName = `${module}s`;
const expectedFileNameJs = `${module}.js`;
const expectedFileNameTs = `${module}.ts`;
const expectedFolderName = `${module}s`;

const actualFolders = path.dirname(filePath).split(path.sep);
const actualFileName = path.basename(filePath);

/* Check both folder and filename to support both classic and POD's structure */
return (
filePath.includes(fileNameJs) || filePath.includes(fileNameTs) || filePath.includes(folderName)
actualFileName === expectedFileNameJs ||
actualFileName === expectedFileNameTs ||
actualFolders.includes(expectedFolderName)
);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/lib/utils/ember-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ describe('isModuleByFilePath', () => {
const filePath = 'example-app/components/path/to/some-component.ts';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeTruthy();
});

// Avoid false positives:
it('should not detect a component in a folder named `fake-components`', () => {
const filePath = 'example-app/fake-components/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});

it('should not detect a component with a file named `components`', () => {
const filePath = 'example-app/some-folder/path/to/components';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});

it('should not detect a component with a directory named `component.js`', () => {
const filePath = 'example-app/component.js/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});

it('should not detect a component with a directory named `component.ts`', () => {
const filePath = 'example-app/component.ts/path/to/file.js';
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
});
});

describe('isMirageDirectory', () => {
Expand Down

0 comments on commit f1d0b9e

Please sign in to comment.