flow-remove-types
Advanced tools
Comparing version 1.0.1 to 1.0.2
24
index.js
@@ -6,4 +6,8 @@ var babylon = require('babylon'); | ||
* which has removed those types. | ||
* | ||
* Options: | ||
* | ||
* - checkPragma: (default: true) looks for an @flow pragma before parsing. | ||
*/ | ||
module.exports = function flowRemoveTypes(source) { | ||
module.exports = function flowRemoveTypes(source, options) { | ||
// If there's no @flow or @noflow flag, then expect no annotation. | ||
@@ -15,3 +19,3 @@ var pragmaStart = source.indexOf('@flow'); | ||
pragmaEnd = pragmaStart + 7; | ||
if (pragmaStart === -1) { | ||
if (pragmaStart === -1 && !(options && options.checkPragma === false)) { | ||
return source; | ||
@@ -21,3 +25,5 @@ } | ||
var removedNodes = [ { start: pragmaStart, end: pragmaEnd } ]; | ||
var removedNodes = pragmaStart === -1 ? | ||
[] : | ||
[ { start: pragmaStart, end: pragmaEnd } ]; | ||
@@ -36,2 +42,6 @@ // Babylon is one of the sources of truth for Flow syntax. This parse | ||
if (removedNodes.length === 0) { | ||
return source; | ||
} | ||
var result = ''; | ||
@@ -80,3 +90,3 @@ var lastPos = 0; | ||
Identifier(removedNodes, node, ast) { | ||
Identifier: function (removedNodes, node, ast) { | ||
if (node.optional) { | ||
@@ -92,3 +102,3 @@ // Find the optional token. | ||
ClassProperty(removedNodes, node) { | ||
ClassProperty: function (removedNodes, node) { | ||
if (!node.value) { | ||
@@ -99,3 +109,3 @@ return removeNode(removedNodes, node) | ||
ExportNamedDeclaration(removedNodes, node) { | ||
ExportNamedDeclaration: function (removedNodes, node) { | ||
if (node.exportKind === 'type') { | ||
@@ -106,3 +116,3 @@ return removeNode(removedNodes, node); | ||
ImportDeclaration(removedNodes, node) { | ||
ImportDeclaration: function (removedNodes, node) { | ||
if (node.importKind === 'type') { | ||
@@ -109,0 +119,0 @@ return removeNode(removedNodes, node); |
{ | ||
"name": "flow-remove-types", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Removes Flow type annotations from JavaScript files with speed and simplicity.", | ||
@@ -8,3 +8,6 @@ "author": "Lee Byron <lee@leebyron.com> (http://leebyron.com/)", | ||
"main": "index.js", | ||
"bin": "flow-remove-types", | ||
"bin": { | ||
"flow-remove-types": "./flow-remove-types", | ||
"flow-node": "./flow-node" | ||
}, | ||
"homepage": "https://github.com/leebyron/flow-remove-types", | ||
@@ -19,3 +22,3 @@ "bugs": { | ||
"scripts": { | ||
"test": "DIFF=$(./flow-remove-types test/source.js | diff test/expected.js -); if [ -n \"$DIFF\" ]; then echo \"$DIFF\"; exit 1; fi;", | ||
"test": "DIFF=$(./flow-remove-types test/source.js | diff test/expected.js -); if [ -n \"$DIFF\" ]; then echo \"$DIFF\"; exit 1; fi; RES=$(node -e 'require(\"./register\");require(\"./test/test-node-module.js\")'); if [ \"$RES\" != 42 ]; then echo 'Node register hook failed'; exit 1; fi; FLOW_NODE=$(./flow-node ./test/test-node-module.js); if [ \"$FLOW_NODE\" != 42 ]; then echo 'flow-node failed'; exit 1; fi;", | ||
"test-update": "./flow-remove-types test/source.js > test/expected.js" | ||
@@ -22,0 +25,0 @@ }, |
flow-remove-types | ||
================= | ||
[![npm](https://img.shields.io/npm/v/flow-remove-types.svg?maxAge=86400)](https://www.npmjs.com/package/flow-remove-types) | ||
[![Build Status](https://img.shields.io/travis/leebyron/flow-remove-types.svg?style=flat&label=travis&branch=master)](https://travis-ci.org/leebyron/flow-remove-types) | ||
Turn your JavaScript with [Flow](https://flowtype.org/) type annotations into | ||
@@ -20,2 +23,3 @@ standard JavaScript in an instant with no configuration and minimal setup. | ||
## Get Started! | ||
@@ -49,2 +53,35 @@ | ||
## Use in Build Systems: | ||
**Rollup**: [`rollup-plugin-flow`](https://github.com/leebyron/rollup-plugin-flow) | ||
**Browserify:** [`unflowify`](https://github.com/leebyron/unflowify) | ||
## Use `flow-node` | ||
Wherever you use `node` you can substitute `flow-node` and have a super fast | ||
flow-types aware evaluator or REPL. | ||
``` | ||
$ flow-node | ||
> var x: number = 42 | ||
undefined | ||
> x | ||
42 | ||
``` | ||
## Use the require hook | ||
Using the require hook allows you to automatically compile files on the fly when | ||
requiring in node: | ||
```js | ||
require('flow-remove-types/register') | ||
require('./some-module-with-flow-type-syntax') | ||
``` | ||
## Dead-Simple Transforms | ||
@@ -106,4 +143,51 @@ | ||
## Use in Build Systems: | ||
**Rollup**: [`rollup-plugin-flow`](https://github.com/leebyron/rollup-plugin-flow) | ||
## Performance | ||
### Install: | ||
Installing via `npm` from an empty project: | ||
**flow-remove-types:** | ||
``` | ||
time npm install flow-remove-types | ||
real 0m3.193s | ||
user 0m1.643s | ||
sys 0m0.775s | ||
``` | ||
**Babel:** | ||
``` | ||
time npm install babel-cli babel-plugin-transform-flow-strip-types | ||
real 0m23.200s | ||
user 0m10.395s | ||
sys 0m4.238s | ||
``` | ||
### Transform: | ||
Transforming a directory of 20 files of 100 lines each: | ||
**flow-remove-types:** | ||
``` | ||
time flow-remove-types src/ --out-dir dest/ | ||
real 0m0.431s | ||
user 0m0.436s | ||
sys 0m0.068s | ||
``` | ||
**Babel:** | ||
``` | ||
time babel src/ --out-dir dest/ | ||
real 0m1.074s | ||
user 0m1.092s | ||
sys 0m0.149s | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24562
11
381
191
1