Skip to content

Commit

Permalink
fix: fix rule import statements
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Jul 20, 2023
1 parent c8afb0b commit b77ac88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 13 additions & 8 deletions lib/support/compile-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path').posix;
const path = require('path');

const { isString } = require('min-dash');

Expand Down Expand Up @@ -133,7 +133,7 @@ function createImportStatement(pkg, ruleName, idx, resolver) {
);
}

const rulePath = computeRuleImport(resolver, pkg, rule);
const rulePath = computeRuleImport(resolver, pkg, originalPkg, rule);

return `
import rule_${ idx } from '${rulePath}';
Expand All @@ -150,15 +150,20 @@ import rule_${ idx } from '${ pkg }/rules/${ ruleName }';
cache['${ originalPkg }/${ ruleName }'] = rule_${ idx };`;
}

function computeRuleImport(resolver, pkg, rule) {
function computeRuleImport(resolver, pkg, originalPkg, rule) {

// local reference, resolved relative to pkg location
if (rule.startsWith('.')) {
const pkgDir = path.dirname(resolver.require.resolve(pkg));
const pkgJsonPath = path.win32.dirname(resolver.require.resolve(`${ pkg }/package.json`));

return path.posix.normalize(`${ pkgDir }/${ rule }`);
}
const pkgMainPath = path.win32.dirname(resolver.require.resolve(pkg));

const relativePkgMainPath = path.win32.relative(pkgJsonPath, pkgMainPath);

// absolute reference
return path.normalize(rule);
return path.posix.normalize(`${ originalPkg }/${ relativePkgMainPath }/${ rule }`);
} else {

// absolute reference
return path.posix.normalize(rule);
}
}
11 changes: 8 additions & 3 deletions test/spec/support/compile-config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ describe('support/compile-config', function() {
throw new Error('not found: ' + path);
},
function __resolve(path) {
if (path === 'bpmnlint-plugin-foreign/package.json') {
return 'C:\\bpmnlint-plugin-foreign/package.json';
}

if (path === 'bpmnlint-plugin-foreign') {
return 'bpmnlint-plugin-foreign/lib/index.js';
return 'C:\\bpmnlint-plugin-foreign/lib/index.js';
}


Expand All @@ -106,7 +109,6 @@ describe('support/compile-config', function() {
)
});


// when
const code = await compileConfig({
extends: 'plugin:foreign/recommended'
Expand Down Expand Up @@ -153,6 +155,10 @@ describe('support/compile-config', function() {
throw new Error('not found: ' + path);
},
function __resolve(path) {
if (path === './package.json') {
return 'bpmnlint-plugin-local/package.json';
}

if (path === '.') {
return 'bpmnlint-plugin-local/lib/index.js';
}
Expand All @@ -162,7 +168,6 @@ describe('support/compile-config', function() {
)
});


// when
const code = await compileConfig({
extends: 'plugin:local/recommended'
Expand Down

0 comments on commit b77ac88

Please sign in to comment.