Skip to content

Commit 1353dd5

Browse files
committed
fix: correctly resolve parserOpts #115 #95
1 parent f3583a0 commit 1353dd5

File tree

7 files changed

+40
-37
lines changed

7 files changed

+40
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
rules: {
3+
'references-empty': [2, 'never']
4+
},
5+
parserPreset: {
6+
parserOpts: {
7+
issuePrefixes: ['REF-']
8+
}
9+
}
10+
};

@commitlint/cli/fixtures/parser-preset/commitlint.config.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module.exports = {
2-
parserOpts: {
3-
parserPreset: './parser-preset'
4-
},
2+
parserPreset: './parser-preset',
53
rules: {
64
'type-enum': [2, 'always', ['type']],
75
'scope-enum': [2, 'always', ['scope']],

@commitlint/cli/src/cli.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ async function main(options) {
9696
throw err;
9797
}
9898

99-
return Promise.all(
100-
messages.map(async message => {
101-
const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
102-
const parserOpts = selectParserOpts(loaded.parserPreset);
103-
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
99+
const loaded = await core.load(getSeed(flags), {cwd: flags.cwd});
100+
const parserOpts = selectParserOpts(loaded.parserPreset);
101+
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
104102

105-
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
106-
if (range.edit) {
107-
opts.parserOpts.commentChar = '#';
108-
}
103+
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
104+
if (range.edit) {
105+
opts.parserOpts.commentChar = '#';
106+
}
109107

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

@@ -182,13 +182,11 @@ function selectParserOpts(parserPreset) {
182182
return undefined;
183183
}
184184

185-
const opts = parserPreset.opts;
186-
187-
if (typeof opts !== 'object') {
185+
if (typeof parserPreset.parserOpts !== 'object') {
188186
return undefined;
189187
}
190188

191-
return opts.parserOpts;
189+
return parserPreset.parserOpts;
192190
}
193191

194192
// Catch unhandled rejections globally

@commitlint/cli/src/cli.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ test('should handle --amend with signoff', async () => {
182182
await execa('git', ['commit', '--amend', '--no-edit'], {cwd});
183183
});
184184

185+
test('should handle linting with issue prefixes', async t => {
186+
const cwd = await git.bootstrap('fixtures/issue-prefixes');
187+
const actual = await cli([], {cwd})('foobar REF-1');
188+
t.is(actual.code, 0);
189+
});
190+
185191
async function writePkg(payload, options) {
186192
const pkgPath = path.join(options.cwd, 'package.json');
187193
const pkg = JSON.parse(await sander.readFile(pkgPath));

@commitlint/core/src/load.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
2626
config.parserPreset = {
2727
name: config.parserPreset,
2828
path: resolvedParserPreset,
29-
opts: require(resolvedParserPreset)
29+
parserOpts: (await require(resolvedParserPreset)).parserOpts
3030
};
3131
}
3232

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

4040
const preset = valid(mergeWith(extended, config, w));
41-
4241
// Await parser-preset if applicable
4342
if (
4443
typeof preset.parserPreset === 'object' &&
45-
typeof preset.parserPreset.opts === 'object'
44+
typeof preset.parserPreset.parserOpts === 'object' &&
45+
typeof preset.parserPreset.parserOpts.then === 'function'
4646
) {
47-
preset.parserPreset.opts = await preset.parserPreset.opts;
47+
preset.parserPreset.parserOpts = (await preset.parserPreset
48+
.parserOpts).parserOpts;
4849
}
4950

5051
// Execute rule config functions if needed

@commitlint/core/src/load.test.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ test('uses seed with parserPreset', async t => {
2424
},
2525
{cwd}
2626
);
27-
2827
t.is(actual.name, './conventional-changelog-custom');
29-
t.deepEqual(actual.opts, {
30-
parserOpts: {
31-
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
32-
}
28+
t.deepEqual(actual.parserOpts, {
29+
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
3330
});
3431
});
3532

@@ -134,25 +131,19 @@ test('recursive extends with package.json file', async t => {
134131
test('parser preset overwrites completely instead of merging', async t => {
135132
const cwd = await git.bootstrap('fixtures/parser-preset-override');
136133
const actual = await load({}, {cwd});
137-
138134
t.is(actual.parserPreset.name, './custom');
139-
t.is(typeof actual.parserPreset.opts, 'object');
140-
t.deepEqual(actual.parserPreset.opts, {
141-
b: 'b',
142-
parserOpts: {
143-
headerPattern: /.*/
144-
}
135+
t.deepEqual(actual.parserPreset.parserOpts, {
136+
headerPattern: /.*/
145137
});
146138
});
147139

148140
test('recursive extends with parserPreset', async t => {
149141
const cwd = await git.bootstrap('fixtures/recursive-parser-preset');
150142
const actual = await load({}, {cwd});
151-
152143
t.is(actual.parserPreset.name, './conventional-changelog-custom');
153-
t.is(typeof actual.parserPreset.opts, 'object');
144+
t.is(typeof actual.parserPreset.parserOpts, 'object');
154145
t.deepEqual(
155-
actual.parserPreset.opts.parserOpts.headerPattern,
146+
actual.parserPreset.parserOpts.headerPattern,
156147
/^(\w*)(?:\((.*)\))?-(.*)$/
157148
);
158149
});

@commitlint/resolve-extends/src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ function loadExtends(config = {}, context = {}) {
4848
typeof c.parserPreset === 'string'
4949
) {
5050
const resolvedParserPreset = resolveFrom(cwd, c.parserPreset);
51-
5251
const parserPreset = {
5352
name: c.parserPreset,
5453
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
5554
.split(path.sep)
5655
.join('/'),
57-
opts: require(resolvedParserPreset)
56+
parserOpts: require(resolvedParserPreset)
5857
};
5958

6059
ctx.parserPreset = parserPreset;

0 commit comments

Comments
 (0)