@reverecre/eslint-plugin-fontawesome
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -31,2 +31,10 @@ /** | ||
//---------------------------------------------------------------------- | ||
// Variables | ||
//---------------------------------------------------------------------- | ||
const isTypeScript = context.parserPath.includes( | ||
'node_modules/@typescript-eslint/parser' | ||
); | ||
//---------------------------------------------------------------------- | ||
// Helpers | ||
@@ -37,13 +45,41 @@ //---------------------------------------------------------------------- | ||
function generateFixedImports(node) { | ||
const importedIcons = node.specifiers.map((spec) => { | ||
if (spec.type !== 'ImportSpecifier') { | ||
return null; | ||
} | ||
const nonIconImports = node.specifiers | ||
.filter((spec) => { | ||
return ( | ||
spec.type === 'ImportSpecifier' && | ||
!spec.imported.name.startsWith('fa') | ||
); | ||
}) | ||
.map((spec) => { | ||
if (spec.type !== 'ImportSpecifier') { | ||
return null; | ||
} | ||
return `import { ${spec.imported.name}${ | ||
spec.imported.name !== spec.local.name ? ` as ${spec.local.name}` : '' | ||
} } from '${node.source.value}/${spec.imported.name}';`; | ||
}); | ||
return `import ${isTypeScript ? 'type ' : ''}{ ${spec.imported.name}${ | ||
spec.imported.name !== spec.local.name | ||
? ` as ${spec.local.name}` | ||
: '' | ||
} } from '${node.source.value}';`; | ||
}); | ||
return importedIcons.filter(Boolean).join('\n'); | ||
const importedIcons = node.specifiers | ||
.filter((spec) => { | ||
return ( | ||
spec.type === 'ImportSpecifier' && | ||
spec.imported.name.startsWith('fa') | ||
); | ||
}) | ||
.map((spec) => { | ||
if (spec.type !== 'ImportSpecifier') { | ||
return null; | ||
} | ||
return `import { ${spec.imported.name}${ | ||
spec.imported.name !== spec.local.name | ||
? ` as ${spec.local.name}` | ||
: '' | ||
} } from '${node.source.value}/${spec.imported.name}';`; | ||
}); | ||
return [...nonIconImports, ...importedIcons].filter(Boolean).join('\n'); | ||
} | ||
@@ -60,15 +96,24 @@ | ||
if (node.source.value.match(/^@fortawesome\/[\w-]+-icons$/g)) { | ||
context.report({ | ||
node, | ||
messageId: 'badImport', | ||
fix: (fixer) => fixer.replaceText(node, generateFixedImports(node)), | ||
suggest: [ | ||
{ | ||
messageId: 'fixImport', | ||
fix: (fixer) => { | ||
return fixer.replaceText(node, generateFixedImports(node)); | ||
const hasIconImport = node.specifiers.some( | ||
(spec) => | ||
spec.type === 'ImportSpecifier' && | ||
spec.imported.name.startsWith('fa') | ||
); | ||
if (hasIconImport) { | ||
context.report({ | ||
node, | ||
messageId: 'badImport', | ||
fix: (fixer) => | ||
fixer.replaceText(node, generateFixedImports(node)), | ||
suggest: [ | ||
{ | ||
messageId: 'fixImport', | ||
fix: (fixer) => { | ||
return fixer.replaceText(node, generateFixedImports(node)); | ||
}, | ||
}, | ||
}, | ||
], | ||
}); | ||
], | ||
}); | ||
} | ||
} | ||
@@ -75,0 +120,0 @@ }, |
{ | ||
"name": "@reverecre/eslint-plugin-fontawesome", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Ensures proper usage of Font Awesome imports", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -83,2 +83,36 @@ /* eslint-disable node/no-unpublished-require */ | ||
{ | ||
parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
code: `import { IconDefinition, faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons';`, | ||
output: `import { IconDefinition } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
errors: [ | ||
{ | ||
messageId: 'badImport', | ||
suggestions: [ | ||
{ | ||
output: `import { IconDefinition } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, | ||
code: `import { IconDefinition as def, faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons';`, | ||
output: `import { IconDefinition as def } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
errors: [ | ||
{ | ||
messageId: 'badImport', | ||
suggestions: [ | ||
{ | ||
output: `import { IconDefinition as def } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
@@ -128,3 +162,37 @@ code: `import { faCoffee } from '@fortawesome/free-solid-svg-icons';`, | ||
}, | ||
{ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
code: `import { IconDefinition, faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons';`, | ||
output: `import type { IconDefinition } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
errors: [ | ||
{ | ||
messageId: 'badImport', | ||
suggestions: [ | ||
{ | ||
output: `import type { IconDefinition } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
code: `import { IconDefinition as def, faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons';`, | ||
output: `import type { IconDefinition as def } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
errors: [ | ||
{ | ||
messageId: 'badImport', | ||
suggestions: [ | ||
{ | ||
output: `import type { IconDefinition as def } from '@fortawesome/free-solid-svg-icons'; | ||
import { faCoffee as coffeeIcon } from '@fortawesome/free-solid-svg-icons/faCoffee';`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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
15273
330