eslint-plugin-inker
Advanced tools
Comparing version 2.8.0 to 2.8.1
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const minimatch_1 = require("minimatch"); | ||
const lodash_1 = require("lodash"); | ||
const mergeMaps_1 = __importDefault(require("../utils/mergeMaps")); | ||
const importNamesSchema = { | ||
@@ -80,22 +84,25 @@ type: 'array', | ||
const options = (_a = context.options[0]) !== null && _a !== void 0 ? _a : {}; | ||
const pathsTransformed = options.paths.map(item => ({ | ||
...item, | ||
localByImported: new Map(item.importNames.map(o => [o.imported, o.local])), | ||
})); | ||
const findPaths = (name) => pathsTransformed.filter(item => { | ||
if ('name' in item) { | ||
return item.name === name; | ||
} | ||
const patterns = (0, lodash_1.castArray)(item.pattern); | ||
return patterns.some(pattern => (0, minimatch_1.minimatch)(name, pattern)); | ||
}); | ||
return { | ||
ImportDeclaration(node) { | ||
const { source, specifiers, } = node; | ||
const foundPaths = options.paths.filter(item => { | ||
if ('name' in item) { | ||
return item.name === source.value; | ||
} | ||
const patterns = (0, lodash_1.castArray)(item.pattern); | ||
return patterns.some(pattern => (0, minimatch_1.minimatch)(source.value, pattern)); | ||
}); | ||
const foundPaths = findPaths(source.value); | ||
if (foundPaths.length === 0) { | ||
return; | ||
} | ||
const importNames = foundPaths.flatMap(item => item.importNames); | ||
const localByImported = new Map(foundPaths.flatMap(item => [...item.localByImported])); | ||
const issuesWithGaps = specifiers.map((s) => { | ||
const importNamesNotMatchingOptions = importNames.filter(o => o.local !== s.local.name); | ||
if (s.type === 'ImportSpecifier') { | ||
const foundImportedName = importNamesNotMatchingOptions.find(o => o.imported === s.imported.name); | ||
const suggestedName = foundImportedName === null || foundImportedName === void 0 ? void 0 : foundImportedName.local; | ||
return suggestedName !== undefined && { | ||
const suggestedName = localByImported.get(s.imported.name); | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -119,5 +126,4 @@ message: `Local name should be "${suggestedName}"`, | ||
if (s.type === 'ImportDefaultSpecifier') { | ||
const foundImportedName = importNamesNotMatchingOptions.find(o => o.imported === 'default'); | ||
const suggestedName = foundImportedName === null || foundImportedName === void 0 ? void 0 : foundImportedName.local; | ||
return suggestedName !== undefined && { | ||
const suggestedName = localByImported.get('default'); | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -141,5 +147,4 @@ message: `Local name should be "${suggestedName}"`, | ||
if (s.type === 'ImportNamespaceSpecifier') { | ||
const foundImportedName = importNamesNotMatchingOptions.find(o => o.imported === 'namespace'); | ||
const suggestedName = foundImportedName === null || foundImportedName === void 0 ? void 0 : foundImportedName.local; | ||
return suggestedName !== undefined && { | ||
const suggestedName = localByImported.get('namespace'); | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -169,4 +174,80 @@ message: `Local name should be "${suggestedName}"`, | ||
}, | ||
VariableDeclaration(node) { | ||
const { id, init } = node.declarations[0]; | ||
const isRequire = init | ||
&& init.type === 'CallExpression' | ||
&& init.callee.type === 'Identifier' | ||
&& init.callee.name === 'require'; | ||
if (!isRequire) { | ||
return; | ||
} | ||
const source = init.arguments[0]; | ||
const foundPaths = findPaths(source.value); | ||
if (foundPaths.length === 0) { | ||
return; | ||
} | ||
const localByImported = (0, mergeMaps_1.default)(foundPaths.map(item => item.localByImported)); | ||
const issuesWithGaps = (() => { | ||
var _a; | ||
if (id.type === 'ObjectPattern') { | ||
return id.properties.map(prop => { | ||
const { type: propType } = prop; | ||
if (propType !== 'Property') { | ||
return false; | ||
} | ||
const { key, value } = prop; | ||
if (key.type !== 'Identifier' || value.type !== 'Identifier') { | ||
return false; | ||
} | ||
const suggestedName = localByImported.get(key.name); | ||
return !!suggestedName && suggestedName !== value.name && { | ||
node: value, | ||
message: `Local name should be "${suggestedName}"`, | ||
suggest: [ | ||
{ | ||
desc: `Rename to '${suggestedName}'`, | ||
fix(fixer) { | ||
var _a, _b; | ||
const references = (_b = (_a = context.getDeclaredVariables(prop)[0]) === null || _a === void 0 ? void 0 : _a.references) !== null && _b !== void 0 ? _b : []; | ||
return [ | ||
fixer.replaceText(prop, `${key.name}: ${suggestedName}`), | ||
...references.map(ref => fixer.replaceText(ref.identifier, suggestedName)), | ||
]; | ||
}, | ||
}, | ||
], | ||
}; | ||
}); | ||
} | ||
if (id.type === 'Identifier') { | ||
const suggestedName = (_a = localByImported.get('default')) !== null && _a !== void 0 ? _a : localByImported.get('namespace'); | ||
return [ | ||
!!suggestedName && suggestedName !== id.name && { | ||
node: id, | ||
message: `Local name should be "${suggestedName}"`, | ||
suggest: [ | ||
{ | ||
desc: `Rename to '${suggestedName}'`, | ||
fix(fixer) { | ||
var _a, _b; | ||
const references = (_b = (_a = context.getDeclaredVariables(id)[0]) === null || _a === void 0 ? void 0 : _a.references) !== null && _b !== void 0 ? _b : []; | ||
return [ | ||
id, | ||
...references.map(ref => ref.identifier), | ||
].map(identifier => fixer.replaceText(identifier, suggestedName)); | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
return []; | ||
})(); | ||
const issues = (0, lodash_1.compact)(issuesWithGaps); | ||
for (const issue of issues) { | ||
context.report(issue); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
@@ -20,30 +20,2 @@ "use strict"; | ||
} | ||
const getHandler = (context) => (options) => { | ||
var _a, _b; | ||
const { loc } = options.node; | ||
if (!loc || loc.start.line === loc.end.line) { | ||
return; | ||
} | ||
const children = (0, invokeIfFunction_1.default)(options.children); | ||
if (children.length < 2) { | ||
return; | ||
} | ||
const isFirstItemSameLine = ((_a = children[0].loc) === null || _a === void 0 ? void 0 : _a.start.line) === loc.start.line; | ||
// eslint-disable-next-line unicorn/prefer-at | ||
const isLastItemSameLine = ((_b = children[children.length - 1].loc) === null || _b === void 0 ? void 0 : _b.end.line) === loc.end.line; | ||
if (isFirstItemSameLine || isLastItemSameLine) { | ||
return; | ||
} | ||
const problematicNodes = findSameLineNodes(children); | ||
const { message } = options; | ||
for (const problematicNode of problematicNodes) { | ||
context.report({ | ||
node: problematicNode, | ||
message, | ||
fix(fixer) { | ||
return fixer.insertTextBefore(problematicNode, '\n'); | ||
}, | ||
}); | ||
} | ||
}; | ||
exports.default = { | ||
@@ -54,3 +26,30 @@ meta: { | ||
create(context) { | ||
const handle = getHandler(context); | ||
const handle = (options) => { | ||
var _a, _b; | ||
const { loc } = options.node; | ||
if (!loc || loc.start.line === loc.end.line) { | ||
return; | ||
} | ||
const children = (0, invokeIfFunction_1.default)(options.children); | ||
if (children.length < 2) { | ||
return; | ||
} | ||
const isFirstItemSameLine = ((_a = children[0].loc) === null || _a === void 0 ? void 0 : _a.start.line) === loc.start.line; | ||
// eslint-disable-next-line unicorn/prefer-at | ||
const isLastItemSameLine = ((_b = children[children.length - 1].loc) === null || _b === void 0 ? void 0 : _b.end.line) === loc.end.line; | ||
if (isFirstItemSameLine || isLastItemSameLine) { | ||
return; | ||
} | ||
const problematicNodes = findSameLineNodes(children); | ||
const { message } = options; | ||
for (const problematicNode of problematicNodes) { | ||
context.report({ | ||
node: problematicNode, | ||
message, | ||
fix(fixer) { | ||
return fixer.insertTextBefore(problematicNode, '\n'); | ||
}, | ||
}); | ||
} | ||
}; | ||
return { | ||
@@ -57,0 +56,0 @@ FunctionDeclaration(node) { |
{ | ||
"name": "eslint-plugin-inker", | ||
"version": "2.8.0", | ||
"version": "2.8.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -20,2 +20,4 @@ import { | ||
import mergeMaps from '../utils/mergeMaps' | ||
interface ImportNameObj { | ||
@@ -121,6 +123,13 @@ imported: string, | ||
const pathsTransformed = options.paths.map(item => ({ | ||
...item, | ||
localByImported: new Map( | ||
item.importNames.map(o => [o.imported, o.local] as const), | ||
), | ||
})) | ||
type ReportDescriptor = Parameters<typeof context.report>[0] | ||
const findPaths = (name: string) => | ||
options.paths.filter(item => { | ||
pathsTransformed.filter(item => { | ||
if ('name' in item) { | ||
@@ -145,13 +154,7 @@ return item.name === name | ||
const importNames = foundPaths.flatMap(item => item.importNames) | ||
const localByImported = new Map(foundPaths.flatMap(item => [...item.localByImported])) | ||
const issuesWithGaps = specifiers.map((s): ReportDescriptor | false => { | ||
if (s.type === 'ImportSpecifier') { | ||
const foundImportedName = importNames.find( | ||
o => o.imported === s.imported.name, | ||
) | ||
if (!foundImportedName) { | ||
return false | ||
} | ||
const suggestedName = foundImportedName.local | ||
return suggestedName !== s.local.name && { | ||
const suggestedName = localByImported.get(s.imported.name) | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -175,10 +178,4 @@ message: `Local name should be "${suggestedName}"`, | ||
if (s.type === 'ImportDefaultSpecifier') { | ||
const foundImportedName = importNames.find( | ||
o => o.imported === 'default', | ||
) | ||
if (!foundImportedName) { | ||
return false | ||
} | ||
const suggestedName = foundImportedName.local | ||
return suggestedName !== s.local.name && { | ||
const suggestedName = localByImported.get('default') | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -202,10 +199,4 @@ message: `Local name should be "${suggestedName}"`, | ||
if (s.type === 'ImportNamespaceSpecifier') { | ||
const foundImportedName = importNames.find( | ||
o => o.imported === 'namespace', | ||
) | ||
if (!foundImportedName) { | ||
return false | ||
} | ||
const suggestedName = foundImportedName.local | ||
return suggestedName !== s.local.name && { | ||
const suggestedName = localByImported.get('namespace') | ||
return !!suggestedName && suggestedName !== s.local.name && { | ||
node: s.local, | ||
@@ -230,2 +221,3 @@ message: `Local name should be "${suggestedName}"`, | ||
}) | ||
const issues = compact(issuesWithGaps) | ||
@@ -239,12 +231,3 @@ | ||
VariableDeclaration(node) { | ||
const { | ||
type, | ||
declarations, | ||
} = node | ||
if (type !== 'VariableDeclaration') { | ||
return | ||
} | ||
const { id, init } = declarations[0] | ||
const { id, init } = node.declarations[0] | ||
const isRequire = init | ||
@@ -264,3 +247,3 @@ && init.type === 'CallExpression' | ||
const importNames = foundPaths.flatMap(item => item.importNames) | ||
const localByImported = mergeMaps(foundPaths.map(item => item.localByImported)) | ||
const issuesWithGaps = ((): (ReportDescriptor | false)[] => { | ||
@@ -277,12 +260,4 @@ if (id.type === 'ObjectPattern') { | ||
} | ||
const keyName = key.name | ||
const valueName = value.name | ||
const foundImportedName = importNames.find( | ||
o => o.imported === keyName, | ||
) | ||
if (!foundImportedName) { | ||
return false | ||
} | ||
const suggestedName = foundImportedName.local | ||
return suggestedName !== valueName && { | ||
const suggestedName = localByImported.get(key.name) | ||
return !!suggestedName && suggestedName !== value.name && { | ||
node: value, | ||
@@ -296,3 +271,3 @@ message: `Local name should be "${suggestedName}"`, | ||
return [ | ||
fixer.replaceText(prop, `${keyName}: ${suggestedName}`), | ||
fixer.replaceText(prop, `${key.name}: ${suggestedName}`), | ||
...references.map(ref => fixer.replaceText(ref.identifier, suggestedName)), | ||
@@ -308,11 +283,5 @@ ] | ||
if (id.type === 'Identifier') { | ||
const foundImportedName = importNames.find( | ||
o => o.imported === 'default' || o.imported === 'namespace', | ||
) | ||
if (!foundImportedName) { | ||
return [] | ||
} | ||
const suggestedName = foundImportedName.local | ||
const suggestedName = localByImported.get('default') ?? localByImported.get('namespace') | ||
return [ | ||
suggestedName !== id.name && { | ||
!!suggestedName && suggestedName !== id.name && { | ||
node: id, | ||
@@ -319,0 +288,0 @@ message: `Local name should be "${suggestedName}"`, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61091
43
1687