Skip to content

Commit

Permalink
Renamed promiseRaceTo to promiseSome, prettier autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
sebryu committed Nov 7, 2023
1 parent 86074ed commit c7751da
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/actions/javascript/authorChecklist/authorChecklist.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import core from '@actions/core';
import github from '@actions/github';
import escapeRegExp from 'lodash/escapeRegExp';
import GithubUtils from '../../../libs/GithubUtils';
import CONST from '../../../libs/CONST';
import GithubUtils from '../../../libs/GithubUtils';
import newComponentCategory from './categories/newComponentCategory';

const pathToAuthorChecklist = `https://raw.githubusercontent.com/${CONST.GITHUB_OWNER}/${CONST.APP_REPO}/main/.github/PULL_REQUEST_TEMPLATE.md`;
Expand Down Expand Up @@ -142,7 +142,7 @@ async function generateDynamicChecksAndCheckForCompletion() {
// check for completion
try {
const numberOfItems = await getNumberOfItemsFromAuthorChecklist();
checkPRForCompletedChecklist(numberOfItems, newBody);
checkPRForCompletedChecklist(numberOfItems, checklist);
} catch (error) {
console.error(error);
if (error instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ type Category = {
items: string[];
};

export default Category;
export default Category;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import newComponent from './newComponentCategory';
import Category from './Category';
import newComponent from './newComponentCategory';

const categories: Category[] = [newComponent];

export default categories;
export default categories;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import github from '@actions/github';
import {parse} from '@babel/parser';
import traverse from '@babel/traverse';
import github from '@actions/github';
import Category from './Category';
import CONST from '../../../../libs/CONST';
import GithubUtils from '../../../../libs/GithubUtils';
import promiseRaceTo from '../../../../libs/promiseRaceTo';
import promiseSome from '../../../../libs/promiseSome';
import Category from './Category';

type SuperClassType = {superClass: {name?: string; object: {name: string}; property: {name: string}} | null; name: string};

Expand All @@ -21,6 +21,10 @@ const items = [
'I verified that each component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions',
];

function isComponentOrPureComponent(name?: string) {
return name === 'Component' || name === 'PureComponent';
}

function detectReactComponent(code: string, filename: string): boolean | undefined {
if (!code) {
console.error('failed to get code from a filename', code, filename);
Expand Down Expand Up @@ -51,7 +55,10 @@ function detectReactComponent(code: string, filename: string): boolean | undefin
// eslint-disable-next-line @typescript-eslint/naming-convention
ClassDeclaration(path) {
const {superClass} = path.node as unknown as SuperClassType;
if (superClass && ((superClass.object && superClass.object.name === 'React' && superClass.property.name === 'Component') || superClass.name === 'Component')) {
if (
superClass &&
((superClass.object && superClass.object.name === 'React' && isComponentOrPureComponent(superClass.property.name)) || isComponentOrPureComponent(superClass.name))
) {
isReactComponent = true;
path.stop();
}
Expand Down Expand Up @@ -84,9 +91,9 @@ async function detectReactComponentInFile(filename: string): Promise<boolean | u
async function detect(changedFiles: Array<{filename: string; status: string}>): Promise<boolean> {
const filteredFiles = changedFiles.filter(({filename, status}) => status === 'added' && (filename.endsWith('.js') || filename.endsWith('.ts') || filename.endsWith('.tsx')));
try {
await promiseRaceTo(
await promiseSome(
filteredFiles.map(({filename}) => detectReactComponentInFile(filename)),
true,
(result) => !!result,
);
return true;
} catch (err) {
Expand Down
31 changes: 18 additions & 13 deletions .github/actions/javascript/authorChecklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21484,12 +21484,12 @@ function wrappy (fn, cb) {

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.detectReactComponent = void 0;
const github_1 = __nccwpck_require__(5438);
const parser_1 = __nccwpck_require__(5026);
const traverse_1 = __nccwpck_require__(1380);
const github_1 = __nccwpck_require__(5438);
const CONST_1 = __nccwpck_require__(4097);
const GithubUtils_1 = __nccwpck_require__(7999);
const promiseRaceTo_1 = __nccwpck_require__(8235);
const promiseSome_1 = __nccwpck_require__(8534);
const items = [
"I verified that similar component doesn't exist in the codebase",
'I verified that all props are defined accurately and each prop has a `/** comment above it */`',
Expand All @@ -21502,6 +21502,9 @@ const items = [
'I verified that all JSX used for rendering exists in the render method',
'I verified that each component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions',
];
function isComponentOrPureComponent(name) {
return name === 'Component' || name === 'PureComponent';
}
function detectReactComponent(code, filename) {
if (!code) {
console.error('failed to get code from a filename', code, filename);
Expand Down Expand Up @@ -21530,7 +21533,8 @@ function detectReactComponent(code, filename) {
// eslint-disable-next-line @typescript-eslint/naming-convention
ClassDeclaration(path) {
const { superClass } = path.node;
if (superClass && ((superClass.object && superClass.object.name === 'React' && superClass.property.name === 'Component') || superClass.name === 'Component')) {
if (superClass &&
((superClass.object && superClass.object.name === 'React' && isComponentOrPureComponent(superClass.property.name)) || isComponentOrPureComponent(superClass.name))) {
isReactComponent = true;
path.stop();
}
Expand Down Expand Up @@ -21561,7 +21565,7 @@ async function detectReactComponentInFile(filename) {
async function detect(changedFiles) {
const filteredFiles = changedFiles.filter(({ filename, status }) => status === 'added' && (filename.endsWith('.js') || filename.endsWith('.ts') || filename.endsWith('.tsx')));
try {
await (0, promiseRaceTo_1.default)(filteredFiles.map(({ filename }) => detectReactComponentInFile(filename)), true);
await (0, promiseSome_1.default)(filteredFiles.map(({ filename }) => detectReactComponentInFile(filename)), (result) => !!result);
return true;
}
catch (err) {
Expand All @@ -21577,32 +21581,33 @@ exports["default"] = newComponentCategory;

/***/ }),

/***/ 8235:
/***/ 8534:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
/**
* Like Promise.race, except it resolves with the first promise that resolves to the desired value.
* If no promise resolves to the desired value, it rejects.
* Like _.some but for promises. It short-circuts after a promise fulfills with a value that passes the test implemented by provided function.
* It does not wait for the other promises to complete once it finds one.
* If no promise passes the provided test, it rejects.
*/
function promiseRaceTo(promises, desiredValue) {
function promiseSome(promises, callbackFn) {
return new Promise((resolve, reject) => {
for (const p of promises) {
Promise.resolve(p)
.then((res) => {
if (res !== desiredValue) {
if (!callbackFn(res)) {
return;
}
resolve(res);
resolve(true);
})
.catch(() => { });
}
Promise.allSettled(promises).then(() => reject());
});
}
exports["default"] = promiseRaceTo;
exports["default"] = promiseSome;


/***/ }),
Expand Down Expand Up @@ -64840,8 +64845,8 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
const core_1 = __nccwpck_require__(2186);
const github_1 = __nccwpck_require__(5438);
const escapeRegExp_1 = __nccwpck_require__(8415);
const GithubUtils_1 = __nccwpck_require__(7999);
const CONST_1 = __nccwpck_require__(4097);
const GithubUtils_1 = __nccwpck_require__(7999);
const newComponentCategory_1 = __nccwpck_require__(8750);
const pathToAuthorChecklist = `https://raw.githubusercontent.com/${CONST_1.default.GITHUB_OWNER}/${CONST_1.default.APP_REPO}/main/.github/PULL_REQUEST_TEMPLATE.md`;
const checklistStartsWith = '### PR Author Checklist';
Expand Down Expand Up @@ -64959,7 +64964,7 @@ async function generateDynamicChecksAndCheckForCompletion() {
// check for completion
try {
const numberOfItems = await getNumberOfItemsFromAuthorChecklist();
checkPRForCompletedChecklist(numberOfItems, newBody);
checkPRForCompletedChecklist(numberOfItems, checklist);
}
catch (error) {
console.error(error);
Expand Down
19 changes: 0 additions & 19 deletions .github/libs/promiseRaceTo.ts

This file was deleted.

20 changes: 20 additions & 0 deletions .github/libs/promiseSome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Like _.some but for promises. It short-circuts after a promise fulfills with a value that passes the test implemented by provided function.
* It does not wait for the other promises to complete once it finds one.
* If no promise passes the provided test, it rejects.
*/
export default function promiseSome<T>(promises: Array<Promise<T>>, callbackFn: (arg0: T) => boolean): Promise<boolean> {
return new Promise((resolve, reject) => {
for (const p of promises) {
Promise.resolve(p)
.then((res) => {
if (!callbackFn(res)) {
return;
}
resolve(true);
})
.catch(() => {});
}
Promise.allSettled(promises).then(() => reject());
});
}
26 changes: 26 additions & 0 deletions tests/actions/detectReactComponent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ describe('detectReactComponent test', () => {
expect(result).toBe(true);
});

it('should detect if code is a Class pure component', () => {
const code = `
class Component extends PureComponent {
render() {
return <div>Hello World</div>;
}
}
`;
const result = detectReactComponent(code, 'filename.js');

expect(result).toBe(true);
});

it('should detect if code is a Class pure component with React namespace', () => {
const code = `
class Component extends React.PureComponent {
render() {
return <div>Hello World</div>;
}
}
`;
const result = detectReactComponent(code, 'filename.js');

expect(result).toBe(true);
});

it('should not detect if code is not a React component', () => {
const code = `
function NotAComponent() {
Expand Down

0 comments on commit c7751da

Please sign in to comment.