Skip to content

Commit

Permalink
Merge fb63365 into 798eed7
Browse files Browse the repository at this point in the history
  • Loading branch information
rsolomon authored Nov 27, 2018
2 parents 798eed7 + fb63365 commit 3741542
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Fixed
- [`no-extraneous-dependencies`]: `packageDir` option with array value was clobbering package deps instead of merging them ([#1175]/[#1176], thanks [@aravindet] & [@pzhine])
-
### Added
- [`no-restricted-paths`]: New `except` option per `zone`, allowing exceptions to be defined for a restricted zone.


## [2.14.0] - 2018-08-13
Expand All @@ -14,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
| * e30a757 (source/pr/1151, fork/jsx) Add JSX check to namespace rule
|/
* 8252344 (source/pr/1148) Add error to output when module loaded as resolver has invalid API

### Added
- [`no-useless-path-segments`]: add commonJS (CJS) support ([#1128], thanks [@1pete])
- [`namespace`]: add JSX check ([#1151], thanks [@jf248])
Expand Down
42 changes: 41 additions & 1 deletion docs/rules/no-restricted-paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In order to prevent such scenarios this rule allows you to define restricted zon

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.
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. An optional `except` may be defined for a zone, allowing exception paths that would otherwise violate the related `from`. Note that `except` is relative to `from` and cannot backtrack to a parent directory.

### Examples

Expand Down Expand Up @@ -37,3 +37,43 @@ The following patterns are not considered problems when configuration set to `{
```js
import baz from '../client/baz';
```

---------------

Given the following folder structure:

```
my-project
├── client
│ └── foo.js
│ └── baz.js
└── server
├── one
│ └── a.js
│ └── b.js
└── two
```

and the current file being linted is `my-project/server/one/a.js`.

and the current configuration is set to:

```
{ "zones": [ {
"target": "./tests/files/restricted-paths/server/one",
"from": "./tests/files/restricted-paths/server",
"except": ["./one"]
} ] }
```

The following pattern is considered a problem:

```js
import a from '../two/a'
```

The following pattern is not considered a problem:

```js
import b from './b'
```
55 changes: 50 additions & 5 deletions src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import resolve from 'eslint-module-utils/resolve'
import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'
import importType from '../core/importType'

module.exports = {
meta: {
Expand All @@ -23,6 +24,13 @@ module.exports = {
properties: {
target: { type: 'string' },
from: { type: 'string' },
except: {
type: 'array',
items: {
type: 'string',
},
uniqueItems: true,
},
},
additionalProperties: false,
},
Expand All @@ -45,6 +53,20 @@ module.exports = {
return containsPath(currentFilename, targetPath)
})

function isValidExceptionPath(absoluteFromPath, absoluteExceptionPath) {
const relativeExceptionPath = path.relative(absoluteFromPath, absoluteExceptionPath)

return importType(relativeExceptionPath, context) !== 'parent'
}

function reportInvalidExceptionPath(node) {
context.report({
node,
message: 'Restricted path exceptions must be descendants of the configured ' +
'`from` path for that zone.',
})
}

function checkForRestrictedImportPath(importPath, node) {
const absoluteImportPath = resolve(importPath, context)

Expand All @@ -53,14 +75,37 @@ module.exports = {
}

matchingZones.forEach((zone) => {
const exceptionPaths = zone.except || []
const absoluteFrom = path.resolve(basePath, zone.from)

if (containsPath(absoluteImportPath, absoluteFrom)) {
context.report({
node,
message: `Unexpected path "${importPath}" imported in restricted zone.`,
})
if (!containsPath(absoluteImportPath, absoluteFrom)) {
return
}

const absoluteExceptionPaths = exceptionPaths.map((exceptionPath) =>
path.resolve(absoluteFrom, exceptionPath)
)
const hasValidExceptionPaths = absoluteExceptionPaths.every((absoluteExceptionPath) =>
isValidExceptionPath(absoluteFrom, absoluteExceptionPath)
)

if (!hasValidExceptionPaths) {
reportInvalidExceptionPath(node)
return
}

const pathIsExcepted = absoluteExceptionPaths.some((absoluteExceptionPath) =>
containsPath(absoluteImportPath, absoluteExceptionPath)
)

if (pathIsExcepted) {
return
}

context.report({
node,
message: `Unexpected path "${importPath}" imported in restricted zone.`,
})
})
}

Expand Down
Empty file.
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions tests/src/rules/no-restricted-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ ruleTester.run('no-restricted-paths', rule, {
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' } ],
} ],
}),
test({
code: 'import a from "./a.js"',
filename: testFilePath('./restricted-paths/server/one/a.js'),
options: [ {
zones: [ {
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./one'],
} ],
} ],
}),
test({
code: 'import a from "../two/a.js"',
filename: testFilePath('./restricted-paths/server/one/a.js'),
options: [ {
zones: [ {
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./two'],
} ],
} ],
}),


// irrelevant function calls
Expand Down Expand Up @@ -107,5 +129,38 @@ ruleTester.run('no-restricted-paths', rule, {
column: 19,
} ],
}),
test({
code: 'import b from "../two/a.js"',
filename: testFilePath('./restricted-paths/server/one/a.js'),
options: [ {
zones: [ {
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['./one'],
} ],
} ],
errors: [ {
message: 'Unexpected path "../two/a.js" imported in restricted zone.',
line: 1,
column: 15,
} ],
}),
test({
code: 'import b from "../two/a.js"',
filename: testFilePath('./restricted-paths/server/one/a.js'),
options: [ {
zones: [ {
target: './tests/files/restricted-paths/server/one',
from: './tests/files/restricted-paths/server',
except: ['../client/a'],
} ],
} ],
errors: [ {
message: 'Restricted path exceptions must be descendants of the configured ' +
'`from` path for that zone.',
line: 1,
column: 15,
} ],
}),
],
})

0 comments on commit 3741542

Please sign in to comment.