├── README.md ├── index.js ├── lib ├── common │ └── parseFilename.js └── rules │ └── match-regex.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # eslint-plugin-folders 2 | 3 | Adds [eslint](http://eslint.org/) rules to ensure consistent folder names for your javascript files. 4 | 5 | __Please note__: This plugin will only lint the folder Names of the `.js`, `.jsx` files you are linting with eslint. It will ignore other files that are not linted with eslint. 6 | 7 | ## Enabling the plugin 8 | 9 | This plugin requires a version of `eslint>=1.0.0` to be installed as a peer dependency. 10 | 11 | Modify your `.eslintrc` file to load the plugin and enable the rules you want to use. 12 | 13 | ```json 14 | { 15 | "plugins": [ 16 | "folders" 17 | ], 18 | "rules": { 19 | "folders/match-regex": [2, null, "/root/"] 20 | } 21 | } 22 | ``` 23 | 24 | ## Rules 25 | 26 | ### Consistent Folder Names via regex (match-regex) 27 | 28 | A rule to enforce a certain file naming convention using a regular expression. 29 | 30 | The convention can be configured using a regular expression (the default is `camelCase`). Additionally 31 | the root of the project is defined with a second configuration parameter. 32 | 33 | ```json 34 | "folders/match-regex": [2, "^[a-z_]+$", '/work/'] 35 | ``` 36 | 37 | With these configuration options, any folder beneath the **/work/** directory `camelCase` will be reported as an error while `snake_case` will pass. 38 | 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = { 4 | rules: { 5 | "match-regex": require("./lib/rules/match-regex"), 6 | } 7 | } -------------------------------------------------------------------------------- /lib/common/parseFilename.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = function parseFilename(filename) { 4 | var ext = path.extname(filename); 5 | 6 | return { 7 | dir: path.dirname(filename), 8 | base: path.basename(filename), 9 | ext: ext, 10 | name: path.basename(filename, ext) 11 | } 12 | }; -------------------------------------------------------------------------------- /lib/rules/match-regex.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const path = require("path") 4 | const parseFilename = require('../common/parseFilename') 5 | 6 | module.exports = function(context) { 7 | const defaultRegexp = /^([a-z0-9]+)([A-Z][a-z0-9]+)*$/g 8 | const conventionRegexp = context.options[0] ? context.options[0] : defaultRegexp 9 | const root = context.options[1] 10 | return { 11 | "Program": function(node) { 12 | const filename = context.getFilename() 13 | const absoluteFilename = path.resolve(filename) 14 | const parsed = parseFilename(absoluteFilename) 15 | const [,relativePath] = parsed.dir.split(root) 16 | if(relativePath){ 17 | relativePath.split(path.sep).forEach((directory) => { 18 | if(directory){ 19 | const regex = new RegExp(conventionRegexp) 20 | const matchesRegex = regex.test(directory) 21 | if (!matchesRegex) { 22 | context.report(node, "FolderName '{{name}}' in path '{{path}}' does not match the naming convention.", { 23 | name: directory, 24 | path: relativePath 25 | }); 26 | } 27 | } 28 | 29 | }) 30 | } 31 | 32 | } 33 | }; 34 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-folders", 3 | "version": "1.0.4", 4 | "description": "Eslint rule for consistent folder names.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "peerDependencies": { 10 | "eslint": "*" 11 | }, 12 | "author": "Chris Banks ", 13 | "license": "MIT", 14 | "keywords": [ 15 | "eslint", 16 | "eslintplugin", 17 | "eslint-plugin", 18 | "folder", 19 | "folders", 20 | "path" 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "git://github.com/christopherbradleybanks/eslint-plugin-folders.git" 25 | } 26 | } 27 | --------------------------------------------------------------------------------