-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement new rule no-restricted-paths (fixes #155) #371
Merged
+212
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
} ], | ||
}), | ||
], | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind adding some tests where the filename is in the
server
folder, for completeness' sake?