forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new rule no-restricted-paths (fixes import-js#155)
- Loading branch information
Showing
10 changed files
with
212 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,39 @@ | ||
# no-restricted-paths - Restrict which files can be imported in a given folder | ||
|
||
Some projects contain files which are not always meant to be executed in the same environment. | ||
For example consider a web application that contains specific code for the server and some specific code for the browser/client. In this case you don’t want to import server-only files in your client code. | ||
|
||
In order to prevent such scenarios this rule allows you to define restricted zones where you can forbid files from imported if they match a specific path. | ||
|
||
## Rule Details | ||
|
||
This rule has one option. The option is an object containing the definition of all restricted `zones` and the optional `basePath` which is used to resolve relative paths within. | ||
The default value for `basePath` is the current working directory. | ||
Each zone consists of the `target` path and a `from` path. The `target` is the path where the restricted imports should be applied. The `from` path defines the folder that is not allowed to be used in an import. | ||
|
||
### Examples | ||
|
||
Given the following folder structure: | ||
|
||
``` | ||
my-project | ||
├── client | ||
│ └── foo.js | ||
│ └── baz.js | ||
└── server | ||
└── bar.js | ||
``` | ||
|
||
and the current file being linted is `my-project/client/foo.js`. | ||
|
||
The following patterns are considered problems when configuration set to `{ "zones": [ { "target": "./client", "from": "./server" } ] }`: | ||
|
||
```js | ||
import bar from '../server/bar'; | ||
``` | ||
|
||
The following patterns are not considered problems when configuration set to `{ "zones": [ { "target": "./client", "from": "./server" } ] }`: | ||
|
||
```js | ||
import baz from '../client/baz'; | ||
``` |
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,71 @@ | ||
import containsPath from 'contains-path' | ||
import path from 'path' | ||
|
||
import resolve from '../core/resolve' | ||
import isStaticRequire from '../core/staticRequire' | ||
|
||
module.exports = function noRestrictedPaths(context) { | ||
const options = context.options[0] || {} | ||
const restrictedPaths = options.zones || [] | ||
const basePath = options.basePath || process.cwd() | ||
const currentFilename = context.getFilename() | ||
const matchingZones = restrictedPaths.filter((zone) => { | ||
const targetPath = path.resolve(basePath, zone.target) | ||
|
||
return containsPath(currentFilename, targetPath) | ||
}) | ||
|
||
function checkForRestrictedImportPath(importPath, node) { | ||
const absoluteImportPath = resolve(importPath, context) | ||
|
||
if (!absoluteImportPath) { | ||
return | ||
} | ||
|
||
matchingZones.forEach((zone) => { | ||
const absoluteFrom = path.resolve(basePath, zone.from) | ||
|
||
if (containsPath(absoluteImportPath, absoluteFrom)) { | ||
context.report({ | ||
node, | ||
message: `Unexpected path "${importPath}" imported in restricted zone.`, | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
checkForRestrictedImportPath(node.source.value, node.source) | ||
}, | ||
CallExpression(node) { | ||
if (isStaticRequire(node)) { | ||
const [ firstArgument ] = node.arguments | ||
|
||
checkForRestrictedImportPath(firstArgument.value, firstArgument) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports.schema = [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
zones: { | ||
type: 'array', | ||
minItems: 1, | ||
items: { | ||
type: 'object', | ||
properties: { | ||
target: { type: 'string' }, | ||
from: { type: 'string' }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
basePath: { type: 'string' }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
] |
Empty file.
Empty file.
Empty file.
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,94 @@ | ||
import { RuleTester } from 'eslint' | ||
import rule from 'rules/no-restricted-paths' | ||
|
||
import { test, testFilePath } from '../utils' | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('no-restricted-paths', rule, { | ||
valid: [ | ||
test({ | ||
code: 'import a from "../client/a.js"', | ||
filename: testFilePath('./restricted-paths/server/b.js'), | ||
options: [ { | ||
zones: [ { target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' } ], | ||
} ], | ||
}), | ||
test({ | ||
code: 'const a = require("../client/a.js")', | ||
filename: testFilePath('./restricted-paths/server/b.js'), | ||
options: [ { | ||
zones: [ { target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' } ], | ||
} ], | ||
}), | ||
test({ | ||
code: 'import b from "../server/b.js"', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ { | ||
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' } ], | ||
} ], | ||
}), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import b from "../server/b.js"', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ { | ||
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ], | ||
} ], | ||
errors: [ { | ||
message: 'Unexpected path "../server/b.js" imported in restricted zone.', | ||
line: 1, | ||
column: 15, | ||
} ], | ||
}), | ||
test({ | ||
code: 'import a from "../client/a"\nimport c from "./c"', | ||
filename: testFilePath('./restricted-paths/server/b.js'), | ||
options: [ { | ||
zones: [ | ||
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/client' }, | ||
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/server/c.js' }, | ||
], | ||
} ], | ||
errors: [ | ||
{ | ||
message: 'Unexpected path "../client/a" imported in restricted zone.', | ||
line: 1, | ||
column: 15, | ||
}, | ||
{ | ||
message: 'Unexpected path "./c" imported in restricted zone.', | ||
line: 2, | ||
column: 15, | ||
}, | ||
], | ||
}), | ||
test({ | ||
code: 'import b from "../server/b.js"', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ { | ||
zones: [ { target: './client', from: './server' } ], | ||
basePath: testFilePath('./restricted-paths'), | ||
} ], | ||
errors: [ { | ||
message: 'Unexpected path "../server/b.js" imported in restricted zone.', | ||
line: 1, | ||
column: 15, | ||
} ], | ||
}), | ||
test({ | ||
code: 'const b = require("../server/b.js")', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ { | ||
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ], | ||
} ], | ||
errors: [ { | ||
message: 'Unexpected path "../server/b.js" imported in restricted zone.', | ||
line: 1, | ||
column: 19, | ||
} ], | ||
}), | ||
], | ||
}) |