Skip to content

Commit 837b797

Browse files
committed
fix(@schematics/angular): Allow empty string in the type option
Currently, Component and Class have the options to add custom type. In the case of class, It's already working fine with an empty string in type but in the case of component When setting the type to an empty string the file names generated will contain an extra period (.) which breaks the flow. With this PR, It will generate the files without an extra period (.) Reference #16811 and #16891
1 parent c9a5f3c commit 837b797

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.spec.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22

3-
import { <%= classify(name) %><%= classify(type) %> } from './<%= dasherize(name) %>.<%= dasherize(type) %>';
3+
import { <%= classify(name) %><%= classify(type) %> } from './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>';
44

55
describe('<%= classify(name) %><%= classify(type) %>', () => {
66
let component: <%= classify(name) %><%= classify(type) %>;

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
77
<%= dasherize(name) %> works!
88
</p>
99
`,<% } else { %>
10-
templateUrl: './<%= dasherize(name) %>.<%= dasherize(type) %>.html',<% } if(inlineStyle) { %>
10+
templateUrl: './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.html',<% } if(inlineStyle) { %>
1111
styles: [<% if(displayBlock){ %>
1212
`
1313
:host {
1414
display: block;
1515
}
1616
`<% } %>
1717
],<% } else { %>
18-
styleUrls: ['./<%= dasherize(name) %>.<%= dasherize(type) %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
18+
styleUrls: ['./<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1919
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
2020
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
2121
})

packages/schematics/angular/component/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
*/
88
import { strings } from '@angular-devkit/core';
99
import {
10+
FileOperator,
1011
Rule,
1112
SchematicsException,
1213
Tree,
1314
apply,
1415
applyTemplates,
1516
chain,
1617
filter,
18+
forEach,
1719
mergeWith,
1820
move,
1921
noop,
@@ -49,15 +51,15 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
4951
return host;
5052
}
5153

52-
options.type = !!options.type ? options.type : 'Component';
54+
options.type = options.type != null ? options.type : 'Component';
5355

5456
const modulePath = options.module;
5557
const source = readIntoSourceFile(host, modulePath);
5658

5759
const componentPath = `/${options.path}/`
5860
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
5961
+ strings.dasherize(options.name)
60-
+ '.'
62+
+ (options.type ? '.' : '')
6163
+ strings.dasherize(options.type);
6264
const relativePath = buildRelativePath(modulePath, componentPath);
6365
const classifiedName = strings.classify(options.name) + strings.classify(options.type);
@@ -155,6 +157,16 @@ export default function (options: ComponentOptions): Rule {
155157
'if-flat': (s: string) => options.flat ? '' : s,
156158
...options,
157159
}),
160+
!options.type ? forEach((file => {
161+
if (!!file.path.match(new RegExp('..'))) {
162+
return {
163+
content: file.content,
164+
path: file.path.replace('..', '.'),
165+
};
166+
} else {
167+
return file;
168+
}
169+
}) as FileOperator) : noop(),
158170
move(parsedPath.path),
159171
]);
160172

packages/schematics/angular/component/index_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ describe('Component Schematic', () => {
297297
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
298298
});
299299

300+
it('should allow empty string in the type option', async () => {
301+
const options = { ...defaultOptions, type: '' };
302+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
303+
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
304+
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
305+
expect(content).toContain('export class Foo implements OnInit');
306+
expect(testContent).toContain("describe('Foo'");
307+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
308+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');
309+
});
310+
300311
it('should use the module flag even if the module is a routing module', async () => {
301312
const routingFileName = 'app-routing.module.ts';
302313
const routingModulePath = `/projects/bar/src/app/${routingFileName}`;

0 commit comments

Comments
 (0)