-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): new
no-deep-imports
-rule (#2666)
- Loading branch information
1 parent
3c9378b
commit 467420e
Showing
15 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
projects/addon-editor/constants/default-font-options-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
projects/demo/src/modules/components/input-card-grouped/input-card-grouped.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const MESSAGE_ID = `no-deep-imports`; | ||
const ERROR_MESSAGE = `Deep imports of Taiga UI packages are prohibited`; | ||
|
||
const DEFAULT_OPTIONS = { | ||
importDeclaration: `^@taiga-ui*`, | ||
deepImport: `(?<=^@taiga-ui/[\\w-]+)(/.+)$`, | ||
currentProject: ``, | ||
ignoreImports: [], | ||
}; | ||
|
||
/** | ||
* @type {import('eslint').Rule.RuleModule} | ||
*/ | ||
module.exports = { | ||
meta: { | ||
type: `problem`, | ||
docs: {description: ERROR_MESSAGE}, | ||
messages: { | ||
[MESSAGE_ID]: ERROR_MESSAGE, | ||
}, | ||
fixable: `code`, | ||
schema: [ | ||
{ | ||
type: `object`, | ||
properties: { | ||
importDeclaration: { | ||
type: `string`, | ||
description: `RegExp string to detect import declarations for which this rule should be applied`, | ||
}, | ||
deepImport: { | ||
type: `string`, | ||
description: `RegExp string to pick out deep import part`, | ||
}, | ||
currentProject: { | ||
type: `string`, | ||
description: `RegExp string to pick out current project name of processed file`, | ||
}, | ||
ignoreImports: { | ||
type: `array`, | ||
items: { | ||
type: `string`, | ||
}, | ||
description: `RegExp string to exclude import declarations which is selected by importDeclaration-option`, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
create(context) { | ||
const {importDeclaration, deepImport, currentProject, ignoreImports} = { | ||
...DEFAULT_OPTIONS, | ||
...(context.options[0] || {}), | ||
}; | ||
|
||
return { | ||
[`ImportDeclaration[source.value=/${importDeclaration}/]`]({ | ||
source: sourceNode, | ||
}) { | ||
const currentFilePath = context.getFilename(); | ||
const [currentFileProjectName] = | ||
(currentProject && | ||
currentFilePath.match(new RegExp(currentProject, 'g'))) || | ||
[]; | ||
const importSource = sourceNode?.value || ``; | ||
const isInsideTheSameEntryPoint = Boolean( | ||
currentFileProjectName && | ||
importSource.includes(currentFileProjectName), | ||
); | ||
|
||
if ( | ||
isInsideTheSameEntryPoint || | ||
ignoreImports.some(p => importSource.match(new RegExp(p, 'g'))) | ||
) { | ||
return; | ||
} | ||
|
||
if (importSource.match(new RegExp(deepImport, 'g'))?.length) { | ||
context.report({ | ||
node: sourceNode, | ||
messageId: MESSAGE_ID, | ||
fix: fixer => { | ||
const [start, end] = sourceNode.range; | ||
|
||
return fixer.replaceTextRange( | ||
[start + 1, end - 1], // keeps quotes | ||
importSource.replace(new RegExp(deepImport, 'g'), ``), | ||
); | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
3 changes: 1 addition & 2 deletions
3
projects/kit/components/line-clamp/line-clamp-box.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters