Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Also, check [Danger's guide on Phrasing](http://danger.systems/js/usage/culture.
- [ ] Warn when `package.json` was modified and `yarn.lock` or `shrinkwrap` was not
- [x] Warn if node version is different between .travis.yml, .nvmrc, package.json and README (or just warn if node version has change just in one of these locations)
- [x] At packages.json every package should have its version fixed (do not use ^ or ~), or explicitly set the major and minor versions (ie.: 1.2.x)
- [ ] [TypeScript] Warn if using `<any>` as return type.
- [x] [TypeScript] Warn if using `<any>` as return type.
- [x] Warn if some files/folders were changed/committed:
- `npm-debug.log`
- `yarn-error.log`
Expand Down
68 changes: 65 additions & 3 deletions src/rules/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ describe('Node info', () => {
it('Should not warn about `console.log` when it was not added to code', async () => {
global.danger = {
git: {
modified_files: null,
created_files: null,
modified_files: ['any'],
created_files: ['any'],
diffForFile: jest.fn(() => ({
added: `
any text;
Expand All @@ -114,7 +114,7 @@ describe('Node info', () => {

await node.consoleLog();

expect(global.fail).not.toBeCalled();
expect(global.warn).not.toBeCalled();

});
});
Expand Down Expand Up @@ -333,4 +333,66 @@ describe('Node info', () => {

});

describe('Typescript return any', () => {

it('Should warn about using <any> as a function return type', async () => {
global.danger = {
git: {
modified_files: ['file.ts'],
created_files: ['any'],
diffForFile: jest.fn(() => ({
added: `
test(x): any {

}
more text`,
})),
},
};

await node.typescriptAnyReturn();

expect(global.warn).toBeCalled();
});

it('Should warn about using Promise<any> as a return type', async () => {
global.danger = {
git: {
modified_files: ['file.ts'],
created_files: ['any'],
diffForFile: jest.fn(() => ({
added: `
test(x): Promise<any> {

}
more text`,
})),
},
};

await node.typescriptAnyReturn();

expect(global.warn).toBeCalled();
});

it('Should not warn about using <any> as return type', async () => {
global.danger = {
git: {
modified_files: ['file.ts'],
created_files: ['any'],
diffForFile: jest.fn(() => ({
added: `
test(x): string {

}
more text`,
})),
},
};

await node.typescriptAnyReturn();

expect(global.warn).not.toBeCalled();
});
});
});
9 changes: 8 additions & 1 deletion src/rules/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export let node: Scope = {

/** Warns when 'console.log' is added to code */
async consoleLog() {
if (changedFilesContainsRegex(/console\.log/ig)) {
if (await changedFilesContainsRegex(/console\.log/ig)) {
warn('This PR adds `console.log` to code.');
}
},
Expand Down Expand Up @@ -127,4 +127,11 @@ export let node: Scope = {
}
},

/** Warns when <any> is used as return type */
async typescriptAnyReturn() {
if (await changedFilesContainsRegex(/\)(\s|\n)*:(\s|\n)*(any|\w+<any>)(\s|\n)*{/g)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we reduce the scope of this check only for .ts files?

warn('This PR is using <any> as return type.');
}
},

};