Skip to content

Commit 35ad92c

Browse files
sacgroverdgp1130
authored andcommitted
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 Fixed minor merge conflict due to the lack of d6fa2bd in the release branch. (cherry picked from commit 1c1f1cd)
1 parent bd4078d commit 35ad92c

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,9 +7,9 @@ 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: []<% } else { %>
12-
styleUrls: ['./<%= dasherize(name) %>.<%= dasherize(type) %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
12+
styleUrls: ['./<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
1313
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
1414
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
1515
})

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
@@ -268,6 +268,17 @@ describe('Component Schematic', () => {
268268
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
269269
});
270270

271+
it('should allow empty string in the type option', async () => {
272+
const options = { ...defaultOptions, type: '' };
273+
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
274+
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
275+
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
276+
expect(content).toContain('export class Foo implements OnInit');
277+
expect(testContent).toContain("describe('Foo'");
278+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
279+
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');
280+
});
281+
271282
it('should use the module flag even if the module is a routing module', async () => {
272283
const routingFileName = 'app-routing.module.ts';
273284
const routingModulePath = `/projects/bar/src/app/${routingFileName}`;

0 commit comments

Comments
 (0)