Skip to content

Commit f1d0b9e

Browse files
authored
Merge pull request #821 from bmish/improve-module-filepath-detection
Avoid some false positives when detecting if a file is an Ember component, controller, etc
2 parents 56c8a60 + 6dc0d57 commit f1d0b9e

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/utils/ember.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,18 @@ function isDSModel(node, filePath) {
153153
}
154154

155155
function isModuleByFilePath(filePath, module) {
156-
const fileNameJs = `${module}.js`;
157-
const fileNameTs = `${module}.ts`;
158-
const folderName = `${module}s`;
156+
const expectedFileNameJs = `${module}.js`;
157+
const expectedFileNameTs = `${module}.ts`;
158+
const expectedFolderName = `${module}s`;
159+
160+
const actualFolders = path.dirname(filePath).split(path.sep);
161+
const actualFileName = path.basename(filePath);
159162

160163
/* Check both folder and filename to support both classic and POD's structure */
161164
return (
162-
filePath.includes(fileNameJs) || filePath.includes(fileNameTs) || filePath.includes(folderName)
165+
actualFileName === expectedFileNameJs ||
166+
actualFileName === expectedFileNameTs ||
167+
actualFolders.includes(expectedFolderName)
163168
);
164169
}
165170

tests/lib/utils/ember-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ describe('isModuleByFilePath', () => {
8686
const filePath = 'example-app/components/path/to/some-component.ts';
8787
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeTruthy();
8888
});
89+
90+
// Avoid false positives:
91+
it('should not detect a component in a folder named `fake-components`', () => {
92+
const filePath = 'example-app/fake-components/path/to/file.js';
93+
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
94+
});
95+
96+
it('should not detect a component with a file named `components`', () => {
97+
const filePath = 'example-app/some-folder/path/to/components';
98+
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
99+
});
100+
101+
it('should not detect a component with a directory named `component.js`', () => {
102+
const filePath = 'example-app/component.js/path/to/file.js';
103+
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
104+
});
105+
106+
it('should not detect a component with a directory named `component.ts`', () => {
107+
const filePath = 'example-app/component.ts/path/to/file.js';
108+
expect(emberUtils.isModuleByFilePath(filePath, 'component')).toBeFalsy();
109+
});
89110
});
90111

91112
describe('isMirageDirectory', () => {

0 commit comments

Comments
 (0)