2022-08-25 12:42:15 +02:00
'use strict' ;
/ *
* A eslint rule that ensures the use of the ` import type ` operator from the ` src/core/models/index.ts ` file .
* /
// eslint-disable-next-line no-undef
module . exports = {
2023-01-03 15:51:37 +01:00
/** @type {import('eslint').Rule.RuleModule} */
2022-08-25 12:42:15 +02:00
'bad-type-import' : {
meta : {
2023-01-04 10:34:40 +01:00
type : 'problem' ,
2022-08-25 12:42:15 +02:00
docs : {
description : 'Ensures the use of the `import type` operator from the `src/core/models/index.ts` file.' ,
category : 'Best Practices' ,
recommended : true ,
} ,
fixable : 'code' ,
schema : [ ] ,
} ,
create : function ( context ) {
return {
ImportDeclaration : function ( node ) {
2023-01-03 15:51:37 +01:00
if ( node . source . parent . importKind !== 'type' && ( node . source . value . endsWith ( '/models' ) || node . source . value === 'router-slot/model' ) ) {
2022-08-25 12:42:15 +02:00
const sourceCode = context . getSourceCode ( ) ;
const nodeSource = sourceCode . getText ( node ) ;
context . report ( {
node ,
message : 'Use `import type` instead of `import`.' ,
fix : fixer => fixer . replaceText ( node , nodeSource . replace ( 'import' , 'import type' ) ) ,
} ) ;
}
} ,
} ;
}
2023-01-03 15:51:37 +01:00
} ,
/** @type {import('eslint').Rule.RuleModule} */
'no-direct-api-import' : {
meta : {
type : 'suggestion' ,
docs : {
description : 'Ensures that any API resources from the `@umbraco-cms/backend-api` module are not used directly. Instead you should use the `tryExecuteAndNotify` function from the `@umbraco-cms/resources` module.' ,
category : 'Best Practices' ,
recommended : true
} ,
fixable : 'code' ,
schema : [ ] ,
} ,
create : function ( context ) {
return {
// If methods called on *Resource classes are not already wrapped with `await tryExecuteAndNotify()`, then we should suggest to wrap them.
CallExpression : function ( node ) {
if ( node . callee . type === 'MemberExpression' && node . callee . object . type === 'Identifier' && node . callee . object . name . endsWith ( 'Resource' ) && node . callee . property . type === 'Identifier' && node . callee . property . name !== 'constructor' ) {
2023-01-04 10:23:44 +01:00
const hasTryExecuteAndNotify = node . parent && node . parent . callee && ( node . parent . callee . name === 'tryExecute' || node . parent . callee . name === 'tryExecuteAndNotify' ) ;
2023-01-03 15:51:37 +01:00
if ( ! hasTryExecuteAndNotify ) {
context . report ( {
node ,
message : 'Wrap this call with `tryExecuteAndNotify()`. Make sure to `await` the result.' ,
fix : fixer => [ fixer . insertTextBefore ( node , 'tryExecuteAndNotify(this, ' ) , fixer . insertTextAfter ( node , ')' ) ] ,
} ) ;
}
}
}
} ;
} ,
2022-08-25 12:42:15 +02:00
}
} ;