Skip to content

Commit ef7e027

Browse files
tomblindPerryvw
authored andcommitted
@noself changed to not affect merged namespaces (#689)
1 parent 19db39c commit ef7e027

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/TSHelper.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,25 @@ export function getCustomDecorators(type: ts.Type, checker: ts.TypeChecker): Map
348348
return decMap;
349349
}
350350

351+
export function getCustomNodeDirectives(node: ts.Node): Map<DecoratorKind, Decorator> {
352+
const directivesMap = new Map<DecoratorKind, Decorator>();
353+
354+
ts.getJSDocTags(node).forEach(tag => {
355+
const tagName = tag.tagName.escapedText as string;
356+
if (Decorator.isValid(tagName)) {
357+
const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []);
358+
directivesMap.set(dec.kind, dec);
359+
}
360+
});
361+
362+
return directivesMap;
363+
}
364+
351365
export function getCustomFileDirectives(file: ts.SourceFile): Map<DecoratorKind, Decorator> {
352-
const decMap = new Map<DecoratorKind, Decorator>();
353366
if (file.statements.length > 0) {
354-
const tags = ts.getJSDocTags(file.statements[0]);
355-
for (const tag of tags) {
356-
const tagName = tag.tagName.escapedText as string;
357-
if (Decorator.isValid(tagName)) {
358-
const dec = new Decorator(tagName, tag.comment ? tag.comment.split(" ") : []);
359-
decMap.set(dec.kind, dec);
360-
}
361-
}
367+
return getCustomNodeDirectives(file.statements[0]);
362368
}
363-
return decMap;
369+
return new Map();
364370
}
365371

366372
export function getCustomSignatureDirectives(
@@ -607,8 +613,7 @@ export function hasNoSelfAncestor(declaration: ts.Declaration, checker: ts.TypeC
607613
if (ts.isSourceFile(scopeDeclaration)) {
608614
return getCustomFileDirectives(scopeDeclaration).has(DecoratorKind.NoSelfInFile);
609615
}
610-
const scopeType = checker.getTypeAtLocation(scopeDeclaration);
611-
if (scopeType && getCustomDecorators(scopeType, checker).has(DecoratorKind.NoSelf)) {
616+
if (getCustomNodeDirectives(scopeDeclaration).has(DecoratorKind.NoSelf)) {
612617
return true;
613618
}
614619
return hasNoSelfAncestor(scopeDeclaration, checker);
@@ -645,8 +650,7 @@ export function getDeclarationContextType(
645650
return ContextType.NonVoid;
646651
}
647652

648-
const scopeType = checker.getTypeAtLocation(scopeDeclaration);
649-
if (scopeType && getCustomDecorators(scopeType, checker).has(DecoratorKind.NoSelf)) {
653+
if (getCustomNodeDirectives(scopeDeclaration).has(DecoratorKind.NoSelf)) {
650654
return ContextType.Void;
651655
}
652656
return ContextType.NonVoid;

test/unit/assignments/functionPermutations.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ export const selfTestFunctions: TestFunction[] = [
133133
}
134134
const anonFunctionNestedInNoSelfClass = (new AnonFunctionNestedInNoSelfClass).method();`,
135135
},
136+
{
137+
value: "anonMethodClassMergedNoSelfNS.method",
138+
definition: `class AnonMethodClassMergedNoSelfNS { method(s: string): string { return s; } }
139+
/** @noSelf */ namespace AnonMethodClassMergedNoSelfNS { export function nsFunc(s: string) { return s; } }
140+
const anonMethodClassMergedNoSelfNS = new AnonMethodClassMergedNoSelfNS();`,
141+
},
142+
{
143+
value: "AnonFuncNSMergedNoSelfClass.nsFunc",
144+
definition: `/** @noSelf */ class AnonFuncNSMergedNoSelfClass { method(s: string): string { return s; } }
145+
namespace AnonFuncNSMergedNoSelfClass { export function nsFunc(s: string) { return s; } }`,
146+
},
147+
{
148+
value: "SelfAnonFuncNSMergedNoSelfNS.nsFuncSelf",
149+
definition: `namespace SelfAnonFuncNSMergedNoSelfNS { export function nsFuncSelf(s: string): string { return s; } }
150+
/** @noSelf */ namespace SelfAnonFuncNSMergedNoSelfNS { export function nsFuncNoSelf(s: string) { return s; } }`,
151+
},
136152
];
137153

138154
export const noSelfTestFunctions: TestFunction[] = [
@@ -263,6 +279,22 @@ export const noSelfTestFunctions: TestFunction[] = [
263279
const anonFunctionNestedInClassInNoSelfNs =
264280
(new AnonFunctionNestedInClassInNoSelfNs.AnonFunctionNestedInClass).method();`,
265281
},
282+
{
283+
value: "noSelfAnonMethodClassMergedNS.method",
284+
definition: `/** @noSelf */ class NoSelfAnonMethodClassMergedNS { method(s: string): string { return s; } }
285+
namespace NoSelfAnonMethodClassMergedNS { export function nsFunc(s: string) { return s; } }
286+
const noSelfAnonMethodClassMergedNS = new NoSelfAnonMethodClassMergedNS();`,
287+
},
288+
{
289+
value: "NoSelfAnonFuncNSMergedClass.nsFunc",
290+
definition: `class NoSelfAnonFuncNSMergedClass { method(s: string): string { return s; } }
291+
/** @noSelf */ namespace NoSelfAnonFuncNSMergedClass { export function nsFunc(s: string) { return s; } }`,
292+
},
293+
{
294+
value: "NoSelfAnonFuncNSMergedSelfNS.nsFuncNoSelf",
295+
definition: `namespace NoSelfAnonFuncNSMergedSelfNS { export function nsFuncSelf(s: string): string { return s; } }
296+
/** @noSelf */ namespace NoSelfAnonFuncNSMergedSelfNS { export function nsFuncNoSelf(s: string) { return s; } }`,
297+
},
266298
];
267299

268300
const noSelfInFileTestFunctions: TestFunction[] = [

0 commit comments

Comments
 (0)