eslint-plugin-simple-import-sort
Advanced tools
Comparing version 11.0.0 to 12.0.0
@@ -19,4 +19,2 @@ "use strict"; | ||
["^\\."], | ||
// TypeScript import assignments. | ||
["^\\u0001", "^\\u0002"], | ||
]; | ||
@@ -62,3 +60,3 @@ | ||
return { | ||
"ImportDeclaration,TSImportEqualsDeclaration": (node) => { | ||
ImportDeclaration: (node) => { | ||
parents.add(node.parent); | ||
@@ -104,12 +102,10 @@ }, | ||
const { originalSource } = item.source; | ||
const sourceWithControlCharacter = getSourceWithControlCharacter( | ||
originalSource, | ||
item | ||
); | ||
const source = item.isSideEffectImport | ||
? `\0${originalSource}` | ||
: item.source.kind !== "value" | ||
? `${originalSource}\0` | ||
: originalSource; | ||
const [matchedGroup] = shared | ||
.flatMap(itemGroups, (groups) => | ||
groups.map((group) => [ | ||
group, | ||
group.regex.exec(sourceWithControlCharacter), | ||
]) | ||
groups.map((group) => [group, group.regex.exec(source)]) | ||
) | ||
@@ -140,29 +136,5 @@ .reduce( | ||
function getSourceWithControlCharacter(originalSource, item) { | ||
if (item.isSideEffectImport) { | ||
return `\0${originalSource}`; | ||
} | ||
switch (item.source.kind) { | ||
case shared.KIND_VALUE: | ||
return originalSource; | ||
case shared.KIND_TS_IMPORT_ASSIGNMENT_REQUIRE: | ||
return `\u0001${originalSource}`; | ||
case shared.KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE: | ||
return `\u0002${originalSource}`; | ||
default: // `type` and `typeof`. | ||
return `${originalSource}\u0000`; | ||
} | ||
} | ||
// Exclude "ImportDefaultSpecifier" – the "def" in `import def, {a, b}`. | ||
function getSpecifiers(importNode) { | ||
switch (importNode.type) { | ||
case "ImportDeclaration": | ||
return importNode.specifiers.filter((node) => isImportSpecifier(node)); | ||
case "TSImportEqualsDeclaration": | ||
return []; | ||
// istanbul ignore next | ||
default: | ||
throw new Error(`Unsupported import node type: ${importNode.type}`); | ||
} | ||
return importNode.specifiers.filter((node) => isImportSpecifier(node)); | ||
} | ||
@@ -172,6 +144,3 @@ | ||
function isImport(node) { | ||
return ( | ||
node.type === "ImportDeclaration" || | ||
node.type === "TSImportEqualsDeclaration" | ||
); | ||
return node.type === "ImportDeclaration"; | ||
} | ||
@@ -189,19 +158,7 @@ | ||
function isSideEffectImport(importNode, sourceCode) { | ||
switch (importNode.type) { | ||
case "ImportDeclaration": | ||
return ( | ||
importNode.specifiers.length === 0 && | ||
(!importNode.importKind || | ||
importNode.importKind === shared.KIND_VALUE) && | ||
!shared.isPunctuator( | ||
sourceCode.getFirstToken(importNode, { skip: 1 }), | ||
"{" | ||
) | ||
); | ||
case "TSImportEqualsDeclaration": | ||
return false; | ||
// istanbul ignore next | ||
default: | ||
throw new Error(`Unsupported import node type: ${importNode.type}`); | ||
} | ||
return ( | ||
importNode.specifiers.length === 0 && | ||
(!importNode.importKind || importNode.importKind === "value") && | ||
!shared.isPunctuator(sourceCode.getFirstToken(importNode, { skip: 1 }), "{") | ||
); | ||
} |
@@ -9,3 +9,3 @@ "use strict"; | ||
name: "eslint-plugin-simple-import-sort", | ||
version: "11.0.0", | ||
version: "12.0.0", | ||
}, | ||
@@ -12,0 +12,0 @@ rules: { |
{ | ||
"name": "eslint-plugin-simple-import-sort", | ||
"version": "11.0.0", | ||
"version": "12.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "Simon Lydell", |
@@ -167,3 +167,3 @@ "use strict"; | ||
const source = getSource(sourceCode, node); | ||
const source = getSource(node); | ||
@@ -481,3 +481,3 @@ return { | ||
// If there’s a multiline block comment, put everything _befor_ that | ||
// If there’s a multiline block comment, put everything _before_ that | ||
// comment in the specifiers’s `.after`. | ||
@@ -800,4 +800,4 @@ const multilineBlockCommentIndex = after.findIndex( | ||
function getSource(sourceCode, node) { | ||
const [source, kind] = getSourceTextAndKind(sourceCode, node); | ||
function getSource(node) { | ||
const source = node.source.value; | ||
@@ -831,81 +831,10 @@ return { | ||
originalSource: source, | ||
kind, | ||
kind: getImportExportKind(node), | ||
}; | ||
} | ||
function getSourceTextAndKind(sourceCode, node) { | ||
switch (node.type) { | ||
case "ImportDeclaration": | ||
case "ExportNamedDeclaration": | ||
case "ExportAllDeclaration": | ||
return [node.source.value, getImportExportKind(node)]; | ||
case "TSImportEqualsDeclaration": | ||
return getSourceTextAndKindFromModuleReference( | ||
sourceCode, | ||
node.moduleReference | ||
); | ||
// istanbul ignore next | ||
default: | ||
throw new Error(`Unsupported import/export node type: ${node.type}`); | ||
} | ||
} | ||
const KIND_VALUE = "value"; | ||
const KIND_TS_IMPORT_ASSIGNMENT_REQUIRE = "z_require"; | ||
const KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE = "z_namespace"; | ||
function getSourceTextAndKindFromModuleReference(sourceCode, node) { | ||
switch (node.type) { | ||
case "TSExternalModuleReference": | ||
// Only string literals inside `require()` are allowed by | ||
// TypeScript, but the parser supports anything. Sorting | ||
// is defined for string literals only. For other expressions, | ||
// we just make sure not to crash. | ||
switch (node.expression.type) { | ||
case "Literal": | ||
return [ | ||
typeof node.expression.value === "string" | ||
? node.expression.value | ||
: node.expression.raw, | ||
KIND_TS_IMPORT_ASSIGNMENT_REQUIRE, | ||
]; | ||
default: { | ||
const [start, end] = node.expression.range; | ||
return [ | ||
sourceCode.text.slice(start, end), | ||
KIND_TS_IMPORT_ASSIGNMENT_REQUIRE, | ||
]; | ||
} | ||
} | ||
case "TSQualifiedName": | ||
return [ | ||
getSourceTextFromTSQualifiedName(sourceCode, node), | ||
KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE, | ||
]; | ||
case "Identifier": | ||
return [node.name, KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE]; | ||
// istanbul ignore next | ||
default: | ||
throw new Error(`Unsupported module reference node type: ${node.type}`); | ||
} | ||
} | ||
function getSourceTextFromTSQualifiedName(sourceCode, node) { | ||
switch (node.left.type) { | ||
case "Identifier": | ||
return `${node.left.name}.${node.right.name}`; | ||
case "TSQualifiedName": | ||
return `${getSourceTextFromTSQualifiedName(sourceCode, node.left)}.${ | ||
node.right.name | ||
}`; | ||
// istanbul ignore next | ||
default: | ||
throw new Error(`Unsupported TS qualified name node type: ${node.type}`); | ||
} | ||
} | ||
function getImportExportKind(node) { | ||
// `type` and `typeof` imports, as well as `type` exports (there are no | ||
// `typeof` exports). | ||
return node.importKind || node.exportKind || KIND_VALUE; | ||
return node.importKind || node.exportKind || "value"; | ||
} | ||
@@ -943,5 +872,2 @@ | ||
isPunctuator, | ||
KIND_TS_IMPORT_ASSIGNMENT_NAMESPACE, | ||
KIND_TS_IMPORT_ASSIGNMENT_REQUIRE, | ||
KIND_VALUE, | ||
maybeReportSorting, | ||
@@ -948,0 +874,0 @@ printSortedItems, |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
0
37782
1014