-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure plugin option association (#9)
Previously the basename of the resolved plugin file would be used as the plugin name when applying options. However, that meant that if the file was called "index.js" in the plugin package, i.e. named "my-codemod", it was distributed with, the plugin options would have to be given to a plugin named "index" rather than "my-codemod".
- Loading branch information
1 parent
cee1915
commit 8ec00a6
Showing
5 changed files
with
132 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { deepEqual, strictEqual } from 'assert'; | ||
import { inspect } from 'util'; | ||
import Options, { ParseOptionsResult } from '../src/Options'; | ||
|
||
describe('Options', function() { | ||
it('has sensible defaults', function() { | ||
let options = assertOptionsParsed(Options.parse([])); | ||
deepEqual(options.extensions, new Set(['.js', '.jsx'])); | ||
deepEqual(options.pluginFilePaths, []); | ||
deepEqual(options.plugins, []); | ||
deepEqual(options.sourcePaths, []); | ||
deepEqual(options.requires, []); | ||
strictEqual(options.pluginOptions.size, 0); | ||
|
@@ -22,11 +23,6 @@ describe('Options', function() { | |
strictEqual(error.message, 'unexpected option: --wtf'); | ||
}); | ||
|
||
it('allows existing file paths as plugins', function() { | ||
let options = assertOptionsParsed(Options.parse(['--plugin', __filename])); | ||
deepEqual(options.pluginFilePaths, [__filename]); | ||
}); | ||
|
||
it('interprets non-option arguments as paths', function() { | ||
let options = assertOptionsParsed(Options.parse(['src/', 'a.js'])); | ||
deepEqual(options.sourcePaths, ['src/', 'a.js']); | ||
|
@@ -47,6 +43,46 @@ describe('Options', function() { | |
deepEqual(options.pluginOptions.get('my-plugin'), { foo: true }); | ||
}); | ||
|
||
it('associates plugin options based on declared name', function() { | ||
let options = assertOptionsParsed(Options.parse([ | ||
'--plugin', | ||
'./test/fixtures/plugin/index.js', | ||
'--plugin-options', | ||
'basic-plugin={"a": true}' | ||
])); | ||
|
||
// "basic-plugin" is declared in the plugin file | ||
deepEqual(options.pluginOptions.get('basic-plugin'), { a: true }); | ||
|
||
let babelPlugin = options.getBabelPlugin('basic-plugin'); | ||
|
||
if (!Array.isArray(babelPlugin)) { | ||
throw new Error(`expected plugin to be [plugin, options] tuple: ${inspect(babelPlugin)}`); | ||
} | ||
|
||
deepEqual(babelPlugin[1], { a: true }); | ||
}); | ||
|
||
it('associates plugin options based on inferred name', function() { | ||
let options = assertOptionsParsed(Options.parse([ | ||
'--plugin', | ||
'./test/fixtures/plugin/index.js', | ||
'--plugin-options', | ||
'index={"a": true}' | ||
])); | ||
|
||
// "index" is the name of the file | ||
deepEqual(options.pluginOptions.get('index'), { a: true }); | ||
|
||
let babelPlugin = options.getBabelPlugin('index'); | ||
|
||
if (!Array.isArray(babelPlugin)) { | ||
throw new Error(`expected plugin to be [plugin, options] tuple: ${inspect(babelPlugin)}`); | ||
} | ||
|
||
deepEqual(babelPlugin[1], { a: true }); | ||
}); | ||
|
||
it('can parse a JSON file for plugin options', function() { | ||
// You wouldn't actually use package.json, but it's a convenient JSON file. | ||
let options = assertOptionsParsed(Options.parse(['-o', '[email protected]'])); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = function() { | ||
return { | ||
name: 'basic-plugin', | ||
visitor: {} | ||
} | ||
}; |