From 8ab6c82aab1c394d32b00bb941d14f5fa79318be Mon Sep 17 00:00:00 2001 From: Tom <26638278+tomblind@users.noreply.github.com> Date: Thu, 25 Jul 2019 06:48:40 -0600 Subject: [PATCH] @noSelf changed to not affect merged namespaces --- src/TSHelper.ts | 32 +++++++++++-------- test/unit/assignments/functionPermutations.ts | 32 +++++++++++++++++++ 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 85a6a08ef..74751017f 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -337,19 +337,25 @@ export function getCustomDecorators(type: ts.Type, checker: ts.TypeChecker): Map return decMap; } +export function getCustomNodeDirectives(node: ts.Node): Map { + const directivesMap = new Map(); + + ts.getJSDocTags(node).forEach(tag => { + const tagName = tag.tagName.escapedText as string; + if (Decorator.isValid(tagName)) { + const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []); + directivesMap.set(dec.kind, dec); + } + }); + + return directivesMap; +} + export function getCustomFileDirectives(file: ts.SourceFile): Map { - const decMap = new Map(); if (file.statements.length > 0) { - const tags = ts.getJSDocTags(file.statements[0]); - for (const tag of tags) { - const tagName = tag.tagName.escapedText as string; - if (Decorator.isValid(tagName)) { - const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []); - decMap.set(dec.kind, dec); - } - } + return getCustomNodeDirectives(file.statements[0]); } - return decMap; + return new Map(); } export function getCustomSignatureDirectives( @@ -596,8 +602,7 @@ export function hasNoSelfAncestor(declaration: ts.Declaration, checker: ts.TypeC if (ts.isSourceFile(scopeDeclaration)) { return getCustomFileDirectives(scopeDeclaration).has(DecoratorKind.NoSelfInFile); } - const scopeType = checker.getTypeAtLocation(scopeDeclaration); - if (scopeType && getCustomDecorators(scopeType, checker).has(DecoratorKind.NoSelf)) { + if (getCustomNodeDirectives(scopeDeclaration).has(DecoratorKind.NoSelf)) { return true; } return hasNoSelfAncestor(scopeDeclaration, checker); @@ -634,8 +639,7 @@ export function getDeclarationContextType( return ContextType.NonVoid; } - const scopeType = checker.getTypeAtLocation(scopeDeclaration); - if (scopeType && getCustomDecorators(scopeType, checker).has(DecoratorKind.NoSelf)) { + if (getCustomNodeDirectives(scopeDeclaration).has(DecoratorKind.NoSelf)) { return ContextType.Void; } return ContextType.NonVoid; diff --git a/test/unit/assignments/functionPermutations.ts b/test/unit/assignments/functionPermutations.ts index cbbef188e..2602e2ba7 100644 --- a/test/unit/assignments/functionPermutations.ts +++ b/test/unit/assignments/functionPermutations.ts @@ -133,6 +133,22 @@ export const selfTestFunctions: TestFunction[] = [ } const anonFunctionNestedInNoSelfClass = (new AnonFunctionNestedInNoSelfClass).method();`, }, + { + value: "anonMethodClassMergedNoSelfNS.method", + definition: `class AnonMethodClassMergedNoSelfNS { method(s: string): string { return s; } } + /** @noSelf */ namespace AnonMethodClassMergedNoSelfNS { export function nsFunc(s: string) { return s; } } + const anonMethodClassMergedNoSelfNS = new AnonMethodClassMergedNoSelfNS();`, + }, + { + value: "AnonFuncNSMergedNoSelfClass.nsFunc", + definition: `/** @noSelf */ class AnonFuncNSMergedNoSelfClass { method(s: string): string { return s; } } + namespace AnonFuncNSMergedNoSelfClass { export function nsFunc(s: string) { return s; } }`, + }, + { + value: "SelfAnonFuncNSMergedNoSelfNS.nsFuncSelf", + definition: `namespace SelfAnonFuncNSMergedNoSelfNS { export function nsFuncSelf(s: string): string { return s; } } + /** @noSelf */ namespace SelfAnonFuncNSMergedNoSelfNS { export function nsFuncNoSelf(s: string) { return s; } }`, + }, ]; export const noSelfTestFunctions: TestFunction[] = [ @@ -263,6 +279,22 @@ export const noSelfTestFunctions: TestFunction[] = [ const anonFunctionNestedInClassInNoSelfNs = (new AnonFunctionNestedInClassInNoSelfNs.AnonFunctionNestedInClass).method();`, }, + { + value: "noSelfAnonMethodClassMergedNS.method", + definition: `/** @noSelf */ class NoSelfAnonMethodClassMergedNS { method(s: string): string { return s; } } + namespace NoSelfAnonMethodClassMergedNS { export function nsFunc(s: string) { return s; } } + const noSelfAnonMethodClassMergedNS = new NoSelfAnonMethodClassMergedNS();`, + }, + { + value: "NoSelfAnonFuncNSMergedClass.nsFunc", + definition: `class NoSelfAnonFuncNSMergedClass { method(s: string): string { return s; } } + /** @noSelf */ namespace NoSelfAnonFuncNSMergedClass { export function nsFunc(s: string) { return s; } }`, + }, + { + value: "NoSelfAnonFuncNSMergedSelfNS.nsFuncNoSelf", + definition: `namespace NoSelfAnonFuncNSMergedSelfNS { export function nsFuncSelf(s: string): string { return s; } } + /** @noSelf */ namespace NoSelfAnonFuncNSMergedSelfNS { export function nsFuncNoSelf(s: string) { return s; } }`, + }, ]; const noSelfInFileTestFunctions: TestFunction[] = [