Skip to content

Commit

Permalink
feat: add ignorePackages option
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 19, 2023
1 parent 109fe17 commit 205ce5f
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions src/rules/requireExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import { readPackageJson } from '../utilities/readPackageJson';

const extensions = ['.js', '.ts', '.tsx'];

type Options = [];
const defaultOptions = {
ignorePackages: false,
};

type Options = [
{
ignorePackages?: boolean;
},
];

type MessageIds = 'extensionMissing';

Expand Down Expand Up @@ -135,7 +143,7 @@ const createTSConfigFinder = () => {
const findTSConfig = createTSConfigFinder();

const handleRelativePath = (
context: TSESLint.RuleContext<'extensionMissing', []>,
context: TSESLint.RuleContext<'extensionMissing', Options>,
node: Node,
importPath: string,
) => {
Expand Down Expand Up @@ -169,9 +177,10 @@ const normalizePackageName = (packageName: string) => {
};

const handleAliasPath = (
context: TSESLint.RuleContext<'extensionMissing', []>,
context: TSESLint.RuleContext<'extensionMissing', Options>,
node: Node,
importPath: string,
ignorePackages: boolean,
) => {
// @ts-expect-error we know this setting exists
const project = (context.settings['import/resolver']?.typescript?.project ??
Expand Down Expand Up @@ -213,10 +222,31 @@ const handleAliasPath = (
return true;
}

const moduleRoot = findDirectory(resolvedImportPath, 'package.json', '/');
const targetPackageJsonPath = findDirectory(
resolvedImportPath,
'package.json',
'/',
);

if (targetPackageJsonPath) {
if (ignorePackages) {
const currentPackageJsonPath = findDirectory(
context.getFilename(),
'package.json',
'/',
);

if (
currentPackageJsonPath &&
currentPackageJsonPath !== targetPackageJsonPath
) {
return false;
}
}

if (moduleRoot) {
const packageJson = readPackageJson(resolve(moduleRoot, 'package.json'));
const packageJson = readPackageJson(
resolve(targetPackageJsonPath, 'package.json'),
);

if (
packageJson.name &&
Expand All @@ -243,7 +273,10 @@ const handleAliasPath = (
};

export default createRule<Options, MessageIds>({
create: (context) => {
create: (context, [options]) => {
const ignorePackages =
options.ignorePackages ?? defaultOptions.ignorePackages;

const rule = (node: Node) => {
if (!node.source) {
// export { foo };
Expand All @@ -261,7 +294,7 @@ export default createRule<Options, MessageIds>({

void (
handleRelativePath(context, node, importPath) ||
handleAliasPath(context, node, importPath)
handleAliasPath(context, node, importPath, ignorePackages)
);
};

Expand All @@ -271,7 +304,7 @@ export default createRule<Options, MessageIds>({
ImportDeclaration: rule,
};
},
defaultOptions: [],
defaultOptions: [defaultOptions],
meta: {
docs: {
description: 'Require file extension in import and export statements',
Expand Down

0 comments on commit 205ce5f

Please sign in to comment.