Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Jun 2, 2017
0 parents commit f94280b
Show file tree
Hide file tree
Showing 9 changed files with 2,441 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ignore]

[include]

[libs]

[options]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.log
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017-present James Kyle <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# babel-flow-scope

> Collect Flow bindings in a given scope
```js
import {getFlowBindingsInScope} from 'babel-flow-scope';

getFlowBindingsInScope(path);
// {
// foo: {
// kind: 'import',
// path: (Identifier)
// },
// bar: {
// kind: 'param',
// path: (TypeParameter)
// },
// baz: {
// kind: 'declaration',
// path: (Identifier)
// }
// }
```
212 changes: 212 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`1. import type 1`] = `
"
import type a from \\"mod\\";
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'import'
path: Path: Identifier (1:12,1:13)
name: 'a'
\`;
import type a from \\"mod\\";
"
`;

exports[`2. import {type} 1`] = `
"
import {type a} from \\"mod\\";
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'import'
path: Path: Identifier (1:13,1:14)
__clone: Function
name: 'a'
\`;
import { type a } from \\"mod\\";
"
`;

exports[`3. import typeof 1`] = `
"
import typeof {a} from \\"mod\\";
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'import'
path: Path: Identifier (1:15,1:16)
__clone: Function
name: 'a'
\`;
import typeof { a } from \\"mod\\";
"
`;

exports[`4. import {typeof} 1`] = `
"
import {typeof a} from \\"mod\\";
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'import'
path: Path: Identifier (1:15,1:16)
__clone: Function
name: 'a'
\`;
import { typeof a } from \\"mod\\";
"
`;

exports[`5. type alias 1`] = `
"
type a = {};
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'declaration'
path: Path: Identifier (1:5,1:6)
name: 'a'
\`;
type a = {};
\`{}
\`;
"
`;

exports[`6. interface declaration 1`] = `
"
interface a {}
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'declaration'
path: Path: Identifier (1:10,1:11)
name: 'a'
\`;
interface a {}
\`{}
\`;
"
`;

exports[`7. type alias params 1`] = `
"
type a<b> = {}
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'declaration'
path: Path: Identifier (1:5,1:6)
name: 'a'
\`;
type a<b> = {};
\`
b:
kind: 'param'
path: Path: TypeParameter (1:7,1:8)
bound: undefined
name: 'b'
variance: null
\`;
"
`;
exports[`8. interface params 1`] = `
"
interface a<b> {}
↓ ↓ ↓ ↓ ↓ ↓
\`
a:
kind: 'declaration'
path: Path: Identifier (1:10,1:11)
name: 'a'
\`;
interface a<b> {}
\`
b:
kind: 'param'
path: Path: TypeParameter (1:12,1:13)
bound: undefined
name: 'b'
variance: null
\`;
"
`;
exports[`9. class params 1`] = `
"
class a<b> {}
↓ ↓ ↓ ↓ ↓ ↓
\`{}
\`;
class a<b> {}
\`
b:
kind: 'param'
path: Path: TypeParameter (1:8,1:9)
bound: undefined
name: 'b'
variance: null
\`;
"
`;
exports[`10. function params 1`] = `
"
function a<b>() {}
↓ ↓ ↓ ↓ ↓ ↓
\`{}
\`;
function a<b>() {}
\`
b:
kind: 'param'
path: Path: TypeParameter (1:11,1:12)
bound: undefined
name: 'b'
variance: null
\`;
"
`;
exports[`11. nested 1`] = `
"
function a<b>() {}
↓ ↓ ↓ ↓ ↓ ↓
\`{}
\`;
function a<b>() {}
\`
b:
kind: 'param'
path: Path: TypeParameter (1:11,1:12)
bound: undefined
name: 'b'
variance: null
\`;
"
`;
68 changes: 68 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// @flow
'use strict';

/*::
type Node = {
type: string,
[key: string]: any,
};
type Path = {
type: string,
node: Node,
[key: string]: any,
};
type Bindings = {
[name: string]: {
kind: 'param' | 'hoisted' | 'import',
id: Node,
path: Path,
},
};
*/

let visitor = {
Scope(path) {
path.skip();
},

'InterfaceDeclaration|TypeAlias'(path, state) {
let id = path.get('id');
state.bindings[id.node.name] = {kind: 'declaration', path: id};
path.skip();
},

TypeParameter(path, state) {
state.bindings[path.node.name] = {kind: 'param', path};
},

'ImportSpecifier|ImportDefaultSpecifier'(path, state) {
let importKind = path.node.importKind || path.parent.importKind;
if (importKind !== 'type' && importKind !== 'typeof') return;
let local = path.get('local');
state.bindings[local.node.name] = {kind: 'import', path: local};
},
};

function getFlowScopePath(path /*: Path */) /*: Path */ {
return path.find(p => {
return (
p.isScope() ||
p.isTypeAlias() ||
p.isInterfaceDeclaration()
);
});
}

function getFlowBindingsInScope(path /*: Path */) /*: Bindings */ {
let scopePath = getFlowScopePath(path);
let bindings = {};

scopePath.traverse(visitor, { bindings });

return bindings;
}

exports.getFlowScopePath = getFlowScopePath;
exports.getFlowBindingsInScope = getFlowBindingsInScope;
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "babel-flow-scope",
"version": "0.0.0",
"description": "Collect Flow bindings in a given scope",
"main": "index.js",
"repository": "babel-utils/babel-flow-scope",
"author": "James Kyle <[email protected]>",
"license": "MIT",
"files": [],
"scripts": {
"test": "jest"
},
"devDependencies": {
"ast-pretty-print": "^1.3.0",
"babel-plugin-tester": "^3.0.0",
"babylon-options": "^1.1.2",
"flow-bin": "^0.47.0",
"jest": "^20.0.4"
}
}
Loading

0 comments on commit f94280b

Please sign in to comment.