From 90adec054ae251eb88c0477e1faafa174e846502 Mon Sep 17 00:00:00 2001 From: Ka-wai Wong Date: Tue, 5 Mar 2019 18:32:25 -0800 Subject: [PATCH 1/3] Add base getComponentType function and react check --- .../getComponentType/helpers/isReact.test.ts | 23 +++++++++++ .../getComponentType/helpers/isReact.ts | 16 ++++++++ .../utilities/getComponentType/index.test.ts | 40 +++++++++++++++++++ .../src/utilities/getComponentType/index.ts | 17 ++++++++ 4 files changed, 96 insertions(+) create mode 100644 server/src/utilities/getComponentType/helpers/isReact.test.ts create mode 100644 server/src/utilities/getComponentType/helpers/isReact.ts create mode 100644 server/src/utilities/getComponentType/index.test.ts create mode 100644 server/src/utilities/getComponentType/index.ts diff --git a/server/src/utilities/getComponentType/helpers/isReact.test.ts b/server/src/utilities/getComponentType/helpers/isReact.test.ts new file mode 100644 index 0000000..15b0e17 --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isReact.test.ts @@ -0,0 +1,23 @@ +import isReact from './isReact'; + +describe('isReact.ts', () => { + it('should return false when fileContents is null', () => { + expect(isReact(null)).toBeFalsy(); + }); + + it('should return false when fileContents does not contain the React import from react', () => { + expect(isReact(`import { Component } from 'react'`)).toBeFalsy(); + }); + + it('should return true when fileContents contains the React import from react', () => { + expect(isReact(`import React from 'react'`)).toBeTruthy(); + }); + + it('should return true when fileContents contains the React import last from react', () => { + expect(isReact(`import { Component }, React from 'react'`)).toBeTruthy(); + }); + + it('should return true when fileContents contains the React not last from react', () => { + expect(isReact(`import React, { Component } from 'react'`)).toBeTruthy(); + }); +}); diff --git a/server/src/utilities/getComponentType/helpers/isReact.ts b/server/src/utilities/getComponentType/helpers/isReact.ts new file mode 100644 index 0000000..2d1b74f --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isReact.ts @@ -0,0 +1,16 @@ +function isReact (fileContents: string): boolean { + let hasReactImport = false; + + if (fileContents != null) { + const importIndex = fileContents.indexOf(`from 'react'`); + + if (importIndex > 0) { + hasReactImport = fileContents.substring(0, importIndex).indexOf(' React ') > 0 + || fileContents.substring(0, importIndex).indexOf(' React,') > 0; + } + } + + return hasReactImport; +} + +export default isReact; diff --git a/server/src/utilities/getComponentType/index.test.ts b/server/src/utilities/getComponentType/index.test.ts new file mode 100644 index 0000000..f63981c --- /dev/null +++ b/server/src/utilities/getComponentType/index.test.ts @@ -0,0 +1,40 @@ +import getComponentType from '.'; +import helpers from './helpers'; + +describe('index.ts', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should return null when the input does not return true for any framework', () => { + helpers.isNgComponent = jest.fn(() => false); + helpers.isNgModule = jest.fn(() => false); + helpers.isReact = jest.fn(() => false); + const framework = getComponentType('test'); + expect(framework).toBeNull(); + }); + + it('should return ngComponent when isNgComponent is true', () => { + helpers.isNgComponent = jest.fn(() => true); + helpers.isNgModule = jest.fn(() => false); + helpers.isReact = jest.fn(() => false); + const framework = getComponentType('test'); + expect(framework).toBe('ngComponent'); + }); + + it('should return ngModule when isNgModule is true', () => { + helpers.isNgComponent = jest.fn(() => false); + helpers.isNgModule = jest.fn(() => true); + helpers.isReact = jest.fn(() => false); + const framework = getComponentType('test'); + expect(framework).toBe('ngModule'); + }); + + it('should return react when isReact is true', () => { + helpers.isNgComponent = jest.fn(() => false); + helpers.isNgModule = jest.fn(() => false); + helpers.isReact = jest.fn(() => true); + const framework = getComponentType('test'); + expect(framework).toBe('react'); + }); +}); diff --git a/server/src/utilities/getComponentType/index.ts b/server/src/utilities/getComponentType/index.ts new file mode 100644 index 0000000..e9d62cd --- /dev/null +++ b/server/src/utilities/getComponentType/index.ts @@ -0,0 +1,17 @@ +import helpers from './helpers'; + +function getComponentType (fileContents: string): string { + let componentType = null; + + if (helpers.isNgComponent(fileContents)) { + componentType = 'ngComponent'; + } else if (helpers.isNgModule(fileContents)) { + componentType = 'ngModule'; + } else if (helpers.isReact(fileContents)) { + componentType = 'react'; + } + + return componentType; +} + +export default getComponentType; From a05f3301fe313b6d25a9ff4a0806bda9797605b1 Mon Sep 17 00:00:00 2001 From: Ka-wai Wong Date: Tue, 5 Mar 2019 18:32:38 -0800 Subject: [PATCH 2/3] Add isNgComponent and isNgModule for getComponentType --- .../utilities/getComponentType/helpers/index.ts | 9 +++++++++ .../helpers/isNgComponent.test.ts | 15 +++++++++++++++ .../getComponentType/helpers/isNgComponent.ts | 11 +++++++++++ .../getComponentType/helpers/isNgModule.test.ts | 15 +++++++++++++++ .../getComponentType/helpers/isNgModule.ts | 11 +++++++++++ 5 files changed, 61 insertions(+) create mode 100644 server/src/utilities/getComponentType/helpers/index.ts create mode 100644 server/src/utilities/getComponentType/helpers/isNgComponent.test.ts create mode 100644 server/src/utilities/getComponentType/helpers/isNgComponent.ts create mode 100644 server/src/utilities/getComponentType/helpers/isNgModule.test.ts create mode 100644 server/src/utilities/getComponentType/helpers/isNgModule.ts diff --git a/server/src/utilities/getComponentType/helpers/index.ts b/server/src/utilities/getComponentType/helpers/index.ts new file mode 100644 index 0000000..b6e17ff --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/index.ts @@ -0,0 +1,9 @@ +import isNgComponent from './isNgComponent'; +import isNgModule from './isNgModule'; +import isReact from './isReact'; + +export default { + isNgComponent, + isNgModule, + isReact +}; diff --git a/server/src/utilities/getComponentType/helpers/isNgComponent.test.ts b/server/src/utilities/getComponentType/helpers/isNgComponent.test.ts new file mode 100644 index 0000000..315b6de --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isNgComponent.test.ts @@ -0,0 +1,15 @@ +import isNgComponent from './isNgComponent'; + +describe('isAngular.ts', () => { + it('should return false when fileContents is null', () => { + expect(isNgComponent(null)).toBeFalsy(); + }); + + it('should return false when fileContents does not contain @Component or @Module', () => { + expect(isNgComponent('test')).toBeFalsy(); + }); + + it('should return true when fileContents contains @Component', () => { + expect(isNgComponent('@Component')).toBeTruthy(); + }); +}); diff --git a/server/src/utilities/getComponentType/helpers/isNgComponent.ts b/server/src/utilities/getComponentType/helpers/isNgComponent.ts new file mode 100644 index 0000000..f209c1f --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isNgComponent.ts @@ -0,0 +1,11 @@ +function isNgComponent (fileContents: string): boolean { + let isNgComponent = false; + + if (fileContents != null) { + isNgComponent = fileContents.indexOf('@Component') >= 0; + } + + return isNgComponent; +} + +export default isNgComponent; diff --git a/server/src/utilities/getComponentType/helpers/isNgModule.test.ts b/server/src/utilities/getComponentType/helpers/isNgModule.test.ts new file mode 100644 index 0000000..8ca89bd --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isNgModule.test.ts @@ -0,0 +1,15 @@ +import isNgModule from './isNgModule'; + +describe('isAngular.ts', () => { + it('should return false when fileContents is null', () => { + expect(isNgModule(null)).toBeFalsy(); + }); + + it('should return false when fileContents does not contain @Component or @Module', () => { + expect(isNgModule('test')).toBeFalsy(); + }); + + it('should return true when fileContents contains @Module', () => { + expect(isNgModule('@NgModule')).toBeTruthy(); + }); +}); diff --git a/server/src/utilities/getComponentType/helpers/isNgModule.ts b/server/src/utilities/getComponentType/helpers/isNgModule.ts new file mode 100644 index 0000000..8da1c5a --- /dev/null +++ b/server/src/utilities/getComponentType/helpers/isNgModule.ts @@ -0,0 +1,11 @@ +function isNgModule (fileContents: string): boolean { + let isNgModule = false; + + if (fileContents != null) { + isNgModule = fileContents.indexOf('@NgModule') >= 0; + } + + return isNgModule; +} + +export default isNgModule; From feb4c84e96b6d200cba97fd91083f4e30564c896 Mon Sep 17 00:00:00 2001 From: Ka-wai Wong Date: Wed, 6 Mar 2019 12:12:06 -0800 Subject: [PATCH 3/3] Assign duplicated expression to variable in isReact --- server/src/utilities/getComponentType/helpers/isReact.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/utilities/getComponentType/helpers/isReact.ts b/server/src/utilities/getComponentType/helpers/isReact.ts index 2d1b74f..bd4a289 100644 --- a/server/src/utilities/getComponentType/helpers/isReact.ts +++ b/server/src/utilities/getComponentType/helpers/isReact.ts @@ -5,8 +5,9 @@ function isReact (fileContents: string): boolean { const importIndex = fileContents.indexOf(`from 'react'`); if (importIndex > 0) { - hasReactImport = fileContents.substring(0, importIndex).indexOf(' React ') > 0 - || fileContents.substring(0, importIndex).indexOf(' React,') > 0; + const importPortion = fileContents.substring(0, importIndex); + hasReactImport = importPortion.indexOf(' React ') > 0 + || importPortion.indexOf(' React,') > 0; } }