babel-plugin-feature-flags
Advanced tools
Comparing version
35
index.js
@@ -6,9 +6,16 @@ var stringify = require('json-stable-stringify'); | ||
options.features = options.features || {}; | ||
options.import = options.import || {}; | ||
options.import.name = options.import.name || 'default'; | ||
options.imports = options.imports || []; | ||
if (typeof options.import.module !== 'string') { | ||
throw new Error("options.import.module must be the name of a module, e.g. 'my-app/features'"); | ||
if (options.import) { | ||
options.imports.push(options.import); | ||
} | ||
options.imports.forEach(function(_import) { | ||
_import.name = _import.name || 'default'; | ||
if (typeof _import.module !== 'string') { | ||
throw new Error("options.import.module must be the name of a module, e.g. 'my-app/features'"); | ||
} | ||
}); | ||
Object.keys(options.features).forEach(function(feature) { | ||
@@ -36,14 +43,16 @@ var value = options.features[feature]; | ||
var callee = this.get('callee'); | ||
if (callee.referencesImport(options.import.module, options.import.name)) { | ||
var featureName = getFeatureName(node); | ||
if (featureName in options.features) { | ||
var value = options.features[featureName]; | ||
options.imports.forEach(function(_import) { | ||
if (callee.referencesImport(_import.module, _import.name)) { | ||
var featureName = getFeatureName(node); | ||
if (featureName in options.features) { | ||
var value = options.features[featureName]; | ||
if (typeof value === 'boolean') { | ||
this.replaceWith(t.literal(value)); | ||
if (typeof value === 'boolean') { | ||
this.replaceWith(t.literal(value)); | ||
} | ||
} else { | ||
file.log.error("An unknown feature '" + featureName + "'' was encountered"); | ||
} | ||
} else { | ||
file.log.error("An unknown feature '" + featureName + "'' was encountered"); | ||
} | ||
} | ||
}.bind(this)); | ||
} | ||
@@ -50,0 +59,0 @@ }); |
{ | ||
"name": "babel-plugin-feature-flags", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "A babel transform for managing feature flags", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -55,3 +55,28 @@ var assert = require('assert'); | ||
testFixture('preserves-other-imports', options); | ||
testFixture('import-name', { | ||
import: { | ||
module: 'features', | ||
name: 'isFeatureEnabled' | ||
}, | ||
features: { | ||
enabled: 'enabled', | ||
disabled: 'disabled', | ||
dynamic: 'dynamic' | ||
} | ||
}); | ||
testFixture('multiple-imports', { | ||
imports: [{ | ||
module: 'features' | ||
}, { | ||
module: 'other-features', | ||
name: 'isFeatureEnabled' | ||
}], | ||
features: { | ||
enabled: 'enabled', | ||
disabled: 'disabled', | ||
dynamic: 'dynamic' | ||
} | ||
}); | ||
it('provides a baseDir', function() { | ||
@@ -58,0 +83,0 @@ var expectedPath = path.join(__dirname, '..'); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
15616
9.48%54
8%492
13.1%0
-100%