diff --git a/src/config-parser.js b/src/config-parser.js index 51e5e4c..b034feb 100644 --- a/src/config-parser.js +++ b/src/config-parser.js @@ -56,14 +56,32 @@ class ConfigParser { // Return the configuration object or null. findConfigurationObject(ast) { // Try to find a default export - var defaultExport = ast.body.find( - node => node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'ObjectExpression' - ) - if (defaultExport) { - core.info('Found configuration object in default export declaration') + var defaultExport = ast.body.find(node => node.type === 'ExportDefaultDeclaration') + + // Direct default export + if (defaultExport && defaultExport.declaration.type === 'ObjectExpression') { + core.info('Found configuration object in direct default export declaration') return defaultExport.declaration } + // Indirect default export + else if (defaultExport && defaultExport.declaration.type === 'Identifier') { + const identifierName = defaultExport && defaultExport.declaration.name + const identifierDefinition = ast.body.find( + node => + node.type === 'VariableDeclaration' && + node.declarations.length == 1 && + node.declarations[0].type === 'VariableDeclarator' && + node.declarations[0].id.type === 'Identifier' && + node.declarations[0].id.name === identifierName && + node.declarations[0].init.type === 'ObjectExpression' + ) + if (identifierDefinition) { + core.info('Found configuration object in indirect default export declaration') + return identifierDefinition.declarations[0].init + } + } + // Try to find a module export var moduleExport = ast.body.find( node => diff --git a/src/config-parser.test.js b/src/config-parser.test.js index 830a88e..6c797c2 100644 --- a/src/config-parser.test.js +++ b/src/config-parser.test.js @@ -12,7 +12,6 @@ const cases = [ // // Default export // - { property: 'property', source: `export default {}`, @@ -90,6 +89,30 @@ const cases = [ expected: `export default { a2: false, a1: { a2: "value", a3: [12]}}` }, + // + // Indirect default export + // + { + property: 'property', + source: `const config = {}; export default config`, + expected: `const config = { property: "value"}; export default config` + }, + { + property: 'property', + source: `var config = {}; export default config`, + expected: `var config = { property: "value"}; export default config` + }, + { + property: 'a.b.c', + source: `var config = {}; export default config`, + expected: `var config = { a: { b: { c: "value"}}}; export default config` + }, + { + property: 'a.b.c', + source: `var config = { a: { b: [], c: "hello"}}; export default config`, + expected: `var config = { a: { b: { c: "value"}, c: "hello"}}; export default config` + }, + // // Direct module exports //