Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to ignore members that are public but could be private #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"scripts": {
"build": "rm -rf dist && tsc",
"prepack": "yarn build",
"test": "jest",
"test:ci": "jest --ci"
},
Expand Down
27 changes: 27 additions & 0 deletions src/__test__/analyze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ describe("analyze", () => {
]);
});

it("should ignore private if not wanted", () => {
const project = new Project({ useInMemoryFileSystem: true });
project.createSourceFile(
"foo.ts",
`
class A {
x = 1,

fn() {
return this.x;
}
}
`
);
getConfigMock.mockReturnValue({
ignoreCouldBePrivate: true
});
assertResults(analyze(project), [
{
class: "A",
member: "fn",
reason: "unused",
},
]);
});


it("should check references in all files", () => {
const project = new Project({ useInMemoryFileSystem: true });
project.createSourceFile(
Expand Down
5 changes: 4 additions & 1 deletion src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ export const analyze = (project: t.Project): IOffendingMembers[] => {

// At this point, we knew the member is referenced somewhere, so if
// it's already private then there's nothing to do here.
if (member.getScope() === t.Scope.Private) {
if (
member.getScope() === t.Scope.Private ||
config.ignoreCouldBePrivate
) {
continue;
}

Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ICliConfig {
fix?: boolean | undefined;
path?: string;
ignoreFileRegex?: string;
ignoreCouldBePrivate?: boolean | undefined;
}

interface IConfig extends ICliConfig {
Expand Down Expand Up @@ -33,6 +34,10 @@ const cliConfig = yargs(process.argv.slice(2))
type: "string",
describe: "Regex pattern for excluding files",
},
ignoreCouldBePrivate: {
type: "boolean",
describe: "Don't report members that are public but could be private",
}
})
.hide("fix")
.help().argv;
Expand Down