-
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 #33 from ckeditor/update-allow-imports-only-from-m…
…ain-package-entry-point-rule Fix (eslint-plugin-ckeditor5-rules): Fix `ckeditor5-rules/require-file-extensions-in-imports` rule for packages with `exports` that do not automatically add file extension. Feature (eslint-plugin-ckeditor5-rules): Add the `ckeditor5-rules/no-legacy-imports` rule to detect and fix imports used in the old installation methods. Feature (eslint-plugin-ckeditor5-rules): The `ckeditor5-rules/allow-imports-only-from-main-package-entry-point` rule should disallow importing from any path other than the package main entry point. Closes ckeditor/ckeditor5#16559 MINOR BREAKING CHANGE (eslint-plugin-ckeditor5-rules): Removed the `ckeditor5-rules/no-cross-package-svg-imports` rule, as it does the same thing as the updated `allow-imports-only-from-main-package-entry-point` rule.
- Loading branch information
Showing
8 changed files
with
275 additions
and
128 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
60 changes: 0 additions & 60 deletions
60
packages/eslint-plugin-ckeditor5-rules/lib/rules/no-cross-package-svg-imports.js
This file was deleted.
Oops, something went wrong.
143 changes: 143 additions & 0 deletions
143
packages/eslint-plugin-ckeditor5-rules/lib/rules/no-legacy-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,143 @@ | ||
/** | ||
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// Allowed packages to be checked. | ||
const corePackageName = 'ckeditor5'; | ||
const premiumPackageName = 'ckeditor5-premium-features'; | ||
|
||
// Default packages to check. | ||
const defaultPackages = [ | ||
corePackageName, | ||
premiumPackageName | ||
]; | ||
|
||
// Package dependency cache to avoid excessive file reads. | ||
const cache = new Map(); | ||
|
||
/** | ||
* Returns an array of dependencies of a given package. | ||
* | ||
* @param {string} packageName | ||
* @returns {Array<string>} | ||
*/ | ||
function getDependencies( packageName ) { | ||
if ( cache.has( packageName ) ) { | ||
return cache.get( packageName ); | ||
} | ||
|
||
try { | ||
const packageJsonPath = require.resolve( | ||
`${ packageName }/package.json`, | ||
{ paths: [ process.cwd() ] } | ||
); | ||
|
||
const packageJson = require( packageJsonPath ); | ||
const dependencies = Object.keys( packageJson.dependencies || {} ); | ||
|
||
cache.set( packageName, dependencies ); | ||
|
||
return dependencies; | ||
} catch { | ||
cache.set( packageName, [] ); | ||
|
||
return []; | ||
} | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow legacy imports from CKEditor5 packages.', | ||
category: 'CKEditor5', | ||
// eslint-disable-next-line max-len | ||
url: 'https://ckeditor.com/docs/ckeditor5/latest/framework/contributing/code-style.html#no-legacy-imports' | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
legacyImport: 'Import must be done from the "{{ packageName }}" package' | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
packages: { | ||
type: 'array', | ||
items: [ | ||
// Allow only core and premium packages to be checked. | ||
{ | ||
anyOf: [ | ||
{ | ||
enum: [ corePackageName, premiumPackageName ] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
create( context ) { | ||
// Get the packages to check from the rule options. | ||
const { packages = defaultPackages } = context.options[ 0 ] || {}; | ||
|
||
/** | ||
* Checks if the given path is an invalid core package import. | ||
* | ||
* @param {string} path | ||
* @returns boolean | ||
*/ | ||
function isInvalidCoreImport( path ) { | ||
return packages.includes( corePackageName ) && | ||
getDependencies( corePackageName ).includes( path ) || | ||
path.startsWith( 'ckeditor5/src/' ); | ||
} | ||
|
||
/** | ||
* Checks if the given path is an invalid premium package import. | ||
* | ||
* @param {string} path | ||
* @returns boolean | ||
*/ | ||
function isInvalidPremiumImport( path ) { | ||
return packages.includes( premiumPackageName ) && | ||
getDependencies( premiumPackageName ).includes( path ) || | ||
path.startsWith( 'ckeditor5-collaboration/src/' ); | ||
} | ||
|
||
function report( node, packageName ) { | ||
// Get the original quoting style from the source code (" or '). | ||
const quoteStyle = node.source.raw[ 0 ]; | ||
|
||
return context.report( { | ||
node, | ||
messageId: 'legacyImport', | ||
data: { | ||
packageName | ||
}, | ||
fix( fixer ) { | ||
return fixer.replaceText( node.source, quoteStyle + packageName + quoteStyle ); | ||
} | ||
} ); | ||
} | ||
|
||
return { | ||
ImportDeclaration( node ) { | ||
const path = node.source.value; | ||
|
||
if ( isInvalidCoreImport( path ) ) { | ||
return report( node, corePackageName ); | ||
} | ||
|
||
if ( isInvalidPremiumImport( path ) ) { | ||
return report( node, premiumPackageName ); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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
43 changes: 0 additions & 43 deletions
43
packages/eslint-plugin-ckeditor5-rules/tests/rules/no-cross-package-svg-imports.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.