Skip to content

Commit

Permalink
[Fix] no-relative-parent-imports: support TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan BUTNARU committed Sep 28, 2020
1 parent c51b6a9 commit 37cb3ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`extensions`]/importType: Fix @/abc being treated as scoped module ([#1854], thanks [@3nuc])
- allow using rest operator in named export ([#1878], thanks [@foray1010])
- [`dynamic-import-chunkname`]: allow single quotes to match Webpack support ([#1848], thanks [@straub])
- [`no-relative-parent-imports`]: check unresolved dependency type ([#1130], thanks [@bogdanb])

### Changed
- [`export`]: add tests for a name collision with `export * from` ([#1704], thanks @tomprats)
Expand Down Expand Up @@ -719,6 +720,7 @@ for info on changes for earlier releases.
[`no-named-export`]: ./docs/rules/no-named-export.md
[`no-namespace`]: ./docs/rules/no-namespace.md
[`no-nodejs-modules`]: ./docs/rules/no-nodejs-modules.md
[`no-relative-parent-imports`]: ./docs/rules/no-relative-parent-imports.md
[`no-restricted-paths`]: ./docs/rules/no-restricted-paths.md
[`no-self-import`]: ./docs/rules/no-self-import.md
[`no-unassigned-import`]: ./docs/rules/no-unassigned-import.md
Expand All @@ -732,6 +734,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1130]: https://github.com/benmosher/eslint-plugin-import/issues/1130
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
[#1848]: https://github.com/benmosher/eslint-plugin-import/pull/1848
Expand Down Expand Up @@ -1276,3 +1279,4 @@ for info on changes for earlier releases.
[@foray1010]: https://github.com/foray1010
[@tomprats]: https://github.com/tomprats
[@straub]: https://github.com/straub
[@bogdanb]: https://github.com/bogdanb
21 changes: 13 additions & 8 deletions src/rules/no-relative-parent-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ module.exports = {
const myPath = context.getFilename()
if (myPath === '<text>') return {} // can't check a non-file

function checkSourceValue(sourceNode) {
const depPath = sourceNode.value

if (importType(depPath, context) === 'external') { // ignore packages
return
}

function resolvedImportType(depPath) {
const absDepPath = resolve(depPath, context)

if (!absDepPath) { // unable to resolve path
Expand All @@ -33,7 +27,18 @@ module.exports = {

const relDepPath = relative(dirname(myPath), absDepPath)

if (importType(relDepPath, context) === 'parent') {
return importType(relDepPath, context)
}

function checkSourceValue(sourceNode) {
const depPath = sourceNode.value
const depType = importType(depPath, context)

if (depType === 'external') { // ignore packages
return
}

if (depType === 'parent' || resolvedImportType(depPath) === 'parent') {
context.report({
node: sourceNode,
message: 'Relative imports from parent directories are not allowed. ' +
Expand Down

0 comments on commit 37cb3ca

Please sign in to comment.