@justeattakeaway/eslint-plugin-snacks-pie-migration 0.1.0 → 0.1.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/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
  };
@@ -1,3 +1,4 @@
1
+ const path = require('path');
1
2
  const { getAddedComponents } = require('./get-added-components');
2
3
  const { getDefaultBranchName } = require('./git-utils');
3
4
 
@@ -9,17 +10,20 @@ const { getDefaultBranchName } = require('./git-utils');
9
10
  * @param filePath - the file path
10
11
  * @returns A filtered array of messages based on certain conditions
11
12
  */
12
- function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew, getFileStateFromBranch }) {
13
+ function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew, getFileStateFromRef }) {
13
14
  // Get file relative path to the repo
14
- const relativeFilePath = filePath.replace(process.cwd(), '').substr(1);
15
+ const relativeFilePath = path.relative(process.cwd(), filePath)
16
+ .replace(/\\/g, '/'); // Fix path handling on Windows
15
17
 
16
18
  // Check if file was added, but not committed yet
17
19
  // This is used in the pre-commit hook
18
20
  const isNewFile = isFileNew(relativeFilePath);
19
21
 
20
22
  // Get file content before and after
21
- const mainBranchName = getDefaultBranchName();
22
- 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());
23
27
  const fileCurrentState = readFileSync(relativeFilePath, 'utf8');
24
28
 
25
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.0",
3
+ "version": "0.1.2",
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",