flow-remove-types
Advanced tools
Comparing version 1.2.1 to 1.2.2
58
index.js
@@ -45,19 +45,2 @@ var babylon = require('babylon'); | ||
var removedNodes = []; | ||
// Remove the flow pragma. | ||
if (pragmaStart !== -1) { | ||
var prePragmaLines = source.slice(0, pragmaStart).split('\n'); | ||
var pragmaLine = prePragmaLines.length; | ||
var pragmaCol = prePragmaLines[pragmaLine - 1].length; | ||
removedNodes.push({ | ||
start: pragmaStart, | ||
end: pragmaStart + pragmaSize, | ||
loc: { | ||
start: { line: pragmaLine, column: pragmaCol }, | ||
end: { line: pragmaLine, column: pragmaCol + pragmaSize } | ||
}, | ||
}) | ||
} | ||
// Babylon is one of the sources of truth for Flow syntax. This parse | ||
@@ -73,2 +56,4 @@ // configuration is intended to be as permissive as possible. | ||
var removedNodes = []; | ||
var context = { | ||
@@ -81,2 +66,12 @@ ast: ast, | ||
// Remove the flow pragma. | ||
if (pragmaStart !== -1) { | ||
var pragmaIdx = findTokenIndex(ast.tokens, pragmaStart); | ||
var pragmaType = ast.tokens[pragmaIdx].type; | ||
if (pragmaType === 'CommentLine' || pragmaType === 'CommentBlock') { | ||
removedNodes.push(getPragmaNode(context, pragmaStart, pragmaSize)); | ||
} | ||
} | ||
// Remove all flow type definitions. | ||
visit(ast, context, removeFlowVisitor); | ||
@@ -259,2 +254,31 @@ | ||
function getPragmaNode(context, start, size) { | ||
var source = context.source; | ||
var line = 1; | ||
var column = 0; | ||
for (var position = 0; position < start; position++) { | ||
var char = source[position]; | ||
if (char === '\n') { | ||
line++; | ||
column = 0; | ||
} else if (char === '\r') { | ||
if (source[position + 1] === '\n') { | ||
position++; | ||
} | ||
line++; | ||
column = 0; | ||
} else { | ||
column++; | ||
} | ||
} | ||
return { | ||
start: start, | ||
end: start + size, | ||
loc: { | ||
start: { line: line, column: column }, | ||
end: { line: line, column: column + size }, | ||
}, | ||
}; | ||
} | ||
function getLeadingSpaceNode(context, node) { | ||
@@ -261,0 +285,0 @@ var source = context.source; |
{ | ||
"name": "flow-remove-types", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Removes Flow type annotations from JavaScript files with speed and simplicity.", | ||
@@ -5,0 +5,0 @@ "author": "Lee Byron <lee@leebyron.com> (http://leebyron.com/)", |
@@ -52,2 +52,3 @@ flow-remove-types | ||
When using the `flow-remove-types` script, be sure [not to direct the output to itself](https://superuser.com/questions/597244/why-does-redirecting-the-output-of-a-file-to-itself-produce-a-blank-file)! | ||
@@ -62,3 +63,5 @@ ## Use in Build Systems: | ||
**Gulp:** [`gulp-flow-remove-types`](https://github.com/wain-pc/gulp-flow-remove-types) | ||
## Use with existing development tools | ||
@@ -65,0 +68,0 @@ |
@@ -22,3 +22,3 @@ var flowRemoveTypes = require('./index'); | ||
var jsLoader = require.extensions['.js']; | ||
var exts = [ '.js', '.jsx', '.flow', '.es6' ]; | ||
var exts = [ '.js', '.mjs', '.jsx', '.flow', '.es6' ]; | ||
exts.forEach(function (ext) { | ||
@@ -25,0 +25,0 @@ var superLoader = require.extensions[ext] || jsLoader; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38156
485
243