From 4643c71e75388a260d8c0145b3f7048ea523228c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 29 Sep 2014 16:05:37 +0200 Subject: [PATCH] Treat unknown opt types as Object in ol externs --- tasks/generate-externs.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tasks/generate-externs.js b/tasks/generate-externs.js index 81af5d2b82..04aa38b798 100644 --- a/tasks/generate-externs.js +++ b/tasks/generate-externs.js @@ -39,6 +39,7 @@ function generateExterns(typedefs, symbols, externs, base) { var lines = []; var processedSymbols = {}; var constructors = {}; + var constructorOptionsTypes = {}; function addNamespaces(name) { var parts = name.split('.'); @@ -74,6 +75,16 @@ function generateExterns(typedefs, symbols, externs, base) { return typesWithoutGoog; } + // Store in "constructorOptionsTypes" type names that start + // with "ol." and end with "Options". + function findConstructorOptionsTypes(types) { + types.forEach(function(type) { + if (type.match(/^ol\..*Options$/)) { + constructorOptionsTypes[type] = true; + } + }); + } + function processSymbol(symbol) { addNamespaces(symbol.name.split('#')[0]); @@ -105,6 +116,7 @@ function generateExterns(typedefs, symbols, externs, base) { var args = []; if (symbol.params) { symbol.params.forEach(function(param) { + findConstructorOptionsTypes(param.types); args.push(param.name); lines.push(' * @param {' + (param.variable ? '...' : '') + @@ -137,6 +149,10 @@ function generateExterns(typedefs, symbols, externs, base) { symbols.forEach(processSymbol); typedefs.forEach(function(typedef) { + // we're about to add a @typedef for "typedef.name" so remove that + // type from constructorOptionsTypes + delete constructorOptionsTypes[typedef.name]; + addNamespaces(typedef.name); lines.push('/**'); lines.push(' * @typedef {' + noGoogTypes(typedef.types).join('|') + '}'); @@ -145,6 +161,25 @@ function generateExterns(typedefs, symbols, externs, base) { lines.push('\n'); }); + + // At this point constructorOptionsTypes includes options types for which we + // did not have a @typedef yet. For those we add @typedef {Object}. + // + // This is used for abstract base classes. Abstract base classes should be + // defined as types in the ol externs file. But the corresponding @typedef's + // are not marked with an @api annotation because abstract base classes are + // not instantiated by applications. Yet, we need to have a type defined + // in the ol externs file for these options types. So we just use + // @typedef {Object}. + Object.keys(constructorOptionsTypes).forEach(function(key) { + lines.push('/**'); + lines.push(' * No `@api` annotation for `' + key + '`, use `Object`.'); + lines.push(' * @typedef {Object}'); + lines.push(' */'); + lines.push(nameToJS(key) + ';'); + lines.push('\n'); + }); + return lines.join('\n'); }