-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from epaew/feature/pluralize
Add rule `pluralize`
- Loading branch information
Showing
12 changed files
with
371 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# filenames-simple/pluralize | ||
This rule ensures that filenames are plural (or singular). | ||
|
||
## Configuration example | ||
```json | ||
{ | ||
"plugins": [ | ||
"filenames-simple" | ||
], | ||
"rules": { | ||
"filenames-simple/pluralize": [ | ||
"error", | ||
{ | ||
"parentDir": "plural", | ||
"file": "singular" | ||
}, | ||
{ | ||
"uncountable": ["water"] | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
## Available options | ||
### rule: (the first option) | ||
Specify one of the following naming conventions for each `parentDir` or `file`. | ||
|
||
#### Naming convention presets | ||
* singular | ||
* plural | ||
|
||
### dictionaries: (the second option) | ||
Specify the dictionary to pass to the `pluralize` library. | ||
See also: https://github.com/blakeembrey/pluralize#usage | ||
|
||
#### Keys and examples | ||
* irregular: array of arguments for `pluralize.addIrregularRule()` | ||
* e.g. `[["singular", "plural"], ["person", "people"]]` | ||
* plural: array of arguments for `pluralize.addPluralRule()` | ||
* e.g. `[["plural", "singular"]]` | ||
* singular: array of arguments for `pluralize.addSingularRule()` | ||
* e.g. `[["singular", "plural"]]` | ||
* uncountable: array of arguments for `pluralize.addUncountableRule()` | ||
* e.g. `["uncountable", "water"]` |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import path from 'path'; | ||
import { Rule } from 'eslint'; | ||
import { correct, Dictionaries, isValidName, setDictionaries } from '../utils/pluralize'; | ||
|
||
type Rules = { | ||
parentDir?: 'singular' | 'plural'; | ||
file?: 'singular' | 'plural'; | ||
}; | ||
type Options = { | ||
dictionaries?: Dictionaries; | ||
rules: Rules; | ||
}; | ||
|
||
const irregular = { | ||
type: 'array', | ||
items: { | ||
type: 'array', | ||
items: [{ type: 'string' }, { type: 'string' }], | ||
maxItems: 2, | ||
minItems: 2, | ||
}, | ||
minItems: 1, | ||
uniqueItems: true, | ||
}; | ||
const plural = irregular; | ||
const singular = irregular; | ||
|
||
const fetchFilename = (context: Rule.RuleContext): [string, string[]] => { | ||
const absolutePath = path.resolve(context.getFilename()); | ||
const [dirname, basename] = absolutePath.split(path.sep).slice(-2); | ||
const filename = basename.split('.'); | ||
|
||
return [dirname, filename]; | ||
}; | ||
|
||
const parseOptions = (context: Rule.RuleContext): Options => { | ||
const [rules = {}, dictionaries = {}] = context.options; | ||
return { rules, dictionaries }; | ||
}; | ||
|
||
export const pluralize: Rule.RuleModule = { | ||
meta: { | ||
type: 'suggestion', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
parentDir: { enum: ['singular', 'plural'] }, | ||
file: { enum: ['singular', 'plural'] }, | ||
}, | ||
minProperties: 1, | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
irregular, | ||
plural, | ||
singular, | ||
uncountable: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
minItems: 1, | ||
uniqueItems: true, | ||
}, | ||
}, | ||
minProperties: 1, | ||
}, | ||
], | ||
}, | ||
create: context => { | ||
const { rules, dictionaries } = parseOptions(context); | ||
dictionaries && setDictionaries(dictionaries); | ||
|
||
return { | ||
Program: node => { | ||
const [dirname, [filename, ...rest]] = fetchFilename(context); | ||
|
||
if (isValidName(dirname, rules.parentDir) && isValidName(filename, rules.file)) return; | ||
|
||
context.report({ | ||
node: node, | ||
message: | ||
'The filename must follow the pluralize rule. Should rename to {{ dirname }}/{{ filename }}.{{ extname }}', | ||
data: { | ||
dirname: correct(dirname, rules.parentDir), | ||
filename: correct(filename, rules.file), | ||
extname: rest.join('.'), | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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,44 @@ | ||
import pluralize from 'pluralize'; | ||
|
||
type Rule = 'singular' | 'plural'; | ||
export type Dictionaries = { | ||
irregular?: [string, string][]; | ||
plural?: [string, string][]; | ||
singular?: [string, string][]; | ||
uncountable?: string[]; | ||
}; | ||
|
||
export const correct = (name: string, rule?: Rule) => { | ||
const corrector = { | ||
singular: pluralize.singular, | ||
plural: pluralize.plural, | ||
}; | ||
return rule ? corrector[rule](name) : name; | ||
}; | ||
|
||
export const isValidName = (name: string, rule?: Rule) => { | ||
const validator = { | ||
singular: pluralize.isSingular, | ||
plural: pluralize.isPlural, | ||
}; | ||
return rule ? validator[rule](name) : true; | ||
}; | ||
|
||
export const setDictionaries = (dictionaries: Dictionaries) => { | ||
const keys: Array<keyof Dictionaries> = ['irregular', 'plural', 'singular', 'uncountable']; | ||
const dictionarySetter = { | ||
irregular: ([singular, plural]: [string, string]) => | ||
pluralize.addIrregularRule(singular, plural), | ||
plural: ([plural, singular]: [string, string]) => | ||
pluralize.addPluralRule(new RegExp(plural), singular), | ||
singular: ([singular, plural]: [string, string]) => | ||
pluralize.addSingularRule(new RegExp(singular), plural), | ||
uncountable: (uncountable: string) => pluralize.addUncountableRule(uncountable), | ||
}; | ||
|
||
keys.forEach(key => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
dictionaries[key] && dictionaries[key].forEach(dictionarySetter[key]); | ||
}); | ||
}; |
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,77 @@ | ||
import { RuleTester } from 'eslint'; | ||
import { pluralize } from '#/rules/pluralize'; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('pluralize', pluralize, { | ||
valid: [ | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ parentDir: 'singular' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ file: 'singular' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ parentDir: 'singular', file: 'singular' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controllers/index.js', | ||
options: [{ parentDir: 'plural', file: 'singular' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/indices.js', | ||
options: [{ parentDir: 'singular', file: 'plural' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controllers/indices.js', | ||
options: [{ parentDir: 'plural', file: 'plural' }], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/lib/index.js', | ||
options: [ | ||
{ parentDir: 'plural', file: 'singular' }, | ||
{ irregular: [['person', 'people']], uncountable: ['lib'] }, | ||
], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ parentDir: 'plural', file: 'singular' }], | ||
errors: [ | ||
'The filename must follow the pluralize rule. Should rename to controllers/index.js', | ||
], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ parentDir: 'plural' }], | ||
errors: [ | ||
'The filename must follow the pluralize rule. Should rename to controllers/index.js', | ||
], | ||
}, | ||
{ | ||
code: '', | ||
filename: 'src/controller/index.js', | ||
options: [{ file: 'plural' }], | ||
errors: [ | ||
'The filename must follow the pluralize rule. Should rename to controller/indices.js', | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.