mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2025-12-08 20:58:55 +00:00
CLI: Do not wrap main definition as a module and export directly instead; CLI: Generate prettier definitions with --no-comments
This commit is contained in:
parent
805291086f
commit
65637ffce2
22
cli/pbts.js
22
cli/pbts.js
@ -22,12 +22,15 @@ exports.main = function(args, callback) {
|
||||
var argv = minimist(args, {
|
||||
alias: {
|
||||
name: "n",
|
||||
out : "o"
|
||||
out : "o",
|
||||
main: "m",
|
||||
global: "g"
|
||||
},
|
||||
string: [ "name", "out" ],
|
||||
boolean: [ "comments" ],
|
||||
string: [ "name", "out", "global" ],
|
||||
boolean: [ "comments", "main" ],
|
||||
default: {
|
||||
comments: true
|
||||
comments: true,
|
||||
main: false
|
||||
}
|
||||
});
|
||||
|
||||
@ -43,9 +46,9 @@ exports.main = function(args, callback) {
|
||||
"Generates TypeScript definitions from annotated JavaScript files.",
|
||||
"",
|
||||
" -n, --name Wraps everything in a module of the specified name.",
|
||||
"",
|
||||
" -o, --out Saves to a file instead of writing to stdout.",
|
||||
"",
|
||||
" -m, --main Whether building the main library without any imports.",
|
||||
" -g, --global Name of the global object in browser environments, if any.",
|
||||
" --no-comments Does not output any JSDoc comments.",
|
||||
"",
|
||||
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..."
|
||||
@ -96,7 +99,12 @@ exports.main = function(args, callback) {
|
||||
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
|
||||
""
|
||||
];
|
||||
if (argv.name !== "protobuf")
|
||||
if (argv.global)
|
||||
output.push(
|
||||
"export as namespace " + argv.global + ";",
|
||||
""
|
||||
);
|
||||
if (!argv.main)
|
||||
output.push(
|
||||
"import * as $protobuf from \"protobufjs\";",
|
||||
""
|
||||
|
||||
4403
index.d.ts
vendored
4403
index.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@ -70,9 +70,9 @@ exports.publish = function publish(taffy, opts) {
|
||||
// wrap everything in a module if configured
|
||||
if (options.module) {
|
||||
writeln("export = ", options.module, ";");
|
||||
writeln("export as namespace " + options.module, ";");
|
||||
writeln();
|
||||
writeln("declare namespace ", options.module, " {");
|
||||
writeln();
|
||||
++indent;
|
||||
}
|
||||
|
||||
@ -132,9 +132,12 @@ function writeln() {
|
||||
}
|
||||
|
||||
// writes a comment
|
||||
function writeComment(comment) {
|
||||
if (!comment || options.comments === false)
|
||||
function writeComment(comment, otherwiseNewline) {
|
||||
if (!comment || options.comments === false) {
|
||||
if (otherwiseNewline)
|
||||
writeln();
|
||||
return;
|
||||
}
|
||||
var first = true;
|
||||
comment.split(/\r?\n/g).forEach(function(line) {
|
||||
line = line.trim().replace(/^\*/, " *");
|
||||
@ -161,16 +164,21 @@ function replaceRecursive(name, re, fn) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// tests if an element is considered class-like
|
||||
function isClass(element) {
|
||||
// tests if an element is considered to be a class or class-like
|
||||
function isClassLike(element) {
|
||||
return element && (element.kind === "class" || element.kind === "interface" || element.kind === "mixin");
|
||||
}
|
||||
|
||||
// tests if an element is considered an interface
|
||||
// tests if an element is considered to be an interface
|
||||
function isInterface(element) {
|
||||
return element && (element.kind === "interface" || (getTypeOf(element) === 'Object' && element.properties && element.properties.length));
|
||||
}
|
||||
|
||||
// tests if an element is considered to be a namespace
|
||||
function isNamespace(element) {
|
||||
return element && (element.kind === "namespace" || element.kind === "module");
|
||||
}
|
||||
|
||||
// gets all children of the specified parent
|
||||
function getChildrenOf(parent) {
|
||||
var memberof = parent ? parent.longname : undefined;
|
||||
@ -219,10 +227,11 @@ function getTypeOf(element) {
|
||||
}
|
||||
|
||||
// begins writing the definition of the specified element
|
||||
function begin(element, noDeclare) {
|
||||
writeComment(element.comment);
|
||||
if (element.scope === "global" && !options.module && !noDeclare)
|
||||
write("declare ");
|
||||
function begin(element, is_interface) {
|
||||
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element));
|
||||
if (element.scope !== "global" || options.module || is_interface || isInterface(element))
|
||||
return;
|
||||
write("export ");
|
||||
}
|
||||
|
||||
// writes the function signature describing element
|
||||
@ -281,6 +290,7 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {
|
||||
|
||||
// writes (a typedef as) an interface
|
||||
function writeInterface(element) {
|
||||
writeln();
|
||||
writeln("interface ", element.name, " {");
|
||||
++indent;
|
||||
element.properties.forEach(function(property) {
|
||||
@ -302,7 +312,7 @@ function handleElement(element, parent) {
|
||||
if (seen[element.longname])
|
||||
return;
|
||||
seen[element.longname] = element;
|
||||
if (isClass(element))
|
||||
if (isClassLike(element))
|
||||
return handleClass(element, parent);
|
||||
switch (element.kind) {
|
||||
case "module":
|
||||
@ -376,7 +386,7 @@ function handleClass(element, parent) {
|
||||
// members except inner classes
|
||||
var innerClasses = [];
|
||||
getChildrenOf(element).forEach(function(child) {
|
||||
if (isClass(child))
|
||||
if (isClassLike(child))
|
||||
innerClasses.push(child);
|
||||
else
|
||||
handleElement(child, element);
|
||||
@ -386,7 +396,6 @@ function handleClass(element, parent) {
|
||||
writeln("}");
|
||||
|
||||
if (innerClasses.length) {
|
||||
writeln("");
|
||||
begin(element);
|
||||
writeln("namespace ", element.name, " {");
|
||||
++indent;
|
||||
@ -419,7 +428,7 @@ function handleMember(element, parent) {
|
||||
|
||||
} else {
|
||||
|
||||
if (isClass(parent)) {
|
||||
if (isClassLike(parent)) {
|
||||
if (element.access)
|
||||
write(element.access, " ");
|
||||
if (element.scope === "static")
|
||||
@ -451,7 +460,7 @@ function handleFunction(element, parent, isConstructor) {
|
||||
write("constructor");
|
||||
} else {
|
||||
begin(element);
|
||||
if (isClass(parent)) {
|
||||
if (isClassLike(parent)) {
|
||||
if (element.access)
|
||||
write(element.access, " ");
|
||||
if (element.scope === "static")
|
||||
@ -467,7 +476,7 @@ function handleFunction(element, parent, isConstructor) {
|
||||
// handles a type definition (not a real type)
|
||||
function handleTypeDef(element, parent) {
|
||||
if (isInterface(element)) {
|
||||
if (isClass(parent))
|
||||
if (isClassLike(parent))
|
||||
queuedInterfaces.push(element);
|
||||
else {
|
||||
begin(element);
|
||||
|
||||
@ -35,7 +35,7 @@
|
||||
"prepublish": "node scripts/prepublish",
|
||||
"prof": "node bench/prof",
|
||||
"test": "tape tests/*.js | tap-spec",
|
||||
"types": "node bin/pbts --name protobuf --out index.d.ts src && tsc tests/typescript.ts --lib es2015 --noEmit && tsc tests/data/test.ts --lib es2015 --noEmit",
|
||||
"types": "node bin/pbts --main --global protobuf --out index.d.ts src && tsc tests/typescript.ts --lib es2015 --noEmit && tsc tests/data/test.ts --lib es2015 --noEmit",
|
||||
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
|
||||
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
|
||||
"make": "npm run lint && npm run test && npm run types && npm run build",
|
||||
|
||||
@ -33,8 +33,8 @@ var fs = require("fs"),
|
||||
.forEach(function(file) {
|
||||
var out = file.replace(/\.js$/, ".d.ts");
|
||||
pbts.main([
|
||||
"--name", path.basename(out, ".d.ts"),
|
||||
"--out", out,
|
||||
"--no-comments",
|
||||
file
|
||||
], function(err) {
|
||||
if (err)
|
||||
|
||||
5445
tests/data/test.d.ts
vendored
5445
tests/data/test.d.ts
vendored
File diff suppressed because it is too large
Load Diff
@ -26,7 +26,7 @@ export class Hello extends protobuf.Message {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
protobuf.Class.create(root.lookup("Hello") as protobuf.Type, Hello);
|
||||
protobuf.Class.create(root.lookupType("Hello"), Hello);
|
||||
|
||||
var hello = new Hello();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user