-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass current path to enter/leave callbacks
- Loading branch information
1 parent
283fdfe
commit b582cc7
Showing
5 changed files
with
960 additions
and
47 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,7 @@ | ||
language: node_js | ||
node_js: | ||
- "node" | ||
notifications: | ||
email: false | ||
script: | ||
- npm test |
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,81 @@ | ||
import assert from 'assert' | ||
|
||
import traverse from '../traverse' | ||
|
||
describe('traverse', () => { | ||
it('should pass the current path to enter and leave', () => { | ||
const ast = { | ||
type: 'Apply', | ||
op: 'add', | ||
args: [{ | ||
type: 'Number', | ||
value: '5', | ||
}, { | ||
type: 'Identifier', | ||
name: 'a', | ||
}] | ||
} | ||
|
||
let enterPath = [] | ||
let leavePath = [] | ||
|
||
traverse(ast, { | ||
enter(node, path) { | ||
if (node.value === '5') { | ||
enterPath = path | ||
} | ||
}, | ||
leave(node, path) { | ||
if (node.name === 'a') { | ||
leavePath = path | ||
} | ||
} | ||
}) | ||
|
||
assert.deepEqual(enterPath, ['args', 0]) | ||
assert.deepEqual(leavePath, ['args', 1]) | ||
}) | ||
|
||
it('should pass the current path to enter and leave', () => { | ||
const ast = { | ||
type: 'Apply', | ||
op: 'add', | ||
args: [{ | ||
type: 'Apply', | ||
op: 'add', | ||
args: [ | ||
{ | ||
type: 'Number', | ||
value: '5', | ||
}, | ||
{ | ||
type: 'Number', | ||
value: '10', | ||
}, | ||
], | ||
}, { | ||
type: 'Identifier', | ||
name: 'a', | ||
}] | ||
} | ||
|
||
let enterPath = [] | ||
let leavePath = [] | ||
|
||
traverse(ast, { | ||
enter(node, path) { | ||
if (node.value === '5') { | ||
enterPath = path | ||
} | ||
}, | ||
leave(node, path) { | ||
if (node.value === '10') { | ||
leavePath = path | ||
} | ||
} | ||
}) | ||
|
||
assert.deepEqual(enterPath, ['args', 0, 'args', 0]) | ||
assert.deepEqual(leavePath, ['args', 0, 'args', 1]) | ||
}) | ||
}) |
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,11 @@ | ||
{ | ||
"name": "math-traverse", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Library for traversing and replacing nodes in a math-ast AST", | ||
"main": "dist/math-traverse.js", | ||
"scripts": { | ||
"prepublish": "webpack", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest" | ||
}, | ||
"repository": "git+https://github.com/semantic-math/math-traverse.git", | ||
"author": "Kevin Barabash <[email protected]>", | ||
|
@@ -16,8 +16,17 @@ | |
"homepage": "https://github.com/semantic-math/math-traverse#readme", | ||
"devDependencies": { | ||
"babel-core": "^6.24.1", | ||
"babel-jest": "^20.0.3", | ||
"babel-loader": "^7.0.0", | ||
"babel-polyfill": "^6.23.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"jest": "^20.0.4", | ||
"webpack": "^2.4.1" | ||
}, | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "<rootDir>/node_modules/babel-polyfill/dist/polyfill.js", | ||
"transform": { | ||
"^.+\\.jsx?$": "babel-jest" | ||
} | ||
} | ||
} |
Oops, something went wrong.