Skip to content
This repository was archived by the owner on Aug 24, 2019. It is now read-only.

Commit 1028f04

Browse files
authored
Merge pull request #5 from smooth-code/support-state
feat(h2x-core): add state support
2 parents 3447d97 + 1af6866 commit 1028f04

File tree

8 files changed

+526
-385
lines changed

8 files changed

+526
-385
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ node_js:
55
- 8
66

77
before_install:
8-
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.28.4
8+
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.3.2
99
- export PATH="$HOME/.yarn/bin:$PATH"
1010

1111
script:

.yarnrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@
1515
"devDependencies": {
1616
"babel-cli": "^6.26.0",
1717
"babel-core": "^6.26.0",
18-
"babel-eslint": "^7.2.3",
18+
"babel-eslint": "^8.0.3",
1919
"babel-plugin-transform-class-properties": "^6.24.1",
2020
"babel-plugin-transform-object-rest-spread": "^6.26.0",
2121
"babel-preset-env": "^1.6.0",
2222
"chalk": "^2.1.0",
23-
"codecov": "^2.3.0",
24-
"eslint": "^4.4.1",
25-
"eslint-config-airbnb-base": "^11.3.1",
26-
"eslint-config-prettier": "^2.3.0",
23+
"codecov": "^3.0.0",
24+
"eslint": "^4.12.1",
25+
"eslint-config-airbnb-base": "^12.1.0",
26+
"eslint-config-prettier": "^2.9.0",
2727
"eslint-plugin-import": "^2.7.0",
2828
"glob": "^7.1.2",
29-
"jest": "^20.0.4",
30-
"lerna": "^2.1.0",
31-
"lerna-tools": "^0.0.2",
29+
"jest": "^21.2.1",
30+
"lerna": "^2.5.1",
31+
"lerna-tools": "^0.0.3",
3232
"micromatch": "^3.0.4",
3333
"mkdirp": "^0.5.1",
34+
"prettier": "^1.9.1",
3435
"string-length": "^2.0.0"
3536
},
3637
"workspaces": [

packages/h2x-core/src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import parse from 'h2x-parse'
33
import generate from 'h2x-generate'
44
import traverse from 'h2x-traverse'
55

6-
76
const reduceVisitors = (visitors, opts) => {
87
Object.keys(visitors).forEach(key => {
98
const visitor = visitors[key]
@@ -31,9 +30,9 @@ const mergePlugins = plugins =>
3130

3231
export { traverse, generate }
3332

34-
export function transform(code, { plugins = [] } = {}) {
33+
export function transform(code, { plugins = [], state = {} } = {}) {
3534
const ast = parse(code)
3635
const { visitor, generator } = mergePlugins(plugins)
37-
traverse(ast, visitor)
36+
traverse(ast, visitor, state)
3837
return generate(ast, generator)
3938
}

packages/h2x-core/src/index.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,37 @@ describe('transform', () => {
3131
const result = transform(code, { plugins: [simpleJsx] })
3232
expect(result).toBe(`<div />\n`)
3333
})
34+
35+
it('should support state', () => {
36+
class JSXNode {
37+
static [NODE_TYPE] = 'JSXElement'
38+
39+
constructor(name) {
40+
this.name = name
41+
}
42+
}
43+
44+
const simpleJsx = () => ({
45+
visitor: {
46+
HTMLElement: {
47+
enter(path, state) {
48+
state.tags.push(path.node.tagName)
49+
path.replace(new JSXNode('div'))
50+
},
51+
},
52+
},
53+
generator: {
54+
JSXElement: {
55+
enter(path, generator) {
56+
generator.writeLine(`<${path.node.name} />`)
57+
},
58+
},
59+
},
60+
})
61+
const state = { tags: [] }
62+
const code = `<div></div>`
63+
const result = transform(code, { plugins: [simpleJsx], state })
64+
expect(result).toBe(`<div />\n`)
65+
expect(state.tags).toEqual(['DIV'])
66+
})
3467
})

packages/h2x-plugin-jsx/src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @returns {boolean}
66
*/
77
export function isNumeric(value) {
8-
return !isNaN(value - parseFloat(value))
8+
return !Number.isNaN(value - parseFloat(value))
99
}
1010

1111
/**

packages/h2x-traverse/src/NodePath.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class NodePath {
4040
}
4141

4242
call(key) {
43-
const opts = this.opts
43+
const { opts } = this
4444

4545
if (this.node) {
4646
if (this._call(opts[key])) return true
@@ -90,7 +90,7 @@ class NodePath {
9090
for (const fn of fns) {
9191
if (!fn) continue
9292

93-
const node = this.node
93+
const { node } = this
9494
if (!node) return true
9595

9696
const ret = fn(this, this.state)

0 commit comments

Comments
 (0)