├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── index.js └── package.json /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v1.2.1 (2020-05-27) 4 | 5 | * Support overrides having no rules :see_no_evil: ([#5](https://github.com/not-an-aardvark/eslint-plugin-self/issues/5)) ([e266543](https://github.com/not-an-aardvark/eslint-plugin-self/commit/e266543e50d062251755dd488abae31be7644bb1)) 6 | 7 | ## v1.2.0 (2019-03-04) 8 | 9 | * Support redefining plugins, overrides and rules with a "/" in them ([#2](https://github.com/not-an-aardvark/eslint-plugin-self/issues/2)) ([428664e](https://github.com/not-an-aardvark/eslint-plugin-self/commit/428664e1cf8f3726e0bb3b10bb3e137d271749c2)) 10 | 11 | ## v1.1.0 (2018-07-06) 12 | 13 | * Chore: add release script ([983a7d0](https://github.com/not-an-aardvark/eslint-plugin-self/commit/983a7d05c48bccc125f8d89fae1109a0c5a1d670)) 14 | * Update: Add support for @scoped packages ([#1](https://github.com/not-an-aardvark/eslint-plugin-self/issues/1)) ([c57a01b](https://github.com/not-an-aardvark/eslint-plugin-self/commit/c57a01bbf922b82d09a0cceba6b5e845fab7d23a)) 15 | 16 | ## v1.0.1 (2017-07-02) 17 | 18 | * Fix: transform references to own rules in configs to use `self` prefix ([2dce85e](https://github.com/not-an-aardvark/eslint-plugin-self/commit/2dce85e445a7604f5fd963d1366509fa7a66d420)) 19 | 20 | 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © 2017 Teddy Katz 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the “Software”), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-plugin-self 2 | 3 | When writing an ESLint plugin, it's often useful to use the plugin's rules to lint the plugin's own codebase. You can use `eslint-plugin-self` to do that. 4 | 5 | ## Usage 6 | 7 | ``` 8 | npm install eslint-plugin-self --save-dev 9 | ``` 10 | 11 | Note: `eslint-plugin-self` must be installed locally (it will not work if installed globally), and the project that installs it must be a functioning ESLint plugin. 12 | 13 | Add the following to your config file: 14 | 15 | ```json 16 | { 17 | "plugins": [ 18 | "self" 19 | ] 20 | } 21 | ``` 22 | 23 | Then you can use your plugin's rules, with the `self/` prefix: 24 | 25 | ```json 26 | { 27 | "rules": { 28 | "self/my-custom-rule": "error" 29 | } 30 | } 31 | ``` 32 | 33 | You can also use your plugin's configs, or anything else exported by your plugin: 34 | 35 | ```json 36 | { 37 | "extends": [ 38 | "plugin:self/some-config" 39 | ] 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const plugin = require('../..'); 4 | const selfPlugin = Object.assign({}, plugin); 5 | 6 | const pkgName = require('../../package.json').name; 7 | let pluginName; 8 | if (pkgName[0] === "@") { 9 | const matches = pkgName.match(/^(@[^/]+)\/eslint-plugin(?:-(.*))?$/); 10 | pluginName = matches.slice(1, 3).filter(Boolean).join('/'); 11 | } else { 12 | pluginName = pkgName.replace(/^eslint-plugin-/, ''); 13 | } 14 | 15 | function createRuleset(rules) { 16 | return Object.keys(rules).reduce((newRules, oldRuleName) => { 17 | const newRuleName = oldRuleName.startsWith(`${pluginName}/`) 18 | ? `self${oldRuleName.slice(oldRuleName.indexOf('/'))}` 19 | : oldRuleName; 20 | 21 | newRules[newRuleName] = rules[oldRuleName]; 22 | return newRules; 23 | }, {}); 24 | } 25 | 26 | if (plugin.configs) { 27 | selfPlugin.configs = Object.assign({}, plugin.configs); 28 | 29 | Object.keys(plugin.configs).forEach(configName => { 30 | const config = plugin.configs[configName]; 31 | selfPlugin.configs[configName] = Object.assign({}, config); 32 | if (config.extends) { 33 | selfPlugin.configs[configName].extends = [].concat(config.extends) 34 | .map(extendsName => extendsName.replace(`plugin:${pluginName}/`, 'plugin:self/')); 35 | } 36 | if (config.plugins) { 37 | selfPlugin.configs[configName].plugins = [].concat(config.plugins) 38 | .map(enabledPluginName => enabledPluginName.replace(pluginName, 'self')); 39 | } 40 | if (config.rules) { 41 | selfPlugin.configs[configName].rules = createRuleset(config.rules); 42 | } 43 | if (config.overrides) { 44 | selfPlugin.configs[configName].overrides = [].concat(config.overrides) 45 | .map((override) => { 46 | if (!override.rules) return override; 47 | return Object.assign( 48 | {}, 49 | override, 50 | {rules: createRuleset(override.rules)} 51 | ); 52 | }) 53 | } 54 | }); 55 | } 56 | 57 | module.exports = selfPlugin; 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-self", 3 | "version": "1.2.1", 4 | "description": "Allows ESLint plugins to be run on themselves", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "generate-release": "node-release-script" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/not-an-aardvark/eslint-plugin-self.git" 13 | }, 14 | "keywords": [ 15 | "eslint-plugin", 16 | "eslintplugin" 17 | ], 18 | "author": "Teddy Katz", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/not-an-aardvark/eslint-plugin-self/issues" 22 | }, 23 | "homepage": "https://github.com/not-an-aardvark/eslint-plugin-self#readme", 24 | "devDependencies": { 25 | "@not-an-aardvark/node-release-script": "^0.1.0" 26 | } 27 | } 28 | --------------------------------------------------------------------------------