Skip to content

Commit

Permalink
fix: correctly resolve parserOpts #115 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jan 9, 2018
1 parent f3583a0 commit 1353dd5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
10 changes: 10 additions & 0 deletions @commitlint/cli/fixtures/issue-prefixes/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
rules: {
'references-empty': [2, 'never']
},
parserPreset: {
parserOpts: {
issuePrefixes: ['REF-']
}
}
};
4 changes: 1 addition & 3 deletions @commitlint/cli/fixtures/parser-preset/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module.exports = {
parserOpts: {
parserPreset: './parser-preset'
},
parserPreset: './parser-preset',
rules: {
'type-enum': [2, 'always', ['type']],
'scope-enum': [2, 'always', ['scope']],
Expand Down
24 changes: 11 additions & 13 deletions @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ async function main(options) {
throw err;
}

return Promise.all(
messages.map(async message => {
const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};

// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
if (range.edit) {
opts.parserOpts.commentChar = '#';
}
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
if (range.edit) {
opts.parserOpts.commentChar = '#';
}

return Promise.all(
messages.map(async message => {
const report = await core.lint(message, loaded.rules, opts);
const formatted = core.format(report, {color: flags.color});

Expand Down Expand Up @@ -182,13 +182,11 @@ function selectParserOpts(parserPreset) {
return undefined;
}

const opts = parserPreset.opts;

if (typeof opts !== 'object') {
if (typeof parserPreset.parserOpts !== 'object') {
return undefined;
}

return opts.parserOpts;
return parserPreset.parserOpts;
}

// Catch unhandled rejections globally
Expand Down
6 changes: 6 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ test('should handle --amend with signoff', async () => {
await execa('git', ['commit', '--amend', '--no-edit'], {cwd});
});

test('should handle linting with issue prefixes', async t => {
const cwd = await git.bootstrap('fixtures/issue-prefixes');
const actual = await cli([], {cwd})('foobar REF-1');
t.is(actual.code, 0);
});

async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
Expand Down
9 changes: 5 additions & 4 deletions @commitlint/core/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
config.parserPreset = {
name: config.parserPreset,
path: resolvedParserPreset,
opts: require(resolvedParserPreset)
parserOpts: (await require(resolvedParserPreset)).parserOpts
};
}

Expand All @@ -38,13 +38,14 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
});

const preset = valid(mergeWith(extended, config, w));

// Await parser-preset if applicable
if (
typeof preset.parserPreset === 'object' &&
typeof preset.parserPreset.opts === 'object'
typeof preset.parserPreset.parserOpts === 'object' &&
typeof preset.parserPreset.parserOpts.then === 'function'
) {
preset.parserPreset.opts = await preset.parserPreset.opts;
preset.parserPreset.parserOpts = (await preset.parserPreset
.parserOpts).parserOpts;
}

// Execute rule config functions if needed
Expand Down
21 changes: 6 additions & 15 deletions @commitlint/core/src/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ test('uses seed with parserPreset', async t => {
},
{cwd}
);

t.is(actual.name, './conventional-changelog-custom');
t.deepEqual(actual.opts, {
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
}
t.deepEqual(actual.parserOpts, {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
});
});

Expand Down Expand Up @@ -134,25 +131,19 @@ test('recursive extends with package.json file', async t => {
test('parser preset overwrites completely instead of merging', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset-override');
const actual = await load({}, {cwd});

t.is(actual.parserPreset.name, './custom');
t.is(typeof actual.parserPreset.opts, 'object');
t.deepEqual(actual.parserPreset.opts, {
b: 'b',
parserOpts: {
headerPattern: /.*/
}
t.deepEqual(actual.parserPreset.parserOpts, {
headerPattern: /.*/
});
});

test('recursive extends with parserPreset', async t => {
const cwd = await git.bootstrap('fixtures/recursive-parser-preset');
const actual = await load({}, {cwd});

t.is(actual.parserPreset.name, './conventional-changelog-custom');
t.is(typeof actual.parserPreset.opts, 'object');
t.is(typeof actual.parserPreset.parserOpts, 'object');
t.deepEqual(
actual.parserPreset.opts.parserOpts.headerPattern,
actual.parserPreset.parserOpts.headerPattern,
/^(\w*)(?:\((.*)\))?-(.*)$/
);
});
Expand Down
3 changes: 1 addition & 2 deletions @commitlint/resolve-extends/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ function loadExtends(config = {}, context = {}) {
typeof c.parserPreset === 'string'
) {
const resolvedParserPreset = resolveFrom(cwd, c.parserPreset);

const parserPreset = {
name: c.parserPreset,
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
.split(path.sep)
.join('/'),
opts: require(resolvedParserPreset)
parserOpts: require(resolvedParserPreset)
};

ctx.parserPreset = parserPreset;
Expand Down

0 comments on commit 1353dd5

Please sign in to comment.