Skip to content

Commit

Permalink
feat: import rules from package relative paths
Browse files Browse the repository at this point in the history
...instead of importing through public path.
  • Loading branch information
philippfromme committed Jul 21, 2023
1 parent bfd24f1 commit e088a87
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
19 changes: 11 additions & 8 deletions lib/support/compile-config.js
Original file line number Diff line number Diff line change
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,17 +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 pkgMain = resolver.require.resolve(pkg);
const pkgJsonPath = path.dirname(resolver.require.resolve(`${ pkg }/package.json`));

const pkgDir = path.dirname(pkgMain.split(path.sep).join(path.posix.sep));
const pkgMainPath = path.dirname(resolver.require.resolve(pkg));

return path.posix.normalize(path.posix.join(pkgDir, rule));
}
const relativePkgMainPath = path.relative(pkgJsonPath, pkgMainPath);

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

// absolute reference
return path.posix.normalize(rule);
// absolute reference
return path.posix.normalize(rule);
}
}
8 changes: 4 additions & 4 deletions test/integration/bundling/test/app.webpack.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var bpmnlint_rules_start_event_required__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(bpmnlint_rules_start_event_required__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var bpmnlint_rules_end_event_required__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! bpmnlint/rules/end-event-required */ "./node_modules/bpmnlint/rules/end-event-required.js");
/* harmony import */ var bpmnlint_rules_end_event_required__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(bpmnlint_rules_end_event_required__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _test_integration_bundling_node_modules_bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! bpmnlint-plugin-exported/src/foo */ "./node_modules/bpmnlint-plugin-exported/src/foo.js");
/* harmony import */ var _test_integration_bundling_node_modules_bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_test_integration_bundling_node_modules_bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! bpmnlint-plugin-exported/src/foo */ "./node_modules/bpmnlint-plugin-exported/src/foo.js");
/* harmony import */ var bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3__);

const cache = {};

Expand Down Expand Up @@ -87,11 +87,11 @@ cache['bpmnlint/end-event-required'] = (bpmnlint_rules_end_event_required__WEBPA



cache['bpmnlint-plugin-exported/foo'] = (_test_integration_bundling_node_modules_bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default());
cache['bpmnlint-plugin-exported/foo'] = (bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default());



cache['bpmnlint-plugin-exported/foo-absolute'] = (_test_integration_bundling_node_modules_bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default());
cache['bpmnlint-plugin-exported/foo-absolute'] = (bpmnlint_plugin_exported_src_foo__WEBPACK_IMPORTED_MODULE_3___default());

/***/ }),

Expand Down
4 changes: 2 additions & 2 deletions test/integration/compilation/test/bpmnlintrc.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ import rule_20 from 'bpmnlint-plugin-test/rules/no-label-foo';

cache['bpmnlint-plugin-test/no-label-foo'] = rule_20;

import rule_21 from '$ROOT/test/integration/compilation/node_modules/bpmnlint-plugin-exported/src/foo';
import rule_21 from 'bpmnlint-plugin-exported/src/foo';

cache['bpmnlint-plugin-exported/foo'] = rule_21;

import rule_22 from '$ROOT/test/integration/compilation/node_modules/bpmnlint-plugin-exported/src/bar';
import rule_22 from 'bpmnlint-plugin-exported/src/bar';

cache['bpmnlint-plugin-exported/bar'] = rule_22;

Expand Down
65 changes: 61 additions & 4 deletions test/spec/support/compile-config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('support/compile-config', function() {

describe('should import custom path rules', function() {

it('through external source', async function() {
it('through external source (linux)', async function() {

// given
const resolver = new NodeResolver({
Expand All @@ -95,17 +95,71 @@ describe('support/compile-config', function() {
throw new Error('not found: ' + path);
},
function __resolve(path) {
if (path === 'bpmnlint-plugin-foreign/package.json') {
return '/bpmnlint-plugin-foreign/package.json';
}

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


throw new Error('not found: ' + path);
}
)
});

// when
const code = await compileConfig({
extends: 'plugin:foreign/recommended'
}, resolver);

// then
expect(code).to.contain('import rule_0 from \'bpmnlint-plugin-foreign/lib/rules/exported-path\'');
expect(code).to.contain('cache[\'bpmnlint-plugin-foreign/exported-path\'] = rule_0');

expect(code).to.contain('import rule_1 from \'some-rule-library\'');
expect(code).to.contain('cache[\'bpmnlint-plugin-foreign/exported-external-path\'] = rule_1');
});


it('through external source (win32)', async function() {

// given
const resolver = new NodeResolver({
require: createRequire(
function __require(path) {
if (path === 'bpmnlint-plugin-foreign') {
return {
configs: {
recommended: {
rules: {
'exported-path': 'error',
'exported-external-path': 'some-rule-library'
}
}
},
rules: {
'exported-path': './rules/exported-path',
'exported-external-path': 'some-rule-library'
}
};
}

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 'C:\\bpmnlint-plugin-foreign\\lib\\index.js';
}

throw new Error('not found: ' + path);
}
)
});

// when
const code = await compileConfig({
Expand Down Expand Up @@ -153,6 +207,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 +220,6 @@ describe('support/compile-config', function() {
)
});


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

0 comments on commit e088a87

Please sign in to comment.