33import tseslint from "typescript-eslint" ;
44import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended" ;
55
6+ import { TSESTree } from "@typescript-eslint/utils" ;
7+ import { RuleContext } from "@typescript-eslint/utils/dist/ts-eslint" ;
8+
9+ type Node = TSESTree . ImportDeclaration ;
10+
11+ type CustomRuleOptions = {
12+ locations : string [ ] ;
13+ message ?: string ;
14+ fixedLocation ?: string ;
15+ ignoreTypeImports ?: boolean ;
16+ } ;
17+
618export default tseslint . config (
719 // https://typescript-eslint.io/getting-started/typed-linting/
820 tseslint . configs . strictTypeChecked ,
@@ -27,7 +39,7 @@ export default tseslint.config(
2739 } ,
2840 ] ,
2941 } ,
30- create : function ( context ) {
42+ create : function ( context : RuleContext < "bannedImport" , Node [ ] > ) {
3143 const filePath = context . getFilename ( ) ;
3244 const options = context . options [ 0 ] || {
3345 "^/(.*)" : {
@@ -36,8 +48,8 @@ export default tseslint.config(
3648 } ;
3749
3850 return {
39- ImportDeclaration : ( node ) => {
40- Object . entries ( options ) . forEach ( ( [ bannedImport , config ] ) => {
51+ ImportDeclaration : ( node : Node ) => {
52+ Object . entries ( options ) . forEach ( ( [ bannedImport , config ] : [ string , CustomRuleOptions ] ) => {
4153 const importLocationRegex = new RegExp ( bannedImport ) ;
4254
4355 if ( config . ignoreTypeImports && node . importKind === "type" ) return ;
@@ -50,6 +62,7 @@ export default tseslint.config(
5062 node . specifiers . forEach ( ( specifier ) => {
5163 if ( specifier . type !== "ImportDefaultSpecifier" ) {
5264 context . report ( {
65+ // @ts -expect-error `message` seems to work with this...
5366 message : config . message ?? `Importing from '${ bannedImport } ' is banned in '${ fp } '` ,
5467 node,
5568 } ) ;
0 commit comments