From 6f233bfd01e4afcf4e397c9259cd9ef98058600c Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 17 Jan 2020 16:09:04 -0800 Subject: [PATCH] Consider SymbolFlags.Method as function-esque during js declaration emit --- src/compiler/checker.ts | 2 +- .../jsDeclarationsFunctionPrototypeStatic.js | 44 +++++++++++++++++++ ...eclarationsFunctionPrototypeStatic.symbols | 32 ++++++++++++++ ...sDeclarationsFunctionPrototypeStatic.types | 40 +++++++++++++++++ .../jsDeclarationsFunctionPrototypeStatic.ts | 19 ++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.js create mode 100644 tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.symbols create mode 100644 tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.types create mode 100644 tests/cases/conformance/jsdoc/declarations/jsDeclarationsFunctionPrototypeStatic.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 512e431fbe5ae..a498ac0740bf4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5370,7 +5370,7 @@ namespace ts { symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.FunctionScopedVariable | SymbolFlags.Property) && symbol.escapedName !== InternalSymbolName.ExportEquals; const isConstMergedWithNSPrintableAsSignatureMerge = isConstMergedWithNS && isTypeRepresentableAsFunctionNamespaceMerge(getTypeOfSymbol(symbol), symbol); - if (symbol.flags & SymbolFlags.Function || isConstMergedWithNSPrintableAsSignatureMerge) { + if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method) || isConstMergedWithNSPrintableAsSignatureMerge) { serializeAsFunctionNamespaceMerge(getTypeOfSymbol(symbol), symbol, getInternalSymbolName(symbol, symbolName), modifierFlags); } if (symbol.flags & SymbolFlags.TypeAlias) { diff --git a/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.js b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.js new file mode 100644 index 0000000000000..27e300ffbdf70 --- /dev/null +++ b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.js @@ -0,0 +1,44 @@ +//// [source.js] +module.exports = MyClass; + +function MyClass() {} +MyClass.staticMethod = function() {} +MyClass.prototype.method = function() {} +MyClass.staticProperty = 123; + +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ + +//// [source.js] +module.exports = MyClass; +function MyClass() { } +MyClass.staticMethod = function () { }; +MyClass.prototype.method = function () { }; +MyClass.staticProperty = 123; +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ + + +//// [source.d.ts] +export = MyClass; +declare function MyClass(): void; +declare class MyClass { + method(): void; +} +declare namespace MyClass { + export { staticMethod, staticProperty, DoneCB }; +} +declare function staticMethod(): void; +declare var staticProperty: number; +/** + * Callback to be invoked when test execution is complete. + */ +type DoneCB = (failures: number) => any; diff --git a/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.symbols b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.symbols new file mode 100644 index 0000000000000..7618a933e7de9 --- /dev/null +++ b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.symbols @@ -0,0 +1,32 @@ +=== tests/cases/conformance/jsdoc/declarations/source.js === +module.exports = MyClass; +>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/source", Decl(source.js, 0, 0)) +>module : Symbol(export=, Decl(source.js, 0, 0)) +>exports : Symbol(export=, Decl(source.js, 0, 0)) +>MyClass : Symbol(MyClass, Decl(source.js, 0, 25), Decl(source.js, 2, 21), Decl(source.js, 4, 40)) + +function MyClass() {} +>MyClass : Symbol(MyClass, Decl(source.js, 0, 25), Decl(source.js, 2, 21), Decl(source.js, 4, 40)) + +MyClass.staticMethod = function() {} +>MyClass.staticMethod : Symbol(MyClass.staticMethod, Decl(source.js, 2, 21)) +>MyClass : Symbol(MyClass, Decl(source.js, 0, 25), Decl(source.js, 2, 21), Decl(source.js, 4, 40)) +>staticMethod : Symbol(MyClass.staticMethod, Decl(source.js, 2, 21)) + +MyClass.prototype.method = function() {} +>MyClass.prototype : Symbol(MyClass.method, Decl(source.js, 3, 36)) +>MyClass : Symbol(MyClass, Decl(source.js, 0, 25), Decl(source.js, 2, 21), Decl(source.js, 4, 40)) +>prototype : Symbol(Function.prototype, Decl(lib.es5.d.ts, --, --)) +>method : Symbol(MyClass.method, Decl(source.js, 3, 36)) + +MyClass.staticProperty = 123; +>MyClass.staticProperty : Symbol(MyClass.staticProperty, Decl(source.js, 4, 40)) +>MyClass : Symbol(MyClass, Decl(source.js, 0, 25), Decl(source.js, 2, 21), Decl(source.js, 4, 40)) +>staticProperty : Symbol(MyClass.staticProperty, Decl(source.js, 4, 40)) + +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ diff --git a/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.types b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.types new file mode 100644 index 0000000000000..318cf0b2dca4d --- /dev/null +++ b/tests/baselines/reference/jsDeclarationsFunctionPrototypeStatic.types @@ -0,0 +1,40 @@ +=== tests/cases/conformance/jsdoc/declarations/source.js === +module.exports = MyClass; +>module.exports = MyClass : typeof MyClass +>module.exports : typeof MyClass +>module : { "\"tests/cases/conformance/jsdoc/declarations/source\"": typeof MyClass; } +>exports : typeof MyClass +>MyClass : typeof MyClass + +function MyClass() {} +>MyClass : typeof MyClass + +MyClass.staticMethod = function() {} +>MyClass.staticMethod = function() {} : () => void +>MyClass.staticMethod : () => void +>MyClass : typeof MyClass +>staticMethod : () => void +>function() {} : () => void + +MyClass.prototype.method = function() {} +>MyClass.prototype.method = function() {} : () => void +>MyClass.prototype.method : any +>MyClass.prototype : any +>MyClass : typeof MyClass +>prototype : any +>method : any +>function() {} : () => void + +MyClass.staticProperty = 123; +>MyClass.staticProperty = 123 : 123 +>MyClass.staticProperty : number +>MyClass : typeof MyClass +>staticProperty : number +>123 : 123 + +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ diff --git a/tests/cases/conformance/jsdoc/declarations/jsDeclarationsFunctionPrototypeStatic.ts b/tests/cases/conformance/jsdoc/declarations/jsDeclarationsFunctionPrototypeStatic.ts new file mode 100644 index 0000000000000..0c57075af72f9 --- /dev/null +++ b/tests/cases/conformance/jsdoc/declarations/jsDeclarationsFunctionPrototypeStatic.ts @@ -0,0 +1,19 @@ +// @allowJs: true +// @checkJs: true +// @target: es5 +// @outDir: ./out +// @declaration: true +// @filename: source.js +module.exports = MyClass; + +function MyClass() {} +MyClass.staticMethod = function() {} +MyClass.prototype.method = function() {} +MyClass.staticProperty = 123; + +/** + * Callback to be invoked when test execution is complete. + * + * @callback DoneCB + * @param {number} failures - Number of failures that occurred. + */ \ No newline at end of file