@justeattakeaway/eslint-plugin-snacks-pie-migration 0.1.1 → 0.2.0

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/README.md CHANGED
@@ -154,7 +154,9 @@ This is particularly useful during migration to PIE components, as it allows you
154
154
  - Focus on preventing new deprecated component usage
155
155
  - Gradually refactor existing code at your own pace
156
156
 
157
- ℹ️ Changes will be compared against the `main` branch. In case `main` doesn't exist, the `master` branch will be used.
157
+ > ℹ️ When running locally, the branch changes will be compared against the merge base ref. It assumes the `main` branch exists. Otherwise the `master` branch will be used. In any case, no extra configuration is needed.
158
+
159
+ > ℹ️ When running on Github CI, the plugin will use the PR base branch SHA since the merge base can't be easily inferred. In this case, ensure to pass the `BASE_SHA` environment variable. Its value should be set to `github.event.pull_request.base.sha`.
158
160
 
159
161
  To use the processor, add the following line to your ESLint configuration:
160
162
 
@@ -1,5 +1,5 @@
1
1
  const { readFileSync } = require('fs');
2
- const { isFileNew, getFileStateFromBranch } = require('../util/git-utils');
2
+ const { isFileNew, getFileStateFromRef } = require('../util/git-utils');
3
3
  const { addedComponentsPostprocessor } = require('../util/added-components-postprocessor');
4
4
 
5
5
  /**
@@ -9,6 +9,6 @@ const { addedComponentsPostprocessor } = require('../util/added-components-postp
9
9
  */
10
10
  module.exports = {
11
11
  postprocess (messages, filePath) {
12
- return addedComponentsPostprocessor(messages, filePath, { readFileSync, isFileNew, getFileStateFromBranch });
12
+ return addedComponentsPostprocessor(messages, filePath, { readFileSync, isFileNew, getFileStateFromRef });
13
13
  },
14
14
  };
@@ -38,8 +38,9 @@ module.exports = {
38
38
  const replacementData = snacksComponentsData[componentName];
39
39
  // Specific components can be bypassed if an array of names are provided
40
40
  const isBypassedInOptions = bypassedComponents && bypassedComponents.includes(componentName);
41
+ const isDeprecated = replacementData && replacementData.status && replacementData.status === 'stable';
41
42
 
42
- if (replacementData && !isBypassedInOptions) {
43
+ if (!isBypassedInOptions && isDeprecated) {
43
44
  context.report({
44
45
  node,
45
46
  message: `The Snacks component "${componentName}" is being deprecated and can be replaced by "${replacementData.piePackage}".`,
@@ -10,7 +10,7 @@ const { getDefaultBranchName } = require('./git-utils');
10
10
  * @param filePath - the file path
11
11
  * @returns A filtered array of messages based on certain conditions
12
12
  */
13
- function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew, getFileStateFromBranch }) {
13
+ function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew, getFileStateFromRef }) {
14
14
  // Get file relative path to the repo
15
15
  const relativeFilePath = path.relative(process.cwd(), filePath)
16
16
  .replace(/\\/g, '/'); // Fix path handling on Windows
@@ -20,8 +20,10 @@ function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFil
20
20
  const isNewFile = isFileNew(relativeFilePath);
21
21
 
22
22
  // Get file content before and after
23
- const mainBranchName = getDefaultBranchName();
24
- const filePreviousState = isNewFile ? '' : getFileStateFromBranch(relativeFilePath, mainBranchName);
23
+ // In CI is preferred to use the base SHA as it is easily available
24
+ // In local environment it's easier to pull the info with git merge-base from the main branch
25
+ const ref = 'BASE_SHA' in process.env && process.env.BASE_SHA;
26
+ const filePreviousState = isNewFile ? '' : getFileStateFromRef(relativeFilePath, ref, getDefaultBranchName());
25
27
  const fileCurrentState = readFileSync(relativeFilePath, 'utf8');
26
28
 
27
29
  // Get list of added components
@@ -16,17 +16,18 @@ function isFileNew (relativeFilePath) {
16
16
  }
17
17
 
18
18
  /**
19
- * Get file content from a specific branch
19
+ * Get file content from a ref
20
20
  * @param {string} relativeFilePath - The relative path to the file from the repo root
21
- * @param {string} branch - The branch name to check the file content from
21
+ * @param {string} ref - SHA ref to check the file content from, give preference to this option when running on CI
22
+ * @param {string} branch - Alternatively, the branch name to check the file content from, give preference to this option when running on local environment
22
23
  * @returns
23
24
  */
24
- function getFileStateFromBranch (relativeFilePath, branch = 'main') {
25
+ function getFileStateFromRef (relativeFilePath, ref, branch = 'main') {
25
26
  try {
26
- // Get the merge base to find the actual parent branch
27
- const mergeBase = execSync(`git merge-base HEAD ${branch}`, { encoding: 'utf8' }).trim();
28
- return execSync(`git show ${mergeBase}:"${relativeFilePath}"`, { encoding: 'utf8', stdio: 'pipe' });
29
- } catch (error) {
27
+ // If the base sha wasn't provided, try to guess with merge-base
28
+ const sha = ref || execSync(`git merge-base HEAD ${branch}`, { encoding: 'utf8' }).trim();
29
+ return execSync(`git show ${sha}:"${relativeFilePath}"`, { encoding: 'utf8', stdio: 'pipe' });
30
+ } catch {
30
31
  // If the file was added in this branch, this might fail, return empty string
31
32
  return '';
32
33
  }
@@ -47,6 +48,6 @@ function getDefaultBranchName () {
47
48
 
48
49
  module.exports = {
49
50
  isFileNew,
50
- getFileStateFromBranch,
51
+ getFileStateFromRef,
51
52
  getDefaultBranchName,
52
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justeattakeaway/eslint-plugin-snacks-pie-migration",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "This plugin helps developers to identify deprecated Snacks components and provides suggestions of replacement PIE components",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,11 +21,11 @@
21
21
  },
22
22
  "Checkbox": {
23
23
  "piePackage": "@justeattakeaway/pie-checkbox",
24
- "status": "beta"
24
+ "status": "stable"
25
25
  },
26
26
  "CheckboxGroup": {
27
27
  "piePackage": "@justeattakeaway/pie-checkbox-group",
28
- "status": "beta"
28
+ "status": "stable"
29
29
  },
30
30
  "Chip": {
31
31
  "piePackage": "@justeattakeaway/pie-chip",
@@ -61,11 +61,11 @@
61
61
  },
62
62
  "Radio": {
63
63
  "piePackage": "@justeattakeaway/pie-radio",
64
- "status": "beta"
64
+ "status": "stable"
65
65
  },
66
66
  "RadioGroup": {
67
67
  "piePackage": "@justeattakeaway/pie-radio-group",
68
- "status": "beta"
68
+ "status": "stable"
69
69
  },
70
70
  "Select": {
71
71
  "piePackage": "@justeattakeaway/pie-select",