From 7fbacb495d4b2641af5be36784ba470290d43d62 Mon Sep 17 00:00:00 2001 From: GlassBricks <24237065+GlassBricks@users.noreply.github.com> Date: Thu, 23 Jun 2022 16:06:40 -0700 Subject: [PATCH] Fix #1295 --- src/transformation/utils/function-context.ts | 11 +++++++++-- test/unit/functions/noImplicitSelfOption.spec.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/transformation/utils/function-context.ts b/src/transformation/utils/function-context.ts index 3a74bce80..896f7e373 100644 --- a/src/transformation/utils/function-context.ts +++ b/src/transformation/utils/function-context.ts @@ -79,8 +79,15 @@ export function getDeclarationContextType( // When using --noImplicitSelf and the signature is defined in a file targeted by the program apply the @noSelf rule. const options = program.getCompilerOptions() as CompilerOptions; - if (options.noImplicitSelf && program.getSourceFile(signatureDeclaration.getSourceFile().fileName) !== undefined) { - return ContextType.Void; + if (options.noImplicitSelf) { + const sourceFile = program.getSourceFile(signatureDeclaration.getSourceFile().fileName); + if ( + sourceFile !== undefined && + !program.isSourceFileDefaultLibrary(sourceFile) && + !program.isSourceFileFromExternalLibrary(sourceFile) + ) { + return ContextType.Void; + } } // Walk up to find @noSelf or @noSelfInFile diff --git a/test/unit/functions/noImplicitSelfOption.spec.ts b/test/unit/functions/noImplicitSelfOption.spec.ts index b68a1ebc9..b2fbdd7d0 100644 --- a/test/unit/functions/noImplicitSelfOption.spec.ts +++ b/test/unit/functions/noImplicitSelfOption.spec.ts @@ -35,6 +35,18 @@ test.each(["\\", "/"])("transpileFiles handles paths with noImplicitSelf and %s } }); +test("noImplicitSelf does not affect functions in default libraries", () => { + util.testFunction` + const array = [1, 2, 3]; + const items = array.filter(x => x > 1); // array.filter is in external library + return items; + ` + .setOptions({ + noImplicitSelf: true, + }) + .expectToMatchJsResult(); +}); + test("enables noSelfInFile behavior for methods", () => { util.testFunction` class FooBar {