Skip to content

Commit 9d99c66

Browse files
eslint-plugin: Use new no-restricted-imports rule for v9 config (#23845)
* Add getPackageJson module * update package.json * update v9 lint config to use no-restricted-imports * Change File * fix dependency mismatch * nit: remove duplicate import * nit: move getPackageJson to configHelpers
1 parent 0921b7f commit 9d99c66

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "chore: use no-restricted-import rule for v9 lint config.",
4+
"packageName": "@fluentui/eslint-plugin",
5+
"email": "[email protected]",
6+
"dependentChangeType": "none"
7+
}

packages/eslint-plugin/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"eslint-plugin-jsx-a11y": "^6.4.1",
2828
"eslint-plugin-react": "^7.24.0",
2929
"eslint-plugin-react-hooks": "^4.2.0",
30+
"fs-extra": "^8.1.0",
3031
"minimatch": "^3.0.4",
32+
"@nrwl/tao": "13.8.1",
3133
"jju": "^1.4.0"
3234
},
3335
"peerDependencies": {

packages/eslint-plugin/src/configs/react.js

+21
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@ const typeAwareRules = {
88
'@fluentui/ban-context-export': ['error', { exclude: ['**/react-shared-contexts/**'] }],
99
};
1010

11+
const root = configHelpers.findGitRoot();
12+
const v9PackageDeps = Object.keys(
13+
configHelpers.getPackageJson({ root, name: '@fluentui/react-components' }).dependencies,
14+
);
15+
1116
/** @type {import("eslint").Linter.Config} */
1217
module.exports = {
1318
extends: [path.join(__dirname, 'base'), path.join(__dirname, 'react-config')],
1419
rules: {},
1520
overrides: [
1621
// Enable rules requiring type info only for appropriate files/circumstances
1722
...configHelpers.getTypeInfoRuleOverrides(typeAwareRules),
23+
{
24+
files: '**/*.stories.tsx',
25+
rules: {
26+
'@fluentui/no-restricted-imports': [
27+
'warn',
28+
{
29+
paths: [
30+
{
31+
forbidden: v9PackageDeps,
32+
preferred: '@fluentui/react-components',
33+
},
34+
],
35+
},
36+
],
37+
},
38+
},
1839
],
1940
};

packages/eslint-plugin/src/utils/configHelpers.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// @ts-check
22

3-
const fs = require('fs');
3+
const fs = require('fs-extra');
44
const path = require('path');
55
const jju = require('jju');
66

7+
/**
8+
* @typedef {{root: string, name: string}} Options
9+
* @typedef {{name: string, version: string, dependencies: {[key: string]: string}}} PackageJson
10+
* @typedef {import("@nrwl/tao/src/shared/workspace").WorkspaceJsonConfiguration} WorkspaceJsonConfiguration
11+
*/
12+
713
const testFiles = [
814
'**/*{.,-}{test,spec,e2e}.{ts,tsx}',
915
'**/{test,tests}/**',
@@ -218,4 +224,20 @@ module.exports = {
218224
}
219225
return cwd;
220226
},
227+
228+
/**
229+
* Gets package.json of provided package name.
230+
* @param {Options} options Takes provided root folder of git repo and package name.
231+
* @returns {PackageJson} package.json file of the provided package name.
232+
*/
233+
getPackageJson: (/** @type {Options} */ options) => {
234+
/** @type {WorkspaceJsonConfiguration} */
235+
const nxWorkspace = JSON.parse(fs.readFileSync(path.join(options.root, 'workspace.json'), 'utf-8'));
236+
const projectMetaData = nxWorkspace.projects[options.name];
237+
const packagePath = path.join(options.root, projectMetaData.root);
238+
/** @type {PackageJson} */
239+
const packageJson = fs.readJSONSync(path.join(packagePath, 'package.json'));
240+
241+
return packageJson;
242+
},
221243
};

0 commit comments

Comments
 (0)