eslint-plugin: refactor to fix issues uncovered by more strict parsing
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -36,8 +36,10 @@ const manypkg = require('@manypkg/get-packages');
|
||||
|
||||
// Loads all packages in the monorepo once, and caches the result
|
||||
module.exports = (function () {
|
||||
/** @type {PackageMap | undefined} */
|
||||
let result = undefined;
|
||||
let lastLoadAt = undefined;
|
||||
/** @type {number} */
|
||||
let lastLoadAt = 0;
|
||||
|
||||
/** @returns {PackageMap | undefined} */
|
||||
return function getPackages(/** @type {string} */ dir) {
|
||||
@@ -58,11 +60,9 @@ module.exports = (function () {
|
||||
list: packages.packages,
|
||||
root: packages.root,
|
||||
byPath(filePath) {
|
||||
return packages.packages.find(pkg => {
|
||||
if (!path.relative(pkg.dir, filePath).startsWith('..')) {
|
||||
return pkg;
|
||||
}
|
||||
});
|
||||
return packages.packages.find(
|
||||
pkg => !path.relative(pkg.dir, filePath).startsWith('..'),
|
||||
);
|
||||
},
|
||||
};
|
||||
lastLoadAt = Date.now();
|
||||
|
||||
@@ -56,12 +56,17 @@ const getPackages = require('./getPackages');
|
||||
|
||||
/**
|
||||
* @callback ImportVisitor
|
||||
* @param {import('eslint').Rule.Node} node
|
||||
* @param {ConsideredNode} node
|
||||
* @param {LocalImport | InternalImport | ExternalImport | BuiltinImport} import
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {import('estree').ImportDeclaration | import('estree').ExportAllDeclaration | import('estree').ExportNamedDeclaration | import('estree').ImportExpression | import('estree').SimpleCallExpression} node
|
||||
* @typedef ConsideredNode
|
||||
* @type {import('estree').ImportDeclaration | import('estree').ExportAllDeclaration | import('estree').ExportNamedDeclaration | import('estree').ImportExpression | import('estree').SimpleCallExpression}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {ConsideredNode} node
|
||||
* @returns {undefined | {path: string, kind: 'type' | 'value'}}
|
||||
*/
|
||||
function getImportInfo(node) {
|
||||
@@ -103,6 +108,9 @@ module.exports = function visitImports(context, visitor) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ConsideredNode} node
|
||||
*/
|
||||
function visit(node) {
|
||||
const info = getImportInfo(node);
|
||||
if (!info) {
|
||||
|
||||
@@ -37,7 +37,7 @@ const devModulePatterns = [
|
||||
];
|
||||
|
||||
function getExpectedDepType(
|
||||
localPkg,
|
||||
/** @type {any} */ localPkg,
|
||||
/** @type {string} */ impPath,
|
||||
/** @type {string} */ modulePath,
|
||||
) {
|
||||
@@ -58,6 +58,7 @@ function getExpectedDepType(
|
||||
case 'react-router-dom':
|
||||
return 'peer';
|
||||
}
|
||||
break;
|
||||
case 'cli':
|
||||
case 'frontend':
|
||||
case 'backend':
|
||||
@@ -100,10 +101,14 @@ function findConflict(pkg, name, expectedType) {
|
||||
|
||||
return { oldDepsField, depsField };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function getAddFlagForConflict(conflict) {
|
||||
switch (conflict?.depsField) {
|
||||
/**
|
||||
* @param {string} depsField
|
||||
*/
|
||||
function getAddFlagForDepsField(depsField) {
|
||||
switch (depsField) {
|
||||
case depFields.dep:
|
||||
return '';
|
||||
case depFields.dev:
|
||||
@@ -189,7 +194,7 @@ module.exports = {
|
||||
data: {
|
||||
...conflict,
|
||||
packagePath,
|
||||
addFlag: getAddFlagForConflict(conflict),
|
||||
addFlag: getAddFlagForDepsField(conflict.depsField),
|
||||
packageName: imp.packageName,
|
||||
packageJsonPath: packageJsonPath,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user