This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
199 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env", "flow"] | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-s2s-initial-state-creater/__fixtures__/reducer-no-state.js
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,5 @@ | ||
// @flow | ||
type Count = number | ||
type State = { count: Count, isLoading: boolean } | ||
|
||
const initState = {} |
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-s2s-initial-state-creater/__fixtures__/reducer.js
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 @@ | ||
// @flow | ||
type Count = number | ||
type State = { count: Count, isLoading: boolean } | ||
|
||
// $FlowFixMe | ||
const initialState: State = {} |
29 changes: 29 additions & 0 deletions
29
packages/babel-plugin-s2s-initial-state-creater/package.json
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,29 @@ | ||
{ | ||
"name": "babel-plugin-s2s-initial-state-creater", | ||
"version": "0.0.0", | ||
"description": "create initial state from flow info", | ||
"license": "MIT", | ||
"repository": "akameco/s2s-plugins", | ||
"author": { | ||
"name": "akameco", | ||
"email": "akameco.t@gmail.com", | ||
"url": "akameco.github.io" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"prepare": "npm run build", | ||
"build": "babel src --ignore *.test.js -d lib " | ||
}, | ||
"main": "lib/index.js", | ||
"files": ["lib"], | ||
"dependencies": { | ||
"babylon": "^6.18.0", | ||
"babel-plugin-syntax-flow": "^6.18.0", | ||
"babel-types": "^6.26.0", | ||
"s2s-babel-flow-types": "^0.2.0", | ||
"s2s-utils": "^0.3.0", | ||
"flow-faker": "^0.0.0" | ||
} | ||
} |
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,53 @@ | ||
# babel-plugin-s2s-initial-state-creater | ||
|
||
> s2s plugin: create initial-state from flow info | ||
|
||
## Install | ||
|
||
``` | ||
$ npm install --save-dev babel-plugin-s2s-initial-state-creater | ||
``` | ||
|
||
### Example | ||
|
||
#### IN: | ||
|
||
```js | ||
// @flow | ||
type Count = number | ||
type State = { count: Count, isLoading: boolean } | ||
|
||
const initialState: State = {} | ||
``` | ||
|
||
|
||
#### OUT: | ||
|
||
```js | ||
// @flow | ||
type Count = number; | ||
type State = { count: Count, isLoading: boolean }; | ||
|
||
const initialState: State = { count: 0, isLoading: false }; | ||
``` | ||
|
||
|
||
### Usage | ||
|
||
``` | ||
{ | ||
['s2s-initial-state-creater'] | ||
} | ||
``` | ||
|
||
### Options | ||
|
||
#### from | ||
|
||
type: `string` <br> | ||
required: true | ||
|
||
Set target.js path | ||
|
||
If you use `s2s`, you **don't** use this option. s2s handle it automatically. |
39 changes: 39 additions & 0 deletions
39
packages/babel-plugin-s2s-initial-state-creater/src/__snapshots__/index.test.js.snap
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,39 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`initialStateがある場合 1`] = ` | ||
" | ||
// @flow | ||
type Count = number | ||
type State = { count: Count, isLoading: boolean } | ||
// $FlowFixMe | ||
const initialState: State = {} | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
// @flow | ||
type Count = number; | ||
type State = { count: Count; isLoading: boolean; }; | ||
// $FlowFixMe | ||
const initialState: State = { \\"count\\": 0, \\"isLoading\\": false }; | ||
" | ||
`; | ||
|
||
exports[`initialStateがない場合 1`] = ` | ||
" | ||
// @flow | ||
type Count = number | ||
type State = { count: Count, isLoading: boolean } | ||
const initState = {} | ||
↓ ↓ ↓ ↓ ↓ ↓ | ||
// @flow | ||
type Count = number; | ||
type State = { count: Count; isLoading: boolean; }; | ||
const initState = {}; | ||
" | ||
`; |
40 changes: 40 additions & 0 deletions
40
packages/babel-plugin-s2s-initial-state-creater/src/index.js
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,40 @@ | ||
// @flow | ||
import { parseExpression } from 'babylon' | ||
import { inheritsOpts } from 's2s-utils' | ||
import { flowFakerSync } from 'flow-faker' | ||
import type { Path, State } from 's2s-babel-flow-types' | ||
|
||
const INITIAL_STATE = 'initialState' | ||
|
||
export default () => { | ||
return { | ||
inherits: inheritsOpts, | ||
name: 's2s-initial-state-creater', | ||
visitor: { | ||
Program(programPath: Path, state: State) { | ||
const { opts: { from } } = state | ||
programPath.traverse({ | ||
VariableDeclarator(nodePath) { | ||
const idPath = nodePath.get('id') | ||
|
||
if (idPath.node.name !== INITIAL_STATE) { | ||
return | ||
} | ||
|
||
const { line, column } = nodePath.get('loc').get('start').node | ||
const flowInfo = flowFakerSync(from, { | ||
row: line, | ||
column: column + 1, | ||
}) | ||
|
||
const newAST = parseExpression(JSON.stringify(flowInfo), { | ||
plugins: ['flow'], | ||
}) | ||
|
||
nodePath.get('init').replaceWith(newAST) | ||
}, | ||
}) | ||
}, | ||
}, | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/babel-plugin-s2s-initial-state-creater/src/index.test.js
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,24 @@ | ||
// @flow | ||
import path from 'path' | ||
import pluginTester from 'babel-plugin-tester' | ||
import plugin from '.' | ||
|
||
const testWithFixture = (title, fixture) => { | ||
pluginTester({ | ||
plugin, | ||
snapshot: true, | ||
pluginOptions: { from: fixture }, | ||
tests: [ | ||
{ | ||
title, | ||
fixture, | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
const getFixturePath = (...x) => | ||
path.resolve(__dirname, '..', '__fixtures__', ...x) | ||
|
||
testWithFixture('initialStateがある場合', getFixturePath('reducer.js')) | ||
testWithFixture('initialStateがない場合', getFixturePath('reducer-no-state.js')) |