Skip to content

feat(load): resolve plugins correctly if provided as relative or absolute path #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion @commitlint/load/src/utils/load-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import loadPlugin from './load-plugin';
import chalk from 'chalk';

jest.mock('commitlint-plugin-example', () => ({example: true}), {
virtual: true,
Expand All @@ -8,6 +9,22 @@ jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), {
virtual: true,
});

jest.mock('./relative/posix.js', () => ({relativePosix: true}), {
virtual: true,
});

jest.mock('/absolute/posix.js', () => ({relativePosix: true}), {
virtual: true,
});

jest.mock('.\\relative\\windows.js', () => ({relativePosix: true}), {
virtual: true,
});

jest.mock('C:\\absolute\\windows.js', () => ({relativePosix: true}), {
virtual: true,
});

test('should load a plugin when referenced by short name', () => {
const plugins = loadPlugin({}, 'example');
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
Expand All @@ -34,9 +51,14 @@ test('should throw an error when a plugin has whitespace', () => {
});

test("should throw an error when a plugin doesn't exist", () => {
const spy = jest.spyOn(console, 'error').mockImplementation();
expect(() => loadPlugin({}, 'nonexistentplugin')).toThrow(
'Failed to load plugin'
'Failed to load plugin commitlint-plugin-nonexistentplugin.'
);
expect(spy).toBeCalledWith(
chalk.red(`Failed to load plugin commitlint-plugin-nonexistentplugin.`)
);
spy.mockRestore();
});

test('should load a scoped plugin when referenced by short name', () => {
Expand All @@ -63,3 +85,25 @@ test("should load a scoped plugin when referenced by long name, but should not g
const plugins = loadPlugin({}, '@scope/commitlint-plugin-example');
expect(plugins['example']).toBe(undefined);
});

test('should load a plugin when relative posix path is provided', () => {
const plugins = loadPlugin({}, './relative/posix.js');
expect(plugins['posix.js']).toBe(require('./relative/posix.js'));
});

test('should load a plugin when absolute posix path is provided', () => {
const plugins = loadPlugin({}, '/absolute/posix.js');
// eslint-disable-next-line import/no-absolute-path
expect(plugins['posix.js']).toBe(require('/absolute/posix.js'));
});

test('should load a plugin when relative windows path is provided', () => {
const plugins = loadPlugin({}, '.\\relative\\windows.js');
expect(plugins['windows.js']).toBe(require('.\\relative\\windows.js'));
});

test('should load a plugin when absolute windows path is provided', () => {
const plugins = loadPlugin({}, 'C:\\absolute\\windows.js');
// eslint-disable-next-line import/no-absolute-path
expect(plugins['windows.js']).toBe(require('C:\\absolute\\windows.js'));
});
5 changes: 2 additions & 3 deletions @commitlint/load/src/utils/load-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import chalk from 'chalk';
import {normalizePackageName, getShorthandName} from './plugin-naming';
import {normalizePackageName} from './plugin-naming';
import {WhitespacePluginError, MissingPluginError} from './plugin-errors';
import {PluginRecords} from '@commitlint/types';

Expand All @@ -9,8 +9,7 @@ export default function loadPlugin(
pluginName: string,
debug: boolean = false
): PluginRecords {
const longName = normalizePackageName(pluginName);
const shortName = getShorthandName(longName);
const {longName, shortName} = normalizePackageName(pluginName);
let plugin = null;

if (pluginName.match(/\s+/u)) {
Expand Down
72 changes: 39 additions & 33 deletions @commitlint/load/src/utils/plugin-naming.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import path from 'path';

// largely adapted from eslint's plugin system
const NAMESPACE_REGEX = /^@.*\//iu;
// In eslint this is a parameter - we don't need to support the extra options
const prefix = 'commitlint-plugin';

// Replace Windows with posix style paths
function convertPathToPosix(filepath: string) {
/**
* Replace Windows with posix style paths
*/
function convertPathToPosix(filepath: string): string {
const normalizedFilepath = path.normalize(filepath);
const posixFilepath = normalizedFilepath.replace(/\\/gu, '/');

return posixFilepath;
return normalizedFilepath.replace(/\\/gu, '/');
}

/**
* Brings package name to correct format based on prefix
* @param {string} name The name of the package.
* @returns {string} Normalized name of the package
* @private
* @param name The name of the package.
* @returns Normalized name of the package
* @internal
*/
export function normalizePackageName(name: string) {
export function normalizePackageName(
name: string
): {longName: string; shortName: string} {
let normalizedName = name;

if (
path.isAbsolute(name) ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nodejs.org/api/path.html#path_path_isabsolute_path

For example, on POSIX:

path.isAbsolute('/foo/bar'); // true
path.isAbsolute('/baz/..');  // true
path.isAbsolute('qux/');     // false
path.isAbsolute('.');        // false

On Windows:

path.isAbsolute('//server');    // true
path.isAbsolute('\\\\server');  // true
path.isAbsolute('C:/foo/..');   // true
path.isAbsolute('C:\\foo\\..'); // true
path.isAbsolute('bar\\baz');    // false
path.isAbsolute('bar/baz');     // false
path.isAbsolute('.');           // false

name.startsWith('./') ||
name.startsWith('../') ||
name.startsWith('.\\') ||
name.startsWith('..\\')
Comment on lines +27 to +30
Copy link
Contributor Author

@armano2 armano2 Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be done in form of regexp

/^\.{1,2}(\/|\\)/g

https://regexr.com/5k20p

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, personally I like the readability of the current code as long as won't grow to more cases.

) {
return {
longName: name,
shortName: path.basename(name) || name,
};
}

/**
* On Windows, name can come in with Windows slashes instead of Unix slashes.
* Normalize to Unix first to avoid errors later on.
Expand Down Expand Up @@ -61,40 +74,33 @@ export function normalizePackageName(name: string) {
normalizedName = `${prefix}-${normalizedName}`;
}

return normalizedName;
return {
longName: normalizedName,
shortName: getShorthandName(normalizedName),
};
}

/**
* Removes the prefix from a fullname.
* @param {string} fullname The term which may have the prefix.
* @returns {string} The term without prefix.
* Removes the prefix from a fullName.
* @param fullName The term which may have the prefix.
* @returns The term without prefix.
* @internal
*/
export function getShorthandName(fullname: string) {
if (fullname[0] === '@') {
let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, 'u').exec(fullname);
export function getShorthandName(fullName: string): string {
if (fullName[0] === '@') {
let matchResult = new RegExp(`^(@[^/]+)/${prefix}$`, 'u').exec(fullName);

if (matchResult) {
return matchResult[1];
}

matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, 'u').exec(fullname);
matchResult = new RegExp(`^(@[^/]+)/${prefix}-(.+)$`, 'u').exec(fullName);
if (matchResult) {
return `${matchResult[1]}/${matchResult[2]}`;
}
} else if (fullname.startsWith(`${prefix}-`)) {
return fullname.slice(prefix.length + 1);
} else if (fullName.startsWith(`${prefix}-`)) {
return fullName.slice(prefix.length + 1);
}

return fullname;
}

/**
* Gets the scope (namespace) of a term.
* @param {string} term The term which may have the namespace.
* @returns {string} The namepace of the term if it has one.
*/
export function getNamespaceFromTerm(term: string) {
const match = term.match(NAMESPACE_REGEX);

return match ? match[0] : '';
return fullName;
}