eslint-plugin-storybook 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,36 @@
1
+ # v0.6.2 (Tue Aug 02 2022)
2
+
3
+ ### Release Notes
4
+
5
+ #### feat(no-uninstalled-addons): add option for a custom package.json location ([#102](https://github.com/storybookjs/eslint-plugin-storybook/pull/102))
6
+
7
+ #### feat(no-uninstalled-addons): add option for a custom package.json location ([#102](https://github.com/storybookjs/eslint-plugin-storybook/pull/102))
8
+
9
+ ---
10
+
11
+ #### 🐛 Bug Fix
12
+
13
+ - feat(no-uninstalled-addons): add option for a custom package.json location [#102](https://github.com/storybookjs/eslint-plugin-storybook/pull/102) ([@andrelas1](https://github.com/andrelas1) [@yannbf](https://github.com/yannbf))
14
+
15
+ #### Authors: 2
16
+
17
+ - Andre Luis Araujo Santos ([@andrelas1](https://github.com/andrelas1))
18
+ - Yann Braga ([@yannbf](https://github.com/yannbf))
19
+
20
+ ---
21
+
22
+ # v0.6.1 (Tue Jul 12 2022)
23
+
24
+ #### 🐛 Bug Fix
25
+
26
+ - fix(no-uninstalled-addons): ignore local addons [#98](https://github.com/storybookjs/eslint-plugin-storybook/pull/98) ([@yannbf](https://github.com/yannbf))
27
+
28
+ #### Authors: 1
29
+
30
+ - Yann Braga ([@yannbf](https://github.com/yannbf))
31
+
32
+ ---
33
+
1
34
  # v0.6.0 (Sun Jul 10 2022)
2
35
 
3
36
  #### 🚀 Enhancement
@@ -13,7 +13,7 @@ module.exports = {
13
13
  },
14
14
  },
15
15
  {
16
- files: ['main.@(js|cjs|mjs|ts)'],
16
+ files: ['.storybook/main.@(js|cjs|mjs|ts)'],
17
17
  rules: {
18
18
  'storybook/no-uninstalled-addons': 'error',
19
19
  },
@@ -14,7 +14,7 @@ module.exports = {
14
14
  },
15
15
  },
16
16
  {
17
- files: ['main.@(js|cjs|mjs|ts)'],
17
+ files: ['.storybook/main.@(js|cjs|mjs|ts)'],
18
18
  rules: {
19
19
  'storybook/no-uninstalled-addons': 'error',
20
20
  },
@@ -18,7 +18,7 @@ module.exports = {
18
18
  },
19
19
  },
20
20
  {
21
- files: ['main.@(js|cjs|mjs|ts)'],
21
+ files: ['.storybook/main.@(js|cjs|mjs|ts)'],
22
22
  rules: {
23
23
  'storybook/no-uninstalled-addons': 'error',
24
24
  },
@@ -3,7 +3,11 @@
3
3
  * @fileoverview This rule identifies storybook addons that are invalid because they are either not installed or contain a typo in their name.
4
4
  * @author Andre "andrelas1" Santos
5
5
  */
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
6
9
  const fs_1 = require("fs");
10
+ const ts_dedent_1 = __importDefault(require("ts-dedent"));
7
11
  const path_1 = require("path");
8
12
  const create_storybook_rule_1 = require("../utils/create-storybook-rule");
9
13
  const constants_1 = require("../utils/constants");
@@ -19,12 +23,24 @@ module.exports = (0, create_storybook_rule_1.createStorybookRule)({
19
23
  recommended: 'error', // or 'error'
20
24
  },
21
25
  messages: {
22
- addonIsNotInstalled: `The {{ addonName }} is not installed. Did you forget to install it?`,
26
+ addonIsNotInstalled: `The {{ addonName }} is not installed in {{packageJsonPath}}. Did you forget to install it or is your package.json in a different location?`,
23
27
  },
24
- schema: [], // Add a schema if the rule has options. Otherwise remove this
28
+ schema: [
29
+ {
30
+ type: 'object',
31
+ properties: {
32
+ packageJsonLocation: {
33
+ type: 'string',
34
+ },
35
+ },
36
+ },
37
+ ], // Add a schema if the rule has options. Otherwise remove this
25
38
  },
26
39
  create(context) {
27
40
  // variables should be defined here
41
+ const packageJsonLocation = context.options.reduce((acc, val) => {
42
+ return val['packageJsonLocation'] || acc;
43
+ }, '');
28
44
  //----------------------------------------------------------------------
29
45
  // Helpers
30
46
  //----------------------------------------------------------------------
@@ -38,12 +54,17 @@ module.exports = (0, create_storybook_rule_1.createStorybookRule)({
38
54
  return [...deps, ...devDeps];
39
55
  };
40
56
  const isAddonInstalled = (addon, installedAddons) => {
41
- // cleanup /register or /preset from registered addon
42
- const addonName = addon.replace(/\/register$/, '').replace(/\/preset$/, '');
57
+ // cleanup /register or /preset + file extension from registered addon
58
+ const addonName = addon
59
+ .replace(/\.[mc]?js$/, '')
60
+ .replace(/\/register$/, '')
61
+ .replace(/\/preset$/, '');
43
62
  return installedAddons.includes(addonName);
44
63
  };
45
64
  const areThereAddonsNotInstalled = (addons, installedSbAddons) => {
46
65
  const result = addons
66
+ // remove local addons (e.g. ./my-addon/register.js)
67
+ .filter((addon) => !addon.startsWith('.'))
47
68
  .filter((addon) => !isAddonInstalled(addon, installedSbAddons))
48
69
  .map((addon) => ({ name: addon }));
49
70
  return result.length ? result : false;
@@ -60,7 +81,10 @@ module.exports = (0, create_storybook_rule_1.createStorybookRule)({
60
81
  packageJson.devDependencies = parsedFile.devDependencies || {};
61
82
  }
62
83
  catch (e) {
63
- throw new Error('Could not fetch package.json - it is probably not in the same directory as the .storybook folder');
84
+ throw new Error((0, ts_dedent_1.default) `The provided path in your eslintrc.json - ${path} is not a valid path to a package.json file or your package.json file is not in the same folder as ESLint is running from.
85
+
86
+ Read more at: https://github.com/storybookjs/eslint-plugin-storybook/blob/main/docs/rules/no-uninstalled-addons.md
87
+ `);
64
88
  }
65
89
  return packageJson;
66
90
  };
@@ -89,30 +113,30 @@ module.exports = (0, create_storybook_rule_1.createStorybookRule)({
89
113
  return { listOfAddons: [], listOfAddonElements: [] };
90
114
  };
91
115
  function reportUninstalledAddons(addonsProp) {
92
- // when this is running for .storybook/main.js, we get the path to the folder which contains the package.json of the
93
- // project. This will be handy for monorepos that may be running ESLint in a node process in another folder.
94
- const projectRoot = context.getPhysicalFilename
95
- ? (0, path_1.resolve)(context.getPhysicalFilename(), '../../')
96
- : './';
116
+ const packageJsonPath = (0, path_1.resolve)(packageJsonLocation || `./package.json`);
97
117
  let packageJsonObject;
98
118
  try {
99
- packageJsonObject = getPackageJson(`${projectRoot}/package.json`);
119
+ packageJsonObject = getPackageJson(packageJsonPath);
100
120
  }
101
121
  catch (e) {
102
122
  // if we cannot find the package.json, we cannot check if the addons are installed
103
- console.error(e);
104
- return;
123
+ throw new Error(e);
105
124
  }
106
125
  const depsAndDevDeps = mergeDepsWithDevDeps(packageJsonObject);
107
126
  const { listOfAddons, listOfAddonElements } = extractAllAddonsFromTheStorybookConfig(addonsProp);
108
127
  const result = areThereAddonsNotInstalled(listOfAddons, depsAndDevDeps);
109
128
  if (result) {
110
129
  const elemsWithErrors = listOfAddonElements.filter((elem) => !!result.find((addon) => addon.name === elem.value));
130
+ const rootDir = process.cwd().split('/').pop();
131
+ const packageJsonPath = `${rootDir}/${(0, path_1.relative)(process.cwd(), packageJsonLocation)}`;
111
132
  elemsWithErrors.forEach((elem) => {
112
133
  context.report({
113
134
  node: elem.node,
114
135
  messageId: 'addonIsNotInstalled',
115
- data: { addonName: elem.value },
136
+ data: {
137
+ addonName: elem.value,
138
+ packageJsonPath,
139
+ },
116
140
  });
117
141
  });
118
142
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-storybook",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Best practice rules for Storybook",
5
5
  "keywords": [
6
6
  "eslint",