Skip to content

Commit d66a704

Browse files
committed
feat(@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 angular#16811 and angular#16891
1 parent c9a5f3c commit d66a704

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
noop,
2020
url,
2121
} from '@angular-devkit/schematics';
22+
import { rename } from '@angular-devkit/schematics/src/rules/rename';
2223
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
2324
import {
2425
addDeclarationToModule,
@@ -49,15 +50,15 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
4950
return host;
5051
}
5152

52-
options.type = !!options.type ? options.type : 'Component';
53+
options.type = options.type != null ? options.type : 'Component';
5354

5455
const modulePath = options.module;
5556
const source = readIntoSourceFile(host, modulePath);
5657

5758
const componentPath = `/${options.path}/`
5859
+ (options.flat ? '' : strings.dasherize(options.name) + '/')
5960
+ strings.dasherize(options.name)
60-
+ '.'
61+
+ (options.type ? '.' : '')
6162
+ strings.dasherize(options.type);
6263
const relativePath = buildRelativePath(modulePath, componentPath);
6364
const classifiedName = strings.classify(options.name) + strings.classify(options.type);
@@ -155,6 +156,7 @@ export default function (options: ComponentOptions): Rule {
155156
'if-flat': (s: string) => options.flat ? '' : s,
156157
...options,
157158
}),
159+
!options.type ? rename(name => !!name.match(new RegExp('..')), (name) => name.replace('..', '.')) : noop(),
158160
move(parsedPath.path),
159161
]);
160162

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)