eslint-plugin-beautiful-sort
Advanced tools
Comparing version 2.0.4 to 3.0.0
@@ -41,3 +41,3 @@ "use strict"; | ||
const sortedImports = (0, sort_imports_utils_1.getSortedImports)(imports, { special, sortTable }); | ||
(0, sort_imports_utils_1.reportErrors)(context, imports, sortedImports); | ||
(0, sort_imports_utils_1.reportErrors)(context, program, imports, sortedImports); | ||
}, | ||
@@ -44,0 +44,0 @@ }; |
@@ -7,3 +7,3 @@ import { Rule } from 'eslint'; | ||
export declare const isEmpty: <T>(array: T[]) => boolean; | ||
export declare type SortTable = { | ||
export type SortTable = { | ||
[key in Type]?: number; | ||
@@ -15,3 +15,3 @@ } & { | ||
export declare const getSortedImports: (imports: ImportDeclaration[], params: ImportNodeParams) => ImportDeclaration[]; | ||
export declare const reportErrors: (context: Rule.RuleContext, imports: ImportDeclaration[], sortedImports: ImportDeclaration[]) => void; | ||
export declare const reportErrors: (context: Rule.RuleContext, program: Program, imports: ImportDeclaration[], sortedImports: ImportDeclaration[]) => void; | ||
export declare const parseStringSpecial: (stringSpecial: string[]) => RegExp[]; |
@@ -7,3 +7,5 @@ "use strict"; | ||
const getImports = (program) => { | ||
return program.body.filter((node) => node.type === IMPORT_AST_TYPE); | ||
return program.body.filter((node) => { | ||
return node.type === IMPORT_AST_TYPE; | ||
}); | ||
}; | ||
@@ -29,18 +31,30 @@ exports.getImports = getImports; | ||
exports.getSortedImports = getSortedImports; | ||
const reportErrors = (context, imports, sortedImports) => { | ||
const reportErrors = (context, program, imports, sortedImports) => { | ||
const sourceCode = context.getSourceCode(); | ||
for (let i = 0; i < sortedImports.length; i++) { | ||
const node = sortedImports[i]; | ||
const finded = imports[i]; | ||
const inImportIndex = imports.findIndex((innerNode) => innerNode === node); // compare by object links; | ||
if (finded !== node) { | ||
const originalPlace = inImportIndex + 1; | ||
const newPlace = i + 1; | ||
context.report({ | ||
message: `${sourceCode.getText(node)} must be ${newPlace}, but it is ${originalPlace}`, | ||
node: node, | ||
fix: (fixer) => fixer.replaceTextRange(finded.range, sourceCode.getText(node)), | ||
}); | ||
} | ||
} | ||
const shouldFix = sortedImports.some((node, i) => { | ||
return imports[i] !== node; | ||
}); | ||
if (!shouldFix) | ||
return; | ||
const message = `Replace imports to: ${sortedImports | ||
.map((node) => `\`${sourceCode.getText(node).replace(';', '')}\``) | ||
.join(', ')}`; | ||
const end = sortedImports.at(-1)?.loc?.end; | ||
const start = program.loc?.start; | ||
context.report({ | ||
loc: { | ||
start, | ||
end, | ||
}, | ||
message, | ||
fix: function* (fixer) { | ||
for (let i = 0; i < sortedImports.length; i++) { | ||
const node = sortedImports[i]; | ||
const found = imports[i]; | ||
if (found !== node) { | ||
yield fixer.replaceText(found, sourceCode.getText(node)); | ||
} | ||
} | ||
}, | ||
}); | ||
}; | ||
@@ -47,0 +61,0 @@ exports.reportErrors = reportErrors; |
{ | ||
"name": "eslint-plugin-beautiful-sort", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"description": "eslint plugin for imports sort by their type", | ||
@@ -25,20 +25,20 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@commitlint/cli": "^16.1.0", | ||
"@commitlint/config-conventional": "^16.0.0", | ||
"@types/eslint": "^8.4.1", | ||
"@types/jest": "^27.4.0", | ||
"@typescript-eslint/eslint-plugin": "^5.10.2", | ||
"@typescript-eslint/parser": "^5.10.2", | ||
"eslint": "^8.8.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-beautiful-sort": "^1.0.7", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"husky": "^7.0.4", | ||
"jest": "^27.4.7", | ||
"lint-staged": "^12.3.3", | ||
"prettier": "^2.5.1", | ||
"rimraf": "^3.0.2", | ||
"standard-version": "^9.3.2", | ||
"ts-jest": "^27.1.3", | ||
"typescript": "^4.5.5" | ||
"@commitlint/cli": "^17.5.1", | ||
"@commitlint/config-conventional": "^17.4.4", | ||
"@types/eslint": "^8.37.0", | ||
"@types/jest": "^29.5.0", | ||
"@typescript-eslint/eslint-plugin": "^5.57.1", | ||
"@typescript-eslint/parser": "^5.57.1", | ||
"eslint": "^8.38.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-beautiful-sort": "^2.0.4", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"husky": "^8.0.3", | ||
"jest": "^29.5.0", | ||
"lint-staged": "^13.2.1", | ||
"prettier": "^2.8.7", | ||
"rimraf": "^4.4.1", | ||
"standard-version": "^9.5.0", | ||
"ts-jest": "^29.1.0", | ||
"typescript": "^5.0.4" | ||
}, | ||
@@ -65,4 +65,4 @@ "keywords": [ | ||
"engines": { | ||
"node": ">=16.13.2", | ||
"npm": ">=8.1.2" | ||
"node": ">=18.14.0", | ||
"npm": ">=9.3.1" | ||
}, | ||
@@ -69,0 +69,0 @@ "lint-staged": { |
@@ -33,5 +33,5 @@ # eslint-plugin-beautiful-sort | ||
{ | ||
"node": "^16.13.2", | ||
"npm": "^8.1.2", | ||
"eslint": "^8.8.0" | ||
"node": "^18.14.0", | ||
"npm": "^9.3.1", | ||
"eslint": "^8.38.0" | ||
} | ||
@@ -38,0 +38,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
55801
319