-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 #155)
- Loading branch information
Showing
10 changed files
with
202 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,38 @@ | ||
# 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 tow options. The first option is an array to define all restricted zones. The second option is optional and allows you to override the `basePath` which is the current working directory by default. All paths defined in the first option are either absolute paths or relative to the `basePath`. | ||
|
||
|
||
### 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 `[ { "target": "./client", "from": "./server" } ]`: | ||
|
||
```js | ||
import bar from '../server/bar'; | ||
``` | ||
|
||
The following patterns are not considered problems when configuration set to `[ { "target": "./client", "from": "./server" } ]`: | ||
|
||
```js | ||
import foo from '../server/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,70 @@ | ||
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 restrictedPaths = context.options[0] || [] | ||
const basePath = context.options[1] && context.options[1].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: 'array', | ||
minItems: 1, | ||
items: { | ||
type: 'object', | ||
properties: { | ||
target: { type: 'string' }, | ||
from: { type: 'string' }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
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,87 @@ | ||
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 foo from "../client/foo.js"', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ | ||
[ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ], | ||
], | ||
}), | ||
test({ | ||
code: 'const foo = require("../client/foo.js")', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ | ||
[ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ], | ||
], | ||
}), | ||
], | ||
|
||
invalid: [ | ||
test({ | ||
code: 'import b from "../server/b.js"', | ||
filename: testFilePath('./restricted-paths/client/a.js'), | ||
options: [ | ||
[ { 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: [ | ||
[ | ||
{ 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: [ | ||
[ { 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: [ | ||
[ { 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, | ||
} ], | ||
}), | ||
], | ||
}) |