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

Forbid re-exports of non-importable variables #9

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { subFoo } from "./sub/foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @package
*/
export const subFoo = "hello!";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { subFoo } from "./sub/foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { subFoo } from "./sub/foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { subFoo } from "./sub/index";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @package
*/
export const subFoo = "hello!";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @package
*/
export { subFoo } from "./foo";
89 changes: 89 additions & 0 deletions src/__tests__/reexport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ Array [
]
`);
});
it("Cannot re-export a package-private variable", async () => {
const result = await tester.lintFile(
"src/reexport4/indexLoophole/reexportFromSubFoo.ts"
);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 10,
"endColumn": 16,
"endLine": 1,
"line": 1,
"message": "Cannot re-export a package-private export 'subFoo'",
"messageId": "package:reexport",
"nodeType": "ExportSpecifier",
"ruleId": "import-access/jsdoc",
"severity": 2,
},
]
`);
});
it("Can re-export a variable exported from index.ts", async () => {
const result = await tester.lintFile(
"src/reexport4/indexLoophole/reexportFromSubIndex.ts"
);
expect(result).toMatchInlineSnapshot(`Array []`);
});
describe("indexLoophole = false", () => {
it("Cannot import a package-private variable from sub/index.ts", async () => {
const result = await tester.lintFile("src/reexport/useFoo.ts", {
Expand Down Expand Up @@ -75,6 +101,31 @@ Array [
"severity": 2,
},
]
`);
});
it("Cannot re-export a package-private variable", async () => {
const result = await tester.lintFile(
"src/reexport4/indexLoophole/reexportFromSubIndex.ts",
{
jsdoc: {
indexLoophole: false,
},
}
);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 10,
"endColumn": 16,
"endLine": 1,
"line": 1,
"message": "Cannot re-export a package-private export 'subFoo'",
"messageId": "package:reexport",
"nodeType": "ExportSpecifier",
"ruleId": "import-access/jsdoc",
"severity": 2,
},
]
`);
});
});
Expand All @@ -100,6 +151,44 @@ Array [
"severity": 2,
},
]
`);
});
it("Can re-export from sub directory of same name", async () => {
const result = await tester.lintFile(
"src/reexport4/filenameLoophole/sub.ts",
{
jsdoc: {
indexLoophole: false,
filenameLoophole: true,
},
}
);
expect(result).toMatchInlineSnapshot(`Array []`);
});
it("Cannot re-export from sub directory of different name", async () => {
const result = await tester.lintFile(
"src/reexport4/filenameLoophole/sub2.ts",
{
jsdoc: {
indexLoophole: false,
filenameLoophole: true,
},
}
);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 10,
"endColumn": 16,
"endLine": 1,
"line": 1,
"message": "Cannot re-export a package-private export 'subFoo'",
"messageId": "package:reexport",
"nodeType": "ExportSpecifier",
"ruleId": "import-access/jsdoc",
"severity": 2,
},
]
`);
});
});
Expand Down
66 changes: 57 additions & 9 deletions src/rules/jsdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { checkSymbolImportability } from "../core/checkSymbolmportability";
import { getImmediateAliasedSymbol } from "../utils/getImmediateAliasedSymbol";
import { PackageOptions } from "../utils/isInPackage";

type MessageId = "package" | "private";
type MessageId =
| "package"
| "package:reexport"
| "private"
| "private:reexport";

export type JSDocRuleOptions = {
/**
Expand Down Expand Up @@ -34,7 +38,11 @@ const jsdocRule: Omit<
},
messages: {
package: "Cannot import a package-private export '{{ identifier }}'",
"package:reexport":
"Cannot re-export a package-private export '{{ identifier }}'",
private: "Cannot import a private export '{{ identifier }}'",
"private:reexport":
"Cannot re-export a private export '{{ identifier }}'",
},
schema: [
{
Expand Down Expand Up @@ -102,6 +110,29 @@ const jsdocRule: Omit<
checkSymbol(context, packageOptions, checker, node, tsNode, symbol);
}
},
ExportSpecifier(node) {
const shouldSkip = shouldSkipSymbolCheck(node);
if (shouldSkip) {
return;
}

const checker = parserServices.program.getTypeChecker();

const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);

const symbol = checker.getSymbolAtLocation(tsNode.name);
if (symbol) {
checkSymbol(
context,
packageOptions,
checker,
node,
tsNode,
symbol,
true
);
}
},
};
},
};
Expand All @@ -120,12 +151,28 @@ export function jsDocRuleDefaultOptions(
}

function shouldSkipSymbolCheck(
node: TSESTree.ImportSpecifier | TSESTree.ImportDefaultSpecifier
) {
if (node.parent?.type === "ImportDeclaration") {
const packageName = node.parent.source.value;
return isNodeBuiltinModule(packageName) || willBeImportedFromNodeModules(packageName);
node:
| TSESTree.ImportSpecifier
| TSESTree.ImportDefaultSpecifier
| TSESTree.ExportSpecifier
): boolean {
if (!node.parent) {
return true;
}
if (
node.parent.type !== "ImportDeclaration" &&
node.parent.type !== "ExportNamedDeclaration"
) {
return true;
}
const packageName = node.parent.source?.value;
if (!packageName) {
return true;
}
return (
isNodeBuiltinModule(packageName) ||
willBeImportedFromNodeModules(packageName)
);
}

function isNodeBuiltinModule(importPath: string) {
Expand Down Expand Up @@ -163,7 +210,8 @@ function checkSymbol(
checker: TypeChecker,
originalNode: TSESTree.Node,
tsNode: Node,
symbol: Symbol
symbol: Symbol,
reexport = false
) {
const exsy = getImmediateAliasedSymbol(checker, symbol);
if (!exsy) {
Expand All @@ -179,7 +227,7 @@ function checkSymbol(
case "package": {
context.report({
node: originalNode,
messageId: "package",
messageId: reexport ? "package:reexport" : "package",
data: {
identifier: exsy.name,
},
Expand All @@ -189,7 +237,7 @@ function checkSymbol(
case "private": {
context.report({
node: originalNode,
messageId: "private",
messageId: reexport ? "private:reexport" : "private",
data: {
identifier: exsy.name,
},
Expand Down