babel-plugin-jsdoc-closure
Advanced tools
Comparing version 1.3.4 to 1.4.0
74
index.js
const parseComment = require('comment-parser'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
@@ -24,21 +25,21 @@ let babel, commentsProperty, imports, levelsUp, modulePath, recast, resourcePath; | ||
const typePath = (up || './') + (pathParts.length > 0 ? pathParts.join('/') + '/' : ''); | ||
const filePath = `${typePath}${namedParts[0]}`; | ||
if (namedParts.length > 1 && namedParts[1] != namedParts[0]) { | ||
return `const ${formatType(type)} = require('${typePath}${namedParts[0]}').${namedParts[1]};`; | ||
} else { | ||
return `const ${formatType(type)} = require('${typePath}${namedParts[0]}');`; | ||
} | ||
} | ||
function formatType(type) { | ||
type = type.replace(/module\:/, ''); | ||
const pathParts = type.split('/'); | ||
const name = pathParts.pop(); | ||
const namedParts = name.split(/[\.~]/); | ||
if (namedParts.length > 1) { | ||
return `${pathParts.join('_')}_${namedParts.join('_')}`; | ||
} else { | ||
return `${pathParts.join('$')}$${namedParts[0]}`; | ||
const dependency = require.resolve(path.resolve(path.dirname(resourcePath), filePath)); | ||
const content = babel.transform(fs.readFileSync(dependency, 'utf-8')); | ||
let defaultExportName; | ||
const candidates = content.ast.program.body; | ||
for (let i = 0, ii = candidates.length; i < ii; ++i) { | ||
const node = candidates[i]; | ||
if (node.type == 'ExportDefaultDeclaration') { | ||
defaultExportName = node.declaration.name; | ||
break; | ||
} | ||
} | ||
if (namedParts[1] != defaultExportName) { | ||
return `${filePath}.${namedParts[1]}`; | ||
} | ||
} | ||
return `${filePath}`; | ||
} | ||
@@ -63,6 +64,8 @@ | ||
} else { | ||
replacement = formatType(type); | ||
const importLine = formatImport(type); | ||
imports[importLine] = true; | ||
replacement = formatImport(type); | ||
} | ||
const lookup = path.resolve(path.dirname(resourcePath), replacement); | ||
if (lookup in imports) { | ||
replacement = imports[lookup]; | ||
} | ||
newComment = comment.value.replace(new RegExp(type, 'g'), replacement); | ||
@@ -170,23 +173,32 @@ }); | ||
}, | ||
Program(path, state) { | ||
Program(p, state) { | ||
imports = {}; | ||
recast = state.file.opts.parserOpts && state.file.opts.parserOpts.parser == 'recast'; | ||
commentsProperty = recast ? 'comments' : 'leadingComments'; | ||
resourcePath = state.file.opts.filename; | ||
imports = {}; | ||
const root = path.node; | ||
const root = p.node; | ||
const innerCommentsProperty = recast ? 'comments' : 'innerComments'; | ||
if (root[innerCommentsProperty]) { | ||
processComments(innerCommentsProperty, root, path); | ||
processComments(innerCommentsProperty, root, p); | ||
} | ||
path.traverse({ | ||
enter(path) { | ||
if (path.node[commentsProperty]) { | ||
processComments(commentsProperty, path.node, path); | ||
p.traverse({ | ||
enter(p) { | ||
if (p.node.type == 'ImportDeclaration') { | ||
const specifiers = p.node.specifiers; | ||
for (let i = 0, ii = specifiers.length; i < ii; ++i) { | ||
const specifier = specifiers[i]; | ||
let modulePath = path.resolve(path.dirname(resourcePath), p.node.source.value); | ||
modulePath = modulePath.replace(/\.js$/, ''); | ||
if (specifier.type == 'ImportDefaultSpecifier') { | ||
imports[modulePath] = specifier.local.name; | ||
} else if (specifier.type == 'ImportSpecifier') { | ||
imports[`${modulePath}.${specifier.imported.name}`] = specifier.local.name; | ||
} | ||
} | ||
} | ||
if (p.node[commentsProperty]) { | ||
processComments(commentsProperty, p.node, p); | ||
} | ||
} | ||
}); | ||
Object.keys(imports).forEach(i => { | ||
const node = babel.transform(i).ast.program.body[0]; | ||
path.pushContainer('body', node); | ||
}); | ||
} | ||
@@ -193,0 +205,0 @@ } |
{ | ||
"name": "babel-plugin-jsdoc-closure", | ||
"version": "1.3.4", | ||
"version": "1.4.0", | ||
"description": "Transpiles JSDoc types from namepaths to types for Closure Compiler", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -33,7 +33,6 @@ const babel = require('babel-core'); | ||
'/** @type {module:module1/Bar} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {module1$Bar} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'/** @type {../module1/Bar} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -53,5 +52,4 @@ ); | ||
'function getFoo() {\n' + | ||
' return (\n /** @type {module1$Bar} */\n (bar.getBar())\n );\n' + | ||
'}\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');'; | ||
' return (\n /** @type {../module1/Bar} */\n (bar.getBar())\n );\n' + | ||
'}'; | ||
const filename = './test/module2/types.js'; | ||
@@ -72,5 +70,4 @@ const got = babel.transform(source, Object.assign({filename}, recastOptions)); | ||
'function getFoo() {\n' + | ||
' return (\n /** @type {Object} */ ({\n foo: /** @type {module1$Foo} */ (\'bar\')\n })\n );\n' + | ||
'}\n' + | ||
'const module1$Foo = require(\'../module1/Foo\');'; | ||
' return (\n /** @type {Object} */ ({\n foo: /** @type {../module1/Foo} */ (\'bar\')\n })\n );\n' + | ||
'}'; | ||
const filename = './test/module2/types.js'; | ||
@@ -84,8 +81,9 @@ const got = babel.transform(source, Object.assign({filename}, recastOptions)); | ||
'/** @module module2/types */\n' + | ||
'/** @type {module:module1/Bar~Bar} */\n' + | ||
'let foo;\n', | ||
'import Bar from \'../module1/Bar\';\n' + | ||
'/** @type {module:module1/Bar~BarDefault} */\n' + | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {module1_Bar_Bar} */\n' + | ||
'let foo;\n' + | ||
'const module1_Bar_Bar = require(\'../module1/Bar\');', | ||
'import Bar from \'../module1/Bar\';\n' + | ||
'/** @type {Bar} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -98,8 +96,9 @@ ); | ||
'/** @module module2/types */\n' + | ||
'import {foo as Foo} from \'../types\';\n' + | ||
'/** @type {module:types.foo} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {_types_foo} */\n' + | ||
'let foo;' + '\n' + | ||
'const _types_foo = require(\'../types\').foo;', | ||
'import {foo as Foo} from \'../types\';\n' + | ||
'/** @type {Foo} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -113,7 +112,6 @@ ); | ||
'/** @type {module:types~foo} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {_types_foo} */\n' + | ||
'let foo;' + '\n' + | ||
'const _types_foo = require(\'../types\').foo;', | ||
'/** @type {../types.foo} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -127,8 +125,6 @@ ); | ||
'/** @type {module:types~foo.<module:types~bar>} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {_types_foo.<_types_bar>} */\n' + | ||
'let foo;' + '\n' + | ||
'const _types_foo = require(\'../types\').foo;\n' + | ||
'const _types_bar = require(\'../types\').bar;', | ||
'/** @type {../types.foo.<../types.bar>} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -142,7 +138,6 @@ ); | ||
'/** @type {module:module1/Bar} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {module1$Bar} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../../module1/Bar\');', | ||
'/** @type {../../module1/Bar} */\n' + | ||
'let foo;', | ||
'./test/module2/types/index.js' | ||
@@ -156,7 +151,6 @@ ); | ||
'/** @type {module:module1/Bar|string} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {module1$Bar|string} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'/** @type {../module1/Bar|string} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -170,7 +164,6 @@ ); | ||
'/** @type {Object<string, module:module1/Bar>} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @type {Object<string, module1$Bar>} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'/** @type {Object<string, ../module1/Bar>} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -184,7 +177,6 @@ ); | ||
'/** @param {module:module1/Bar=} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @param {module1$Bar=} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'/** @param {../module1/Bar=} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -198,7 +190,6 @@ ); | ||
'/** @param {!module:module1/Bar} */\n' + | ||
'let foo;\n', | ||
'let foo;', | ||
'/** @module module2/types */\n' + | ||
'/** @param {!module1$Bar} */\n' + | ||
'let foo;\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'/** @param {!../module1/Bar} */\n' + | ||
'let foo;', | ||
'./test/module2/types.js' | ||
@@ -238,3 +229,3 @@ ); | ||
'export let Bar;\n\n' + | ||
'/** @typedef {{bar:(!module1$Bar),baz:(undefined|number)}}\n' + | ||
'/** @typedef {{bar:(!../module1/Bar),baz:(undefined|number)}}\n' + | ||
' *\n' + | ||
@@ -244,4 +235,3 @@ ' *\n' + | ||
' */\n' + | ||
'export let Foo;\n\n' + | ||
'const module1$Bar = require(\'../module1/Bar\');', | ||
'export let Foo;', | ||
'./test/module2/types.js' | ||
@@ -248,0 +238,0 @@ ); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
19843
11
409
2