-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add internal rule to prevent deep import from plugins/js
- Loading branch information
Showing
5 changed files
with
130 additions
and
2 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
11 changes: 11 additions & 0 deletions
11
tools/eslint-rules/rules/restrict-js-plugin-deep-import.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TSESLint } from '@typescript-eslint/utils'; | ||
import { rule, RULE_NAME } from './restrict-js-plugin-deep-import'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
}); | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [`const example = true;`], | ||
invalid: [], | ||
}); |
104 changes: 104 additions & 0 deletions
104
tools/eslint-rules/rules/restrict-js-plugin-deep-import.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* This file sets you up with structure needed for an ESLint rule. | ||
* | ||
* It leverages utilities from @typescript-eslint to allow TypeScript to | ||
* provide autocompletions etc for the configuration. | ||
* | ||
* Your rule's custom logic will live within the create() method below | ||
* and you can learn more about writing ESLint rules on the official guide: | ||
* | ||
* https://eslint.org/docs/developer-guide/working-with-rules | ||
* | ||
* You can also view many examples of existing rules here: | ||
* | ||
* https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin/src/rules | ||
*/ | ||
|
||
import { normalizePath, workspaceRoot } from '@nrwl/devkit'; | ||
import { | ||
ESLintUtils, | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/utils'; | ||
|
||
// NOTE: The rule will be available in ESLint configs as "@nrwl/nx/workspace/restrict-js-plugin-deep-import" | ||
export const RULE_NAME = 'restrict-js-plugin-deep-import'; | ||
|
||
export const rule = ESLintUtils.RuleCreator(() => __filename)({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: `Ensure that there are no deep imports from nx/src/plugins/js`, | ||
recommended: 'error', | ||
}, | ||
schema: [], | ||
messages: { | ||
noDeepImport: | ||
'Functions from "nx/src/plugins/js" should be imported via barrel import. Deep import found: {{imp}}', | ||
noDeepRelativeImport: | ||
'Functions from "./plugins/js" should be imported via relative barrel import. Deep import found: {{imp}}', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
function run( | ||
node: | ||
| TSESTree.ImportDeclaration | ||
| TSESTree.ImportExpression | ||
| TSESTree.ExportAllDeclaration | ||
| TSESTree.ExportNamedDeclaration | ||
) { | ||
// Ignoring ExportNamedDeclarations like: | ||
// export class Foo {} | ||
if (!node.source) { | ||
return; | ||
} | ||
|
||
// accept only literals because template literals have no value | ||
if (node.source.type !== AST_NODE_TYPES.Literal) { | ||
return; | ||
} | ||
const imp = node.source.value as string; | ||
if (imp.includes('nx/src/plugins/js/')) { | ||
context.report({ | ||
node, | ||
messageId: 'noDeepImport', | ||
data: { | ||
imp, | ||
}, | ||
}); | ||
} | ||
const fileName = normalizePath(context.getFilename()).slice( | ||
workspaceRoot.length + 1 | ||
); | ||
if ( | ||
imp.includes('./plugins/js/') && | ||
fileName.startsWith('packages/nx/') | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'noDeepRelativeImport', | ||
data: { | ||
imp, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
ImportDeclaration(node: TSESTree.ImportDeclaration) { | ||
run(node); | ||
}, | ||
ImportExpression(node: TSESTree.ImportExpression) { | ||
run(node); | ||
}, | ||
ExportAllDeclaration(node: TSESTree.ExportAllDeclaration) { | ||
run(node); | ||
}, | ||
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { | ||
run(node); | ||
}, | ||
}; | ||
}, | ||
}); |