-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ckeditor/ck/14329
Feature (eslint-plugin-ckeditor5-rules): Created the `ckeditor5-rules/no-scoped-imports-within-package` that disallows using scoped import (like `"@ckeditor/ckeditor5-*"`) to the same package where the import declaration is located. Closes ckeditor/ckeditor5#14329. Other (eslint-config-ckeditor5): Enabled the `ckeditor5-rules/no-scoped-imports-within-package` in the ESLint configuration. Internal (eslint-config-ckeditor5): Bumped ESLint parser configuration to target the ES2020 grammar. Internal (eslint-plugin-ckeditor5-rules): Aligned all tests for all ESLint rules to target the ES2020 grammar.
- Loading branch information
Showing
33 changed files
with
863 additions
and
59 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
97 changes: 97 additions & 0 deletions
97
packages/eslint-plugin-ckeditor5-rules/lib/rules/no-scoped-imports-within-package.js
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,97 @@ | ||
/** | ||
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const upath = require( 'upath' ); | ||
const fs = require( 'fs-extra' ); | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow scoped import like "@ckeditor/*" to the same package where the import declaration is located.', | ||
category: 'CKEditor5', | ||
// eslint-disable-next-line max-len | ||
url: 'https://ckeditor.com/docs/ckeditor5/latest/framework/contributing/code-style.html#imports-within-a-package-ckeditor5-rulesno-scoped-imports-within-package' | ||
}, | ||
schema: [] | ||
}, | ||
create( context ) { | ||
const handler = callbackFactory( context ); | ||
|
||
return { | ||
ImportDeclaration: handler, | ||
ExportNamedDeclaration: handler, | ||
ExportAllDeclaration: handler | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* @param {Object} context | ||
* @return {Function} | ||
*/ | ||
function callbackFactory( context ) { | ||
return node => { | ||
if ( !node.source ) { | ||
return; | ||
} | ||
|
||
const importedPackageName = node.source.value; | ||
|
||
const isScopedImport = | ||
importedPackageName.startsWith( '@ckeditor/ckeditor5' ) || | ||
importedPackageName.startsWith( 'ckeditor5' ); | ||
|
||
if ( !isScopedImport ) { | ||
return; | ||
} | ||
|
||
const directory = upath.dirname( context.getFilename() ); | ||
const cwd = upath.normalizeTrim( context.getCwd() ); | ||
const packageName = getPackageName( directory, cwd ); | ||
|
||
if ( !packageName ) { | ||
return; | ||
} | ||
|
||
const isScopedImportToSamePackage = | ||
importedPackageName === packageName || | ||
importedPackageName.startsWith( `${ packageName }/` ); | ||
|
||
if ( isScopedImportToSamePackage ) { | ||
context.report( { | ||
node, | ||
message: 'Scoped import like "@ckeditor/*" to the same package where the import declaration is located is disallowed.' | ||
} ); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @param {String} directory | ||
* @param {String} cwd | ||
* @return {String|null} | ||
*/ | ||
function getPackageName( directory, cwd ) { | ||
const packageJsonPath = upath.join( directory, 'package.json' ); | ||
|
||
if ( fs.pathExistsSync( packageJsonPath ) ) { | ||
const packageJson = fs.readJsonSync( packageJsonPath, { throws: false } ); | ||
|
||
if ( packageJson ) { | ||
return packageJson.name; | ||
} | ||
} | ||
|
||
const parentDirectory = upath.dirname( directory ); | ||
|
||
if ( parentDirectory.startsWith( cwd ) ) { | ||
return getPackageName( parentDirectory, cwd ); | ||
} | ||
|
||
return null; | ||
} |
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
3 changes: 3 additions & 0 deletions
3
...s/no-scoped-imports-within-package/invalid/packages/ckeditor5-feature-nested/package.json
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,3 @@ | ||
{ | ||
"name": "@ckeditor/ckeditor5-feature-nested" | ||
} |
32 changes: 32 additions & 0 deletions
32
...orts-within-package/invalid/packages/ckeditor5-feature-nested/src/a/b/c/scoped-imports.js
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,32 @@ | ||
import name1 from '@ckeditor/ckeditor5-feature-nested'; | ||
import name2 from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
import * as name3 from '@ckeditor/ckeditor5-feature-nested'; | ||
import * as name4 from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
import { name5 } from '@ckeditor/ckeditor5-feature-nested'; | ||
import { name6 } from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
import { name as name7 } from '@ckeditor/ckeditor5-feature-nested'; | ||
import { name as name8 } from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
import { default as name9 } from '@ckeditor/ckeditor5-feature-nested'; | ||
import { default as name10 } from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
import '@ckeditor/ckeditor5-feature-nested'; | ||
import '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
export * from '@ckeditor/ckeditor5-feature-nested'; | ||
export * from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
export * as name11 from '@ckeditor/ckeditor5-feature-nested'; | ||
export * as name12 from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
export { name13 } from '@ckeditor/ckeditor5-feature-nested'; | ||
export { name14 } from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
export { name as name15 } from '@ckeditor/ckeditor5-feature-nested'; | ||
export { name as name16 } from '@ckeditor/ckeditor5-feature-nested/src'; | ||
|
||
export { default as name17 } from '@ckeditor/ckeditor5-feature-nested'; | ||
export { default as name18 } from '@ckeditor/ckeditor5-feature-nested/src'; |
3 changes: 3 additions & 0 deletions
3
...es/no-scoped-imports-within-package/invalid/packages/ckeditor5-feature-short/package.json
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,3 @@ | ||
{ | ||
"name": "ckeditor5-feature-short" | ||
} |
32 changes: 32 additions & 0 deletions
32
...-scoped-imports-within-package/invalid/packages/ckeditor5-feature-short/scoped-imports.js
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,32 @@ | ||
import name1 from 'ckeditor5-feature-short'; | ||
import name2 from 'ckeditor5-feature-short/src'; | ||
|
||
import * as name3 from 'ckeditor5-feature-short'; | ||
import * as name4 from 'ckeditor5-feature-short/src'; | ||
|
||
import { name5 } from 'ckeditor5-feature-short'; | ||
import { name6 } from 'ckeditor5-feature-short/src'; | ||
|
||
import { name as name7 } from 'ckeditor5-feature-short'; | ||
import { name as name8 } from 'ckeditor5-feature-short/src'; | ||
|
||
import { default as name9 } from 'ckeditor5-feature-short'; | ||
import { default as name10 } from 'ckeditor5-feature-short/src'; | ||
|
||
import 'ckeditor5-feature-short'; | ||
import 'ckeditor5-feature-short/src'; | ||
|
||
export * from 'ckeditor5-feature-short'; | ||
export * from 'ckeditor5-feature-short/src'; | ||
|
||
export * as name11 from 'ckeditor5-feature-short'; | ||
export * as name12 from 'ckeditor5-feature-short/src'; | ||
|
||
export { name13 } from 'ckeditor5-feature-short'; | ||
export { name14 } from 'ckeditor5-feature-short/src'; | ||
|
||
export { name as name15 } from 'ckeditor5-feature-short'; | ||
export { name as name16 } from 'ckeditor5-feature-short/src'; | ||
|
||
export { default as name17 } from 'ckeditor5-feature-short'; | ||
export { default as name18 } from 'ckeditor5-feature-short/src'; |
3 changes: 3 additions & 0 deletions
3
...fixtures/no-scoped-imports-within-package/invalid/packages/ckeditor5-feature/package.json
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,3 @@ | ||
{ | ||
"name": "@ckeditor/ckeditor5-feature" | ||
} |
32 changes: 32 additions & 0 deletions
32
...res/no-scoped-imports-within-package/invalid/packages/ckeditor5-feature/scoped-imports.js
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,32 @@ | ||
import name1 from '@ckeditor/ckeditor5-feature'; | ||
import name2 from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
import * as name3 from '@ckeditor/ckeditor5-feature'; | ||
import * as name4 from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
import { name5 } from '@ckeditor/ckeditor5-feature'; | ||
import { name6 } from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
import { name as name7 } from '@ckeditor/ckeditor5-feature'; | ||
import { name as name8 } from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
import { default as name9 } from '@ckeditor/ckeditor5-feature'; | ||
import { default as name10 } from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
import '@ckeditor/ckeditor5-feature'; | ||
import '@ckeditor/ckeditor5-feature/src'; | ||
|
||
export * from '@ckeditor/ckeditor5-feature'; | ||
export * from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
export * as name11 from '@ckeditor/ckeditor5-feature'; | ||
export * as name12 from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
export { name13 } from '@ckeditor/ckeditor5-feature'; | ||
export { name14 } from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
export { name as name15 } from '@ckeditor/ckeditor5-feature'; | ||
export { name as name16 } from '@ckeditor/ckeditor5-feature/src'; | ||
|
||
export { default as name17 } from '@ckeditor/ckeditor5-feature'; | ||
export { default as name18 } from '@ckeditor/ckeditor5-feature/src'; |
3 changes: 3 additions & 0 deletions
3
...res/no-scoped-imports-within-package/valid/packages/ckeditor5-feature-nested/package.json
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,3 @@ | ||
{ | ||
"name": "@ckeditor/ckeditor5-feature-nested" | ||
} |
43 changes: 43 additions & 0 deletions
43
...rts-within-package/valid/packages/ckeditor5-feature-nested/src/a/b/c/no-scoped-imports.js
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,43 @@ | ||
import name1 from 'no-scoped-import'; | ||
import name2 from './no-scoped-imports'; | ||
import name3 from '../no-scoped-import'; | ||
|
||
import * as name4 from 'no-scoped-import'; | ||
import * as name5 from './no-scoped-imports'; | ||
import * as name6 from '../no-scoped-import'; | ||
|
||
import { name7 } from 'no-scoped-import'; | ||
import { name8 } from './no-scoped-imports'; | ||
import { name9 } from '../no-scoped-import'; | ||
|
||
import { name as name10 } from 'no-scoped-import'; | ||
import { name as name11 } from './no-scoped-imports'; | ||
import { name as name12 } from '../no-scoped-import'; | ||
|
||
import { default as name13 } from 'no-scoped-import'; | ||
import { default as name14 } from './no-scoped-imports'; | ||
import { default as name15 } from '../no-scoped-import'; | ||
|
||
import 'no-scoped-import'; | ||
import './no-scoped-imports'; | ||
import '../no-scoped-import'; | ||
|
||
export * from 'no-scoped-import'; | ||
export * from './no-scoped-imports'; | ||
export * from '../no-scoped-import'; | ||
|
||
export * as name16 from 'no-scoped-import'; | ||
export * as name17 from './no-scoped-imports'; | ||
export * as name18 from '../no-scoped-import'; | ||
|
||
export { name19 } from 'no-scoped-import'; | ||
export { name20 } from './no-scoped-imports'; | ||
export { name21 } from '../no-scoped-import'; | ||
|
||
export { name as name22 } from 'no-scoped-import'; | ||
export { name as name23 } from './no-scoped-imports'; | ||
export { name as name24 } from '../no-scoped-import'; | ||
|
||
export { default as name25 } from 'no-scoped-import'; | ||
export { default as name26 } from './no-scoped-imports'; | ||
export { default as name27 } from '../no-scoped-import'; |
Oops, something went wrong.