Skip to content

Commit 77d3983

Browse files
committed
Merge branch 'main' into no-missing-placeholders-loc-fix
* main: fix: clarify report messages for no-missing-placeholders and no-unused-placeholders (eslint-community#278) fix: allow additional schema types in require-meta-schema (eslint-community#277)
2 parents 066e677 + f5a5c24 commit 77d3983

6 files changed

+71
-5
lines changed

lib/rules/no-missing-placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626
schema: [],
2727
messages: {
2828
placeholderDoesNotExist:
29-
'The placeholder {{{{missingKey}}}} does not exist.',
29+
"The placeholder {{{{missingKey}}}} is missing (must provide it in the report's `data` object).",
3030
},
3131
},
3232

lib/rules/no-unused-placeholders.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525
fixable: null,
2626
schema: [],
2727
messages: {
28-
placeholderUnused: 'The placeholder {{{{unusedKey}}}} is unused.',
28+
placeholderUnused:
29+
'The placeholder {{{{unusedKey}}}} is unused (does not exist in the actual message).',
2930
},
3031
},
3132

lib/rules/require-meta-schema.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ module.exports = {
104104
hasEmptySchema = true;
105105
}
106106

107-
if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
107+
if (
108+
value.type === 'Literal' ||
109+
(value.type === 'Identifier' && value.name === 'undefined')
110+
) {
108111
context.report({ node: value, messageId: 'wrongType' });
109112
}
110113
},

tests/lib/rules/no-missing-placeholders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const RuleTester = require('eslint').RuleTester;
2020
function error(missingKey, type, extra) {
2121
return {
2222
type,
23-
message: `The placeholder {{${missingKey}}} does not exist.`,
23+
message: `The placeholder {{${missingKey}}} is missing (must provide it in the report's \`data\` object).`,
2424
...extra,
2525
};
2626
}

tests/lib/rules/no-unused-placeholders.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const RuleTester = require('eslint').RuleTester;
1818
* @returns {object} An expected error
1919
*/
2020
function error(unusedKey, type = 'Literal') {
21-
return { type, message: `The placeholder {{${unusedKey}}} is unused.` };
21+
return {
22+
type,
23+
message: `The placeholder {{${unusedKey}}} is unused (does not exist in the actual message).`,
24+
};
2225
}
2326

2427
// ------------------------------------------------------------------------------

tests/lib/rules/require-meta-schema.js

+59
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,57 @@ ruleTester.run('require-meta-schema', rule, {
6363
`,
6464
parserOptions: { sourceType: 'module' },
6565
},
66+
// Variable schema with array value.
6667
`
6768
const schema = [];
6869
module.exports = {
6970
meta: { schema },
7071
create(context) {}
7172
};
7273
`,
74+
// Variable schema with object value.
7375
`
7476
const foo = {};
7577
module.exports = {
7678
meta: { schema: foo },
7779
create(context) {}
7880
};
7981
`,
82+
// Variable schema with no static value.
83+
`
84+
module.exports = {
85+
meta: { schema },
86+
create(context) {}
87+
};
88+
`,
89+
// Variable schema pointing to unknown variable chain.
90+
`
91+
module.exports = {
92+
meta: { schema: baseRule.meta.schema },
93+
create(context) {}
94+
};
95+
`,
96+
// Schema with function call as value.
97+
`
98+
module.exports = {
99+
meta: { schema: getSchema() },
100+
create(context) {}
101+
};
102+
`,
103+
// Schema with ternary (conditional) expression.
104+
`
105+
module.exports = {
106+
meta: { schema: foo ? [] : {} },
107+
create(context) {}
108+
};
109+
`,
110+
// Schema with logical expression.
111+
`
112+
module.exports = {
113+
meta: { schema: foo || {} },
114+
create(context) {}
115+
};
116+
`,
80117
`
81118
let schema;
82119
schema = foo ? [] : {};
@@ -296,6 +333,28 @@ schema: [] },
296333
output: null,
297334
errors: [{ messageId: 'wrongType', type: 'Identifier', suggestions: [] }],
298335
},
336+
{
337+
// Schema with number literal value.
338+
code: `
339+
module.exports = {
340+
meta: { schema: 123 },
341+
create(context) {}
342+
};
343+
`,
344+
output: null,
345+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
346+
},
347+
{
348+
// Schema with string literal value.
349+
code: `
350+
module.exports = {
351+
meta: { schema: 'hello world' },
352+
create(context) {}
353+
};
354+
`,
355+
output: null,
356+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
357+
},
299358
{
300359
code: `
301360
const schema = null;

0 commit comments

Comments
 (0)