Skip to content

Commit d2accd7

Browse files
authored
Fix #1295 (#1296)
1 parent b854f3f commit d2accd7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/transformation/utils/function-context.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,15 @@ export function getDeclarationContextType(
7979

8080
// When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule.
8181
const options = program.getCompilerOptions() as CompilerOptions;
82-
if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) {
83-
return ContextType.Void;
82+
if (options.noImplicitSelf) {
83+
const sourceFile = program.getSourceFile(signatureDeclaration.getSourceFile().fileName);
84+
if (
85+
sourceFile !== undefined &&
86+
!program.isSourceFileDefaultLibrary(sourceFile) &&
87+
!program.isSourceFileFromExternalLibrary(sourceFile)
88+
) {
89+
return ContextType.Void;
90+
}
8491
}
8592

8693
// Walk up to find @noSelf or @noSelfInFile

test/unit/functions/noImplicitSelfOption.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ test.each(["\\", "/"])("transpileFiles handles paths with noImplicitSelf and %s
3535
}
3636
});
3737

38+
test("noImplicitSelf does not affect functions in default libraries", () => {
39+
util.testFunction`
40+
const array = [1, 2, 3];
41+
const items = array.filter(x => x > 1); // array.filter is in external library
42+
return items;
43+
`
44+
.setOptions({
45+
noImplicitSelf: true,
46+
})
47+
.expectToMatchJsResult();
48+
});
49+
3850
test("enables noSelfInFile behavior for methods", () => {
3951
util.testFunction`
4052
class FooBar {

0 commit comments

Comments
 (0)