Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Add s2s-initial-state-creater
Browse files Browse the repository at this point in the history
  • Loading branch information
akameco committed Oct 19, 2017
1 parent 9b45b22 commit f370f99
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/babel-plugin-s2s-initial-state-creater/.babelrc
@@ -0,0 +1,3 @@
{
"presets": ["env", "flow"]
}
@@ -0,0 +1,5 @@
// @flow
type Count = number
type State = { count: Count, isLoading: boolean }

const initState = {}
@@ -0,0 +1,6 @@
// @flow
type Count = number
type State = { count: Count, isLoading: boolean }

// $FlowFixMe
const initialState: State = {}
29 changes: 29 additions & 0 deletions packages/babel-plugin-s2s-initial-state-creater/package.json
@@ -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"
}
}
53 changes: 53 additions & 0 deletions packages/babel-plugin-s2s-initial-state-creater/readme.md
@@ -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.
@@ -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 packages/babel-plugin-s2s-initial-state-creater/src/index.js
@@ -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 packages/babel-plugin-s2s-initial-state-creater/src/index.test.js
@@ -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'))

0 comments on commit f370f99

Please sign in to comment.