├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 0.0.3 (05/13/22) 4 | 5 | - Fix showstopper bug, thanks @justinbarclay! 6 | 7 | ## 0.0.2 (10/01/21) 8 | 9 | - Remove lodash dependency 10 | - Remove debug console.log 11 | 12 | ## 0.0.1 (09/30/21) 13 | 14 | - Initial release 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Jeff Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-plugin-absolute-imports 2 | 3 | A (zero-dependency!) eslint plugin that enforces absolute imports on your codebase. 4 | 5 | ## Prerequisites 6 | 7 | You must have a `baseUrl` defined in either `tsconfig.json` or `jsconfig.json`. **This plugin does not currently work with `paths`!** 8 | 9 | ## Setup 10 | 11 | - `npm i --save-dev eslint-plugin-absolute-imports` 12 | - Add `eslint-plugin-absolute-imports` to your eslint `plugins` section 13 | - Add `absolute-imports/only-absolute-imports` to your eslint `rules` section 14 | 15 | ## License 16 | 17 | MIT 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const fs = require("fs"); 4 | const path = require("path"); 5 | 6 | function has(map, path) { 7 | let inner = map; 8 | for (let step of path.split(".")) { 9 | inner = inner[step]; 10 | if (inner === undefined) { 11 | return false; 12 | } 13 | } 14 | return true; 15 | } 16 | 17 | function findDirWithFile(filename) { 18 | let dir = path.resolve(filename); 19 | 20 | do { 21 | dir = path.dirname(dir); 22 | } while (!fs.existsSync(path.join(dir, filename)) && dir !== "/"); 23 | 24 | if (!fs.existsSync(path.join(dir, filename))) { 25 | return; 26 | } 27 | 28 | return dir; 29 | } 30 | 31 | function getBaseUrl(baseDir) { 32 | let url = ""; 33 | 34 | if (fs.existsSync(path.join(baseDir, "tsconfig.json"))) { 35 | const tsconfig = JSON.parse( 36 | fs.readFileSync(path.join(baseDir, "tsconfig.json")) 37 | ); 38 | if (has(tsconfig, "compilerOptions.baseUrl")) { 39 | url = tsconfig.compilerOptions.baseUrl; 40 | } 41 | } else if (fs.existsSync(path.join(baseDir, "jsconfig.json"))) { 42 | const jsconfig = JSON.parse( 43 | fs.readFileSync(path.join(baseDir, "jsconfig.json")) 44 | ); 45 | if (has(jsconfig, "compilerOptions.baseUrl")) { 46 | url = jsconfig.compilerOptions.baseUrl; 47 | } 48 | } 49 | 50 | return path.join(baseDir, url); 51 | } 52 | 53 | module.exports.rules = { 54 | "only-absolute-imports": { 55 | meta: { 56 | fixable: true, 57 | }, 58 | create: function (context) { 59 | const baseDir = findDirWithFile("package.json"); 60 | const baseUrl = getBaseUrl(baseDir); 61 | 62 | return { 63 | ImportDeclaration(node) { 64 | const source = node.source.value; 65 | if (source.startsWith(".")) { 66 | const filename = context.getFilename(); 67 | 68 | const absolutePath = path.normalize( 69 | path.join(path.dirname(filename), source) 70 | ); 71 | const expectedPath = path.relative(baseUrl, absolutePath); 72 | 73 | if (source !== expectedPath) { 74 | context.report({ 75 | node, 76 | message: `Relative imports are not allowed. Use \`${expectedPath}\` instead of \`${source}\`.`, 77 | fix: function (fixer) { 78 | return fixer.replaceText(node.source, `'${expectedPath}'`); 79 | }, 80 | }); 81 | } 82 | } 83 | }, 84 | }; 85 | }, 86 | }, 87 | }; 88 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-absolute-imports", 3 | "version": "0.0.1", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-absolute-imports", 3 | "version": "0.0.3", 4 | "main": "index.js", 5 | "dependencies": {}, 6 | "description": "Eslint rule enforcing absolute imports", 7 | "license": "MIT", 8 | "author": { 9 | "email": "hello@jeff.yt", 10 | "name": "Jeff Chen", 11 | "url": "https://www.jeffchen.dev" 12 | }, 13 | "categories": [ 14 | "Linters" 15 | ], 16 | "repository": { 17 | "url": "https://www.github.com/jchen1/eslint-plugin-absolute-imports" 18 | }, 19 | "keywords": [ 20 | "eslint", 21 | "imports", 22 | "eslint-plugin" 23 | ] 24 | } 25 | --------------------------------------------------------------------------------