Skip to content

fix(@schematics/angular): Allow empty string in the type option #17040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

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

describe('<%= classify(name) %><%= classify(type) %>', () => {
let component: <%= classify(name) %><%= classify(type) %>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }
<%= dasherize(name) %> works!
</p>
`,<% } else { %>
templateUrl: './<%= dasherize(name) %>.<%= dasherize(type) %>.html',<% } if(inlineStyle) { %>
templateUrl: './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.html',<% } if(inlineStyle) { %>
styles: [<% if(displayBlock){ %>
`
:host {
display: block;
}
`<% } %>
],<% } else { %>
styleUrls: ['./<%= dasherize(name) %>.<%= dasherize(type) %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
styleUrls: ['./<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
})
Expand Down
16 changes: 14 additions & 2 deletions packages/schematics/angular/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
*/
import { strings } from '@angular-devkit/core';
import {
FileOperator,
Rule,
SchematicsException,
Tree,
apply,
applyTemplates,
chain,
filter,
forEach,
mergeWith,
move,
noop,
Expand Down Expand Up @@ -49,15 +51,15 @@ function addDeclarationToNgModule(options: ComponentOptions): Rule {
return host;
}

options.type = !!options.type ? options.type : 'Component';
options.type = options.type != null ? options.type : 'Component';

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

const componentPath = `/${options.path}/`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this line is already calculating the file name base, I think we should consider splitting this into two pieces and using them on the templates. “componentPath” can be the first two parts of the current. And “componentFile” can be the remainder as the filename part. This second part can then be used has the base for all file elements in the templates.
This would remove the need to fix the broken file names with a rename as well as optimize the name creation so that it doesn’t have to happen in multiple places as it does currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This componentPath we are using to add component in appModule. We are not using it for template currently.

+ (options.flat ? '' : strings.dasherize(options.name) + '/')
+ strings.dasherize(options.name)
+ '.'
+ (options.type ? '.' : '')
+ strings.dasherize(options.type);
const relativePath = buildRelativePath(modulePath, componentPath);
const classifiedName = strings.classify(options.name) + strings.classify(options.type);
Expand Down Expand Up @@ -155,6 +157,16 @@ export default function (options: ComponentOptions): Rule {
'if-flat': (s: string) => options.flat ? '' : s,
...options,
}),
!options.type ? forEach((file => {
if (!!file.path.match(new RegExp('..'))) {
return {
content: file.content,
path: file.path.replace('..', '.'),
};
} else {
return file;
}
}) as FileOperator) : noop(),
move(parsedPath.path),
]);

Expand Down
11 changes: 11 additions & 0 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ describe('Component Schematic', () => {
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
});

it('should allow empty string in the type option', async () => {
const options = { ...defaultOptions, type: '' };
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
expect(content).toContain('export class Foo implements OnInit');
expect(testContent).toContain("describe('Foo'");
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');
});

it('should use the module flag even if the module is a routing module', async () => {
const routingFileName = 'app-routing.module.ts';
const routingModulePath = `/projects/bar/src/app/${routingFileName}`;
Expand Down