From 27c6198b6b73d630a18997200353adabc9c4e2bb Mon Sep 17 00:00:00 2001 From: Rishabh Date: Tue, 4 Jun 2019 17:11:35 +0530 Subject: [PATCH] chore: add errors for invalid params --- packages/utils/ast-utils.ts | 11 ++++++++++- packages/utils/recursive-parser.ts | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/utils/ast-utils.ts b/packages/utils/ast-utils.ts index 67bc6b55b23..83b39f11f72 100644 --- a/packages/utils/ast-utils.ts +++ b/packages/utils/ast-utils.ts @@ -3,6 +3,9 @@ import * as validateIdentifier from "./validate-identifier"; function isImportPresent (j: JSCodeshift, ast: Node, path: string): boolean { + if (typeof path !== "string") { + throw new Error(`path parameter should be string, recieved ${typeof path}`); + } let isPresent = false; ast.find(j.CallExpression).forEach(callExp => { if ((callExp.value as Node).callee.name === 'require' && (callExp.value as Node).arguments[0].value === path) { @@ -646,11 +649,17 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string): } function addMergeImports(configIdentifier: string, configPath: string) { + if (typeof configIdentifier !== "string" || typeof configPath !== "string") { + throw new Error(`Both parameters should be string. Recieved ${configIdentifier}, ${configPath}`) + } ast.find(j.Program).forEach(p => { if (!isImportPresent(j, ast, 'webpack-merge')) { (p.value as Node).body.splice(-1, 0, `const merge = require('webpack-merge')`); } - (p.value as Node).body.splice(-1, 0, `const ${configIdentifier} = require('${configPath}')`); + + if (!isImportPresent(j, ast, configPath)) { + (p.value as Node).body.splice(-1, 0, `const ${configIdentifier} = require('${configPath}')`); + } }) } diff --git a/packages/utils/recursive-parser.ts b/packages/utils/recursive-parser.ts index 275c30651af..217d2289a9c 100644 --- a/packages/utils/recursive-parser.ts +++ b/packages/utils/recursive-parser.ts @@ -15,7 +15,7 @@ export default function recursiveTransform( console.error("Error in parsing top scope, Array required"); return false; } else if (key === "merge") { - if (Array.isArray(value) && typeof value[0] === "string" && typeof value[1] === "string") { + if (Array.isArray(value)) { return utils.parseMerge(j, ast, value, action); } }