babel-plugin-jsdoc-closure
Advanced tools
Comparing version 1.4.2 to 1.5.0
58
index.js
@@ -114,14 +114,18 @@ const parseComment = require('comment-parser'); | ||
if (typedef) { | ||
const closureTypedef = type ? JSON.stringify(type).replace(/"/g, '') : typedef.type; | ||
let addLines = comment.value.split('\n').length - 1; | ||
newComment = typedef.source.replace(/(@typedef\s*){[^}]+}\s.*/, `$1{${closureTypedef}}`); | ||
newComment = `* ${newComment}`; | ||
if (typedef.name) { | ||
addLines--; | ||
typedefExport = `export let ${typedef.name};\n`; | ||
if (type) { | ||
const closureTypedef = [`export function ${typedef.name}() {};`]; | ||
for (const name in type) { | ||
closureTypedef.push(`/** @type {${type[name]}} */ ${typedef.name}.prototype.${name};`); | ||
} | ||
typedefExport = closureTypedef.join('\n'); | ||
newComment = '* @interface '; | ||
} else { | ||
const closureTypedef = typedef.type; | ||
if (typedef.name) { | ||
typedefExport = `export let ${typedef.name};\n`; | ||
} | ||
newComment = typedef.source.replace(/(@typedef\s*){[^}]+}\s.*/, `$1{${closureTypedef}}`); | ||
newComment = `* ${newComment}`; | ||
newComment += ' '; | ||
} | ||
while (addLines--) { | ||
newComment += '\n' + (addLines >= 1 ? ' *' : ''); | ||
} | ||
newComment += ' '; | ||
} | ||
@@ -157,13 +161,23 @@ return [newComment, typedefExport]; | ||
const program = babel.transform(typedefExport).ast.program; | ||
const newNode = program.body[0]; | ||
newNode[commentsProperty] = comments.splice(0, i + 1); | ||
newNode[commentsProperty].forEach(comment => { | ||
comment.leading = true; | ||
}); | ||
i = -1; | ||
ii = comments.length; | ||
if (node.type != 'Program') { | ||
path.insertBefore(newNode); | ||
} else { | ||
path.parent.program.body.push(newNode); | ||
for (let j = 0, jj = program.body.length; j < jj; ++j) { | ||
const newNode = program.body[j]; | ||
if (j == 0) { | ||
newNode[commentsProperty] = comments.splice(0, i + 1); | ||
newNode[commentsProperty].forEach(comment => { | ||
comment.leading = true; | ||
}); | ||
i = -1; | ||
ii = comments.length; | ||
} else if (newNode.leadingComments && commentsProperty !== 'leadingComments') { | ||
newNode[commentsProperty] = newNode.leadingComments; | ||
newNode[commentsProperty].forEach(comment => { | ||
comment.leading = true; | ||
}); | ||
} | ||
if (node.type != 'Program') { | ||
path.insertBefore(newNode); | ||
} else { | ||
path.parent.program.body.push(newNode); | ||
} | ||
} | ||
@@ -170,0 +184,0 @@ typedefExport = undefined; |
{ | ||
"name": "babel-plugin-jsdoc-closure", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Transpiles JSDoc types from namepaths to types for Closure Compiler", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -139,5 +139,1 @@ # babel-plugin-jsdoc-closure | ||
Properties marked as optional with JSDoc notation are also handled. The plugin will transforms `@property {number} [foo] Foo.` or `@property {number=} foo Foo.` to `foo: (undefined|number)`. | ||
### Notes | ||
To avoid the need for source maps, line numbers are retained by this plugin. This is the reason why the `require()` assignments are added at the bottom of each file. |
@@ -24,3 +24,3 @@ const babel = require('babel-core'); | ||
let got = babel.transform(source, filename ? Object.assign({filename}, recastOptions) : recastOptions); | ||
assert.equal(got.code, expected); | ||
assert.equal(got.code, expected.replace(/\(\) \{\};/g, '() {}')); | ||
got = babel.transform(source, filename ? Object.assign({filename}, options) : options); | ||
@@ -43,3 +43,3 @@ assert.equal(got.code.replace(/[\n\s]+/g, ''), expected.replace(/[\n\s]+/g, '')); | ||
it('transforms a type cast with an imported module in a return statement', function() { | ||
// requires recast, and causes line break changes unfortunately | ||
// requires recast | ||
const source = | ||
@@ -61,3 +61,3 @@ '/** @module module2/types */\n' + | ||
it('transforms an object property type cast with an imported module in a return statement', function() { | ||
// requires recast, and causes line break changes unfortunately | ||
// requires recast | ||
const source = | ||
@@ -212,4 +212,3 @@ '/** @module module2/types */\n' + | ||
'/** @module module2/types */\n' + | ||
'/** @typedef {number}\n' + | ||
' */\n' + | ||
'/** @typedef {number} */\n' + | ||
'export let Foo;', | ||
@@ -220,9 +219,6 @@ './test/module2/types.js' | ||
it('modifies Object typedefs', function() { | ||
it('turns Object typedefs into structural interfaces', function() { | ||
test( | ||
'/** @module module2/types */\n' + | ||
'/**\n' + | ||
' * @typedef {number} Bar\n' + | ||
' */\n' + | ||
'/**\n' + | ||
' * @typedef {Object}\n' + | ||
@@ -234,11 +230,8 @@ ' * Foo\n' + | ||
'/** @module module2/types */\n' + | ||
'/** @typedef {number}\n' + | ||
' */\n' + | ||
'export let Bar;\n\n' + | ||
'/** @typedef {{bar:(!../module1/Bar),baz:(undefined|number)}}\n' + | ||
' *\n' + | ||
' *\n' + | ||
' *\n' + | ||
' */\n' + | ||
'export let Foo;', | ||
'/** @interface */\n' + | ||
'export function Foo() {};\n\n' + | ||
'/** @type {(!../module1/Bar)} */\n' + | ||
'Foo.prototype.bar;\n\n' + | ||
'/** @type {(undefined|number)} */\n' + | ||
'Foo.prototype.baz;', | ||
'./test/module2/types.js' | ||
@@ -245,0 +238,0 @@ ); |
21640
458
139