Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from ckeditor/ck/10030
Browse files Browse the repository at this point in the history
Fix: Disallow importing from other directories than "src/" when importing from the "ckeditor5" package. Closes ckeditor/ckeditor5#10030.
  • Loading branch information
mlewand committed Jul 5, 2021
2 parents ebfcdb3 + c04b599 commit d120742
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/rules/ckeditor-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const path = require( 'path' );

const SHORT_PACKAGE_NAME_IMPORT_REGEXP = /@ckeditor\/ckeditor5?-([^/]+)/;
const SHORT_PACKAGE_NAME_PATH_REGEXP = /ckeditor5?-([^/\\]+)/;
const CKEDITOR5_PACKAGE_IMPORT_REGEXP = /ckeditor5\/(?!src)/;

const DLL_IMPORT_ERROR = 'Imports from DLL packages must be done using the "ckeditor5" package.';
const CKEDITOR5_INVALID_IMPORT = 'Imports from the `ckeditor5` package must use the `src/` directory.';

module.exports = {
meta: {
Expand Down Expand Up @@ -42,6 +44,17 @@ module.exports = {
const importPath = node.source.value;
const matchResult = importPath.match( SHORT_PACKAGE_NAME_IMPORT_REGEXP );

// Do not allow importing from the `ckeditor5` package from other directories than `src/`.
// See: https://github.com/ckeditor/ckeditor5/issues/10030.
if ( importPath.match( CKEDITOR5_PACKAGE_IMPORT_REGEXP ) ) {
context.report( {
node,
message: CKEDITOR5_INVALID_IMPORT
} );

return;
}

// If the short package name was not found, it means that 3rd party package or local file is being imported. It is fine.
if ( !matchResult ) {
return;
Expand Down
16 changes: 16 additions & 0 deletions tests/ckeditor-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const DLL_IMPORT_ERROR = {
message: 'Imports from DLL packages must be done using the "ckeditor5" package.'
};

const CKEDITOR5_INVALID_IMPORT = {
message: 'Imports from the `ckeditor5` package must use the `src/` directory.'
};

const ruleTester = new RuleTester( {
parserOptions: {
sourceType: 'module',
Expand Down Expand Up @@ -159,6 +163,18 @@ ruleTester.run( 'eslint-plugin-ckeditor5-rules/ckeditor-imports', ckeditorImport
code: 'import okIcon from \'@ckeditor/ckeditor5-core/theme/icons/ok.svg\';',
filename: path.win32.join( 'C:', 'Workspace', 'ckeditor', 'ckeditor5', 'packages', 'ckeditor5-basic-styles', 'src', 'bold.js' ),
errors: [ DLL_IMPORT_ERROR ]
},

// Importing from other directories than `src/` from the `ckeditor5` package.
{
code: 'import \'ckeditor5/packages/ckeditor5-ui/theme/components/responsive-form/responsiveform.css\';',
filename: '/Users/Workspace/ckeditor/ckeditor5/packages/ckeditor5-basic-styles/src/bold.js',
errors: [ CKEDITOR5_INVALID_IMPORT ]
},
{
code: 'import \'ckeditor5/packages/ckeditor5-ui/theme/components/responsive-form/responsiveform.css\';',
filename: path.win32.join( 'C:', 'Workspace', 'ckeditor', 'ckeditor5', 'packages', 'ckeditor5-basic-styles', 'src', 'bold.js' ),
errors: [ CKEDITOR5_INVALID_IMPORT ]
}
]
} );
Expand Down

0 comments on commit d120742

Please sign in to comment.