-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add postfix convention for store naming rule (#37)
* add postfix mode * change docs * add tests to cover postfix mode * validate mode and move correctedStoreName to report function * fix validation * use shared settings to be able to share name convention across different rules * modify tests according to the new implementation * modify docs and recommended config * modify error message * extract utilities * fix no get state rule * refactor enforce naming rule
- Loading branch information
1 parent
21ab289
commit 01e7175
Showing
42 changed files
with
426 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,5 @@ module.exports = { | |
}, | ||
configs: { | ||
recommended: require("./config/recommended"), | ||
}, | ||
} | ||
}; |
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
35 changes: 34 additions & 1 deletion
35
rules/enforce-store-naming-convention/enforce-store-naming-convention.md
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,11 +1,44 @@ | ||
# effector/enforce-store-naming-convention | ||
|
||
Enforcing naming conventions helps keep the codebase consistent, and reduces overhead when thinking about how to name a variable with store. Your stores should be distingueshed by a prefix $. For example, `$name` is a store,`name` is not. | ||
Enforcing naming conventions helps keep the codebase consistent, and reduces overhead when thinking about how to name a variable with store. Depending on the configuration your stores should be distinguished by a prefix or a postfix $. Enforces prefix convention by default. | ||
|
||
## Prefix convention | ||
When configured as: | ||
```js | ||
module.exports = { | ||
rules: { | ||
"effector/enforce-store-naming-convention": "error", | ||
}, | ||
}; | ||
``` | ||
Prefix convention will be enforced: | ||
```ts | ||
// 👍 nice name | ||
const $name = createStore(null); | ||
|
||
// 👎 bad name | ||
const name = createStrore(null); | ||
``` | ||
## Postfix convention | ||
|
||
When configured as: | ||
```js | ||
module.exports = { | ||
rules: { | ||
"effector/enforce-store-naming-convention": "error", | ||
}, | ||
settings: { | ||
effector: { | ||
storeNameConvention: "postfix" | ||
} | ||
} | ||
}; | ||
``` | ||
Postfix convention will be enforced: | ||
```ts | ||
// 👍 nice name | ||
const name$ = createStore(null); | ||
|
||
// 👎 bad name | ||
const name = createStrore(null); | ||
``` |
84 changes: 84 additions & 0 deletions
84
...s/enforce-store-naming-convention/postfix/enforce-store-naming-convention-postfix.test.js
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,84 @@ | ||
const { RuleTester } = require("eslint"); | ||
|
||
const { readExample } = require("../../../utils/read-example"); | ||
|
||
const rule = require("../enforce-store-naming-convention"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}); | ||
|
||
const readExampleForTheRule = (name) => readExample(__dirname, name); | ||
|
||
ruleTester.run("effector/enforce-store-naming-convention-postfix.test", rule, { | ||
valid: [ | ||
"correct-store-naming.js", | ||
"correct-store-naming-from-other-package.js", | ||
"correct-store-naming-in-domain.js", | ||
"correct-examples-issue-23.js", | ||
] | ||
.map(readExampleForTheRule) | ||
.map((code) => ({ | ||
code, | ||
settings: { | ||
effector: { | ||
storeNameConvention: "postfix" | ||
} | ||
}, | ||
})), | ||
|
||
invalid: [ | ||
// Errors | ||
...[ | ||
"incorrect-createStore.js", | ||
"incorrect-createStore-alias.js", | ||
"incorrect-createStore-prefix.js", | ||
"incorrect-restore.js", | ||
"incorrect-restore-alias.js", | ||
"incorrect-combine.js", | ||
"incorrect-combine-alias.js", | ||
"incorrect-map.js", | ||
"incorrect-createStore-domain.js", | ||
] | ||
.map(readExampleForTheRule) | ||
.map((code) => ({ | ||
code, | ||
settings: { | ||
effector: { | ||
storeNameConvention: "postfix" | ||
} | ||
}, | ||
errors: [ | ||
{ | ||
messageId: "invalidName", | ||
type: "VariableDeclarator", | ||
}, | ||
], | ||
})), | ||
// Suggestions | ||
{ | ||
code: ` | ||
import {createStore} from 'effector'; | ||
const store = createStore(null); | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "invalidName", | ||
suggestions: [ | ||
{ | ||
messageId: "renameStore", | ||
data: { storeName: "store" }, | ||
output: ` | ||
import {createStore} from 'effector'; | ||
const $store = createStore(null); | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
46 changes: 46 additions & 0 deletions
46
...nforce-store-naming-convention/postfix/enforce-store-naming-convention-postfix.ts.test.js
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,46 @@ | ||
const { RuleTester } = | ||
require("@typescript-eslint/experimental-utils").ESLintUtils; | ||
const { join } = require("path"); | ||
|
||
const { readExample } = require("../../../utils/read-example"); | ||
|
||
const rule = require("../enforce-store-naming-convention"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
project: "./tsconfig.json", | ||
tsconfigRootDir: join(__dirname, "../.."), | ||
}, | ||
}); | ||
|
||
const readExampleForTheRule = (name) => ({ | ||
code: readExample(__dirname, name), | ||
filename: join(__dirname, "examples", name), | ||
settings: { | ||
effector: { | ||
storeNameConvention: "postfix" | ||
} | ||
}, | ||
}); | ||
|
||
ruleTester.run("effector/enforce-store-naming-convention-postfix.ts.test", rule, { | ||
valid: ["correct-store-naming.ts"].map(readExampleForTheRule), | ||
|
||
invalid: [ | ||
// Errors | ||
...["incorrect-store-naming.ts"] | ||
.map(readExampleForTheRule) | ||
.map((result) => ({ | ||
...result, | ||
errors: [ | ||
{ | ||
messageId: "invalidName", | ||
type: "VariableDeclarator", | ||
}, | ||
], | ||
})), | ||
], | ||
}); |
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
rules/enforce-store-naming-convention/postfix/examples/correct-store-naming-in-domain.js
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,8 @@ | ||
import { createDomain } from "effector"; | ||
|
||
const domain = createDomain(); | ||
|
||
const storeOne$ = domain.createStore(null); | ||
const storeTwo$ = domain.store(null); | ||
|
||
export { storeOne$, storeTwo$ }; |
16 changes: 16 additions & 0 deletions
16
rules/enforce-store-naming-convention/postfix/examples/correct-store-naming.js
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 { createStore, restore, createEvent, combine } from "effector"; | ||
|
||
// Just createStore | ||
const justStore$ = createStore(null); | ||
|
||
// Restore | ||
const eventForRestore = createEvent(); | ||
const restoredStore$ = restore(eventForRestore, null); | ||
|
||
// Combine | ||
const combinedStore$ = combine($justStore, $restoredStore); | ||
|
||
// Map | ||
const mappedStore$ = $combinedStore.map((values) => values.length); | ||
|
||
export { justStore$, restoredStore$, combinedStore$, mappedStore$ }; |
Oops, something went wrong.