🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

babel-plugin-flow-comments

Package Overview
Dependencies
Maintainers
6
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-flow-comments - npm Package Compare versions

Comparing version
1.0.9
to
6.3.19
+35
-25
lib/index.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.__esModule = true;
exports["default"] = function (_ref) {
var Plugin = _ref.Plugin;
var t = _ref.types;
function wrapInFlowComment(context, parent) {
context.addComment("trailing", generateComment(context, parent));
context.replaceWith(t.noop());
function wrapInFlowComment(path, parent) {
path.addComment("trailing", generateComment(path, parent));
path.replaceWith(t.noop());
}
function generateComment(context, parent) {
var comment = context.getSource().replace(/\*-\//g, "*-ESCAPED/").replace(/\*\//g, "*-/");
function generateComment(path, parent) {
var comment = path.getSource().replace(/\*-\//g, "*-ESCAPED/").replace(/\*\//g, "*-/");
if (parent && parent.optional) comment = "?" + comment;

@@ -23,15 +20,21 @@ if (comment[0] !== ":") comment = ":: " + comment;

return new Plugin("flow-comments", {
return {
inherits: require("babel-plugin-syntax-flow"),
visitor: {
TypeCastExpression: function TypeCastExpression(node) {
this.get("expression").addComment("trailing", generateComment(this.get("typeAnnotation")));
this.replaceWith(t.parenthesizedExpression(node.expression));
TypeCastExpression: function TypeCastExpression(path) {
var node = path.node;
path.get("expression").addComment("trailing", generateComment(path.get("typeAnnotation")));
path.replaceWith(t.parenthesizedExpression(node.expression));
},
// support function a(b?) {}
Identifier: function Identifier(node, parent, scope, file) {
Identifier: function Identifier(path) {
var node = path.node;
if (!node.optional || node.typeAnnotation) {
return;
}
this.addComment("trailing", ":: ?");
path.addComment("trailing", ":: ?");
},

@@ -41,7 +44,8 @@

Function: {
exit: function exit(node) {
for (var i = 0; i < node.params.length; i++) {
var param = node.params[i];
param.optional = false;
}
exit: function exit(_ref2) {
var node = _ref2.node;
node.params.forEach(function (param) {
return param.optional = false;
});
}

@@ -51,20 +55,26 @@ },

// support `export type a = {}` - #8 Error: You passed path.replaceWith() a falsy node
"ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(node, parent, scope, file) {
"ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(path) {
var node = path.node;
var parent = path.parent;
if (t.isExportNamedDeclaration(node) && !t.isFlow(node.declaration)) {
return;
}
wrapInFlowComment(this, parent);
wrapInFlowComment(path, parent);
},
// support `import type A` and `import typeof A` #10
ImportDeclaration: function ImportDeclaration(node, parent, scope, file) {
ImportDeclaration: function ImportDeclaration(path) {
var node = path.node;
var parent = path.parent;
if (t.isImportDeclaration(node) && node.importKind !== "type" && node.importKind !== "typeof") {
return;
}
wrapInFlowComment(this, parent);
wrapInFlowComment(path, parent);
}
}
});
};
};
module.exports = exports["default"];
{
"name": "babel-plugin-flow-comments",
"version": "1.0.9",
"version": "6.3.19",
"description": "Turn flow type annotations into comments",
"repository": "babel-plugins/babel-plugin-flow-comments",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-flow-comments",
"license": "MIT",
"main": "lib/index.js",
"devDependencies": {
"babel": "^5.6.0",
"mocha": "^2.2.5"
},
"scripts": {
"build": "babel-plugin build",
"push": "babel-plugin publish",
"test": "babel-plugin test",
"test-mocha": "mocha --compilers js:babel/register"
},
"keywords": [
"babel-plugin"
]
],
"dependencies": {
"babel-runtime": "^5.0.0",
"babel-plugin-syntax-flow": "^6.3.13"
},
"devDependencies": {
"babel-helper-plugin-test-runner": "^6.3.13"
}
}

@@ -5,3 +5,3 @@ # babel-plugin-flow-comments

When using this plugin, you might want to also blacklist the `flow` transformer to preserve the `/* @flow */` directive.
You should be able to use this plugin instead of `babel-plugin-flow-strip-types` to preserve the `/* @flow */` directive and still use flow.

@@ -56,4 +56,3 @@ http://flowtype.org/blog/2015/02/20/Flow-Comments.html

{
"plugins": ["flow-comments"],
"blacklist": ["flow"]
"plugins": ["flow-comments"]
}

@@ -65,3 +64,3 @@ ```

```sh
$ babel --plugins flow-comments --blacklist flow script.js
$ babel --plugins flow-comments script.js
```

@@ -73,5 +72,4 @@

require("babel-core").transform("code", {
plugins: ["flow-comments"],
blacklist: ["flow"]
plugins: ["flow-comments"]
});
```
language: node_js
node_js:
- "0.12"
- "iojs"
script:
- "npm test-mocha"
export type GraphQLFormattedError = number;
export type GraphQLFormattedError = {
message: string,
locations?: Array<{
line: number,
column: number
}>
};
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
/*:: export type GraphQLFormattedError = number;*/
/*:: export type GraphQLFormattedError = {
message: string,
locations?: Array<{
line: number,
column: number
}>
};*/
import lib from 'library';
import type A, { B, C } from './types';
import typeof D, { E, F } from './types';
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _library = require('library');
var _library2 = _interopRequireDefault(_library);
/*:: import type A, { B, C } from './types';*/
/*:: import typeof D, { E, F } from './types';*/
function multiply(num?: number) {}
"use strict";
function multiply(num /*:: ?: number*/) {}
function foo(bar?) {}
function foo2(bar?: string) {}
"use strict";
function foo(bar /*:: ?*/) {}
function foo2(bar /*:: ?: string*/) {}
type T = /*test*/ number;
type T2 = /* *-/ */ number;
type CustomType = {
/** This is some documentation. */
name: string;
};
"use strict";
/*:: type T = /*test*-/ number;*/
/*:: type T2 = /* *-ESCAPED/ *-/ number;*/
/*:: type CustomType = {
/** This is some documentation. *-/
name: string;
};*/
function a() {}
type A = number;
type B = {
name: string;
};
"use strict";
function a() {}
/*:: type A = number;*/
/*:: type B = {
name: string;
};*/
var a = (y: any);
var a = ((y: foo): any);
"use strict";
var a = (y /*: any*/);
var a = ((y /*: foo*/) /*: any*/);
function foo(x: number): string {}
"use strict";
function foo(x /*: number*/) /*: string*/ {}
const path = require("path");
const fs = require("fs");
const assert = require("assert");
const babel = require("babel");
const plugin = require("../src/index");
function trim(str) {
return str.replace(/^\s+|\s+$/, "");
}
describe("emit flow comments from flow types", () => {
const fixturesDir = path.join(__dirname, "fixtures");
fs.readdirSync(fixturesDir).map((caseName) => {
it(`should ${caseName.split("-").join(" ")}`, () => {
const fixtureDir = path.join(fixturesDir, caseName);
const actual = babel.transformFileSync(
path.join(fixtureDir, "actual.js"), {
plugins: [plugin]
}
).code;
const expected = fs.readFileSync(path.join(fixtureDir, "expected.js")).toString();
assert.equal(trim(actual), trim(expected));
});
});
});

Sorry, the diff of this file is not supported yet