-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: created shared utils package and moved objectKeys function to …
…it (#35615)
- Loading branch information
1 parent
26e1c88
commit 87d22ca
Showing
36 changed files
with
357 additions
and
46 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
3 changes: 2 additions & 1 deletion
3
...ckages/design-system/widgets/src/components/Button/chromatic/Button.chromatic.stories.tsx
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
10 changes: 2 additions & 8 deletions
10
app/client/packages/design-system/widgets/src/components/Button/stories/Button.stories.tsx
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { filterDataProps } from "./filterDataProps"; | ||
export { objectKeys } from "./objectKeys"; |
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,4 @@ | ||
{ | ||
"ignorePatterns": ["dist/*", "Readme.md"], | ||
"extends": ["../../.eslintrc.base.json"] | ||
} |
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,4 @@ | ||
dist/ | ||
node_modules/ | ||
package-lock.json | ||
yarn.lock |
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,106 @@ | ||
# Adding a Custom Rule to Your Appsmith ESLint Plugin | ||
|
||
Welcome to the guide for adding a custom rule to your Appsmith ESLint plugin. Follow these steps to create and integrate a new rule into your Appsmith ESLint plugin. | ||
|
||
You can create one by following the [official ESLint custom rule](https://eslint.org/docs/latest/extend/custom-rule-tutorial). | ||
|
||
## Step 1: Create the Custom Rule File | ||
|
||
1. Navigate to your Appsmith ESLint plugin directory i.e. `app/client/packages/eslint-plugin`. | ||
2. Create a new directory for your custom rule in the root of `app/client/packages/eslint-plugin` directory. For example, `custom-rule/rule.js`. | ||
|
||
```bash | ||
mkdir custom-rule | ||
touch custom-rule/rule.js | ||
``` | ||
|
||
3. Open `custom-rule/rule.js` and define your rule. Here's a basic template to get you started: | ||
```js | ||
module.exports = { | ||
meta: { | ||
type: "problem", // or "suggestion" or "layout" | ||
docs: { | ||
description: "A description of what the rule does", | ||
category: "Best Practices", | ||
recommended: false, | ||
}, | ||
fixable: null, // or "code" if the rule can fix issues automatically | ||
schema: [], // JSON Schema for rule options | ||
}, | ||
create(context) { | ||
return { | ||
// Define the rule's behavior here | ||
// e.g., "Identifier": (node) => { /* logic */ } | ||
}; | ||
}, | ||
}; | ||
``` | ||
|
||
## Step 2: Update the Plugin Index File | ||
|
||
1. Open the `index.js` file inside `eslint-plugin` directory. | ||
|
||
2. Import your custom rule and add it to the rules object in `index.js`. For example: | ||
|
||
```js | ||
const customRule = require("./custom-rule/rule.js"); | ||
module.exports = { | ||
rules: { | ||
"custom-rule": customRule, | ||
}, | ||
}; | ||
``` | ||
|
||
## Step 3: Add Tests for Your Custom Rule | ||
|
||
1. Create a test file for your rule in the `custom-rule` directory. For example, `custom-rule/rule.test.js`. | ||
|
||
```bash | ||
touch custom-rule/rule.test.js | ||
``` | ||
|
||
2. Open `custom-rule/rule.test.js` and write tests using a testing framework like Mocha or Jest. Here's a basic example using ESLint's `RuleTester`: | ||
|
||
```js | ||
const rule = require("./rule"); | ||
const RuleTester = require("eslint").RuleTester; | ||
const ruleTester = new RuleTester(); | ||
ruleTester.run("custom-rule", rule, { | ||
valid: [ | ||
// Examples of valid code | ||
], | ||
invalid: [ | ||
{ | ||
code: "const foo = 1;", | ||
errors: [{ message: "Your custom error message" }], | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
3. Run your tests to ensure your rule works as expected: | ||
|
||
```bash | ||
yarn run test:unit | ||
``` | ||
|
||
## Step 4: Steps to add it to client | ||
|
||
1. Go to `app/client/.eslintrc.base.json` | ||
2. Add your `custom-rule` entry to the rules object. e.g. | ||
|
||
```javascript | ||
"custom-rule": "warn" | ||
``` | ||
|
||
## Additional Resources | ||
|
||
- [ESLint Plugin Developer Guide](https://eslint.org/docs/developer-guide/working-with-plugins) | ||
- [ESLint Rules API](https://eslint.org/docs/developer-guide/working-with-rules) | ||
- [ESLint Testing Guidelines](https://eslint.org/docs/developer-guide/unit-testing) | ||
|
||
Happy linting! |
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 @@ | ||
module.exports = { | ||
roots: ["<rootDir>/src"], | ||
transform: { | ||
"^.+\\.(ts)$": [ | ||
"ts-jest", | ||
{ | ||
isolatedModules: true, | ||
}, | ||
], | ||
}, | ||
}; |
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,14 @@ | ||
{ | ||
"name": "@appsmith/eslint-plugin", | ||
"version": "1.0.0", | ||
"private": true, | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "npx tsc", | ||
"build:watch": "npx tsc --watch", | ||
"lint": "yarn g:lint", | ||
"prettier": "yarn g:prettier", | ||
"postinstall": "yarn build", | ||
"test:unit": "yarn g:jest" | ||
} | ||
} |
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,16 @@ | ||
import { objectKeysRule } from "./object-keys/rule"; | ||
|
||
const plugin = { | ||
rules: { | ||
"object-keys": objectKeysRule, | ||
}, | ||
configs: { | ||
recommended: { | ||
rules: { | ||
"@appsmith/object-keys": "warn", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = plugin; |
18 changes: 18 additions & 0 deletions
18
app/client/packages/eslint-plugin/src/object-keys/rule.test.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,18 @@ | ||
import { TSESLint } from "@typescript-eslint/utils"; | ||
import { objectKeysRule } from "./rule"; | ||
|
||
const ruleTester = new TSESLint.RuleTester(); | ||
|
||
ruleTester.run("object-keys", objectKeysRule, { | ||
valid: [ | ||
{ | ||
code: "objectKeys({ 'a': 'b' })", | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "Object.keys({ 'a': 'b' })", | ||
errors: [{ messageId: "useObjectKeys" }], | ||
}, | ||
], | ||
}); |
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,36 @@ | ||
import type { TSESLint } from "@typescript-eslint/utils"; | ||
|
||
export const objectKeysRule: TSESLint.RuleModule<"useObjectKeys"> = { | ||
defaultOptions: [], | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Warns when Object.keys is used instead of objectKeys", | ||
recommended: "warn", | ||
}, | ||
schema: [], // No options | ||
messages: { | ||
useObjectKeys: | ||
"Use objectKeys from '@appsmith/utils' package instead of Object.keys", | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
// Check if the callee is Object.keys | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
node.callee.object.type === "Identifier" && | ||
node.callee.object.name === "Object" && | ||
node.callee.property.type === "Identifier" && | ||
node.callee.property.name === "keys" | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "useObjectKeys", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"target": "es2022", | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
} | ||
} |
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,3 @@ | ||
{ | ||
"extends": ["../../.eslintrc.base.json"] | ||
} |
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 @@ | ||
module.exports = { | ||
roots: ["<rootDir>/src"], | ||
transform: { | ||
"^.+\\.(ts)$": [ | ||
"ts-jest", | ||
{ | ||
isolatedModules: true, | ||
}, | ||
], | ||
}, | ||
}; |
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,14 @@ | ||
{ | ||
"name": "@appsmith/utils", | ||
"version": "1.0.0", | ||
"description": "This package has all the shared util functions which can be used in different packages e.g. client, rts etc", | ||
"main": "src/index.ts", | ||
"types": "src/index.d.ts", | ||
"scripts": { | ||
"lint": "yarn g:lint", | ||
"prettier": "yarn g:prettier", | ||
"test:unit": "yarn g:jest" | ||
}, | ||
"author": "Aman Agarwal <[email protected]>, Pawan Kumar <[email protected]>", | ||
"license": "ISC" | ||
} |
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 @@ | ||
export * from "./object"; |
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 @@ | ||
export { objectKeys } from "./keys"; |
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,7 @@ | ||
import { objectKeys } from "./keys"; | ||
|
||
test("objectKeys should return all the keys in the object map pass to it.", () => { | ||
const objectMap = { a: 1, b: 2, c: 3 }; | ||
const keys = objectKeys(objectMap); | ||
expect(keys).toStrictEqual(["a", "b", "c"]); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src/**/*"] | ||
} |
Oops, something went wrong.