From 86f70f16107ecf7b4007d2589549b27e3ac394c8 Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Fri, 20 Dec 2019 19:16:36 +0530 Subject: [PATCH] handle undefined values in cli/api getPlugins --- cli/api.ts | 25 ++++++++++--------------- package.json | 4 +++- yarn.lock | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/cli/api.ts b/cli/api.ts index 1ee95365..ec4932e1 100644 --- a/cli/api.ts +++ b/cli/api.ts @@ -54,32 +54,27 @@ const getFileContents = memoize(() => { const getParsedFile = memoize(() => recast.parse(getFileContents()!)); -const getProperties = memoize(() => (getParsedFile().program.body as any[]).map(obj => obj)); +const getProperties = memoize(() => ((getParsedFile()?.program?.body as any[]) || []).map(obj => obj)); -const getPlugins = memoize(() => { +const getPluginsByKey = (key: string) => { const properties = getProperties(); for (let i = 0; i < properties.length; i++) { - const rightProperties = Object.values(properties[i].expression.right.properties); + const rightProperties = Object.values(properties[i]?.expression?.right?.properties || {}); for (let j = 0; j < rightProperties.length; j++) { const plugin = rightProperties[j]; - if (plugin.key.name === 'plugins') { - return plugin.value.elements as any[]; + if (plugin?.key?.name === key) { + return (plugin?.value?.elements as any[]) || []; } } } +}; + +const getPlugins = memoize(() => { + return getPluginsByKey('plugins'); }); const getLocalPlugins = memoize(() => { - const properties = getProperties(); - for (let i = 0; i < properties.length; i++) { - const rightProperties = Object.values(properties[i].expression.right.properties); - for (let j = 0; j < rightProperties.length; j++) { - const plugin = rightProperties[j]; - if (plugin.key.name === 'localPlugins') { - return plugin.value.elements as any[]; - } - } - } + return getPluginsByKey('localPlugins'); }); function exists() { diff --git a/package.json b/package.json index 75a935e8..db69e917 100644 --- a/package.json +++ b/package.json @@ -143,7 +143,8 @@ ], "@babel/plugin-proposal-numeric-separator", "@babel/proposal-class-properties", - "@babel/proposal-object-rest-spread" + "@babel/proposal-object-rest-spread", + "@babel/plugin-proposal-optional-chaining" ], "env": { "production": { @@ -292,6 +293,7 @@ "@babel/plugin-proposal-class-properties": "^7.7.4", "@babel/plugin-proposal-numeric-separator": "^7.7.4", "@babel/plugin-proposal-object-rest-spread": "^7.7.4", + "@babel/plugin-proposal-optional-chaining": "7.7.5", "@babel/preset-react": "7.7.4", "@babel/preset-typescript": "7.7.4", "@types/args": "3.0.0", diff --git a/yarn.lock b/yarn.lock index 88ab3f96..df90d019 100644 --- a/yarn.lock +++ b/yarn.lock @@ -323,6 +323,14 @@ "@babel/helper-plugin-utils" "^7.0.0" "@babel/plugin-syntax-optional-catch-binding" "^7.2.0" +"@babel/plugin-proposal-optional-chaining@7.7.5": + version "7.7.5" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.7.5.tgz#f0835f044cef85b31071a924010a2a390add11d4" + integrity sha512-sOwFqT8JSchtJeDD+CjmWCaiFoLxY4Ps7NjvwHC/U7l4e9i5pTRNt8nDMIFSOUL+ncFbYSwruHM8WknYItWdXw== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-optional-chaining" "^7.7.4" + "@babel/plugin-syntax-async-generators@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.2.0.tgz#69e1f0db34c6f5a0cf7e2b3323bf159a76c8cb7f" @@ -365,6 +373,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-optional-chaining@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.7.4.tgz#c91fdde6de85d2eb8906daea7b21944c3610c901" + integrity sha512-2MqYD5WjZSbJdUagnJvIdSfkb/ucOC9/1fRJxm7GAxY6YQLWlUvkfxoNbUPcPLHJyetKUDQ4+yyuUyAoc0HriA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-typescript@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.7.4.tgz#5d037ffa10f3b25a16f32570ebbe7a8c2efa304b"