Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/transformation/utils/function-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/unit/functions/noImplicitSelfOption.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down