Skip to content

Commit 9f4f461

Browse files
authored
Fix: require-meta-schema: Fix false positive (#111)
* Add failed test * Fix test * Style * Fix * One more test * Unpin version
1 parent 0b4bcaf commit 9f4f461

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

lib/rules/require-meta-schema.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { findVariable } = require('eslint-utils');
34
const utils = require('../utils');
45

56
// ------------------------------------------------------------------------------
@@ -34,7 +35,8 @@ module.exports = {
3435

3536
create (context) {
3637
const sourceCode = context.getSourceCode();
37-
const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager);
38+
const { ast, scopeManager } = sourceCode;
39+
const info = utils.getRuleInfo(ast, scopeManager);
3840

3941
return {
4042
Program () {
@@ -56,8 +58,33 @@ module.exports = {
5658
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
5759
},
5860
});
59-
} else if (!['ArrayExpression', 'ObjectExpression'].includes(schemaNode.value.type)) {
60-
context.report({ node: schemaNode.value, messageId: 'wrongType' });
61+
return;
62+
}
63+
64+
let { value } = schemaNode;
65+
if (value.type === 'Identifier') {
66+
const variable = findVariable(
67+
scopeManager.acquire(value) || scopeManager.globalScope,
68+
value
69+
);
70+
71+
// If we can't find the declarator, we have to assume it's in correct type
72+
if (
73+
!variable ||
74+
!variable.defs ||
75+
!variable.defs[0] ||
76+
!variable.defs[0].node ||
77+
variable.defs[0].node.type !== 'VariableDeclarator' ||
78+
!variable.defs[0].node.init
79+
) {
80+
return;
81+
}
82+
83+
value = variable.defs[0].node.init;
84+
}
85+
86+
if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
87+
context.report({ node: value, messageId: 'wrongType' });
6188
}
6289
},
6390
};

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"url": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues"
2828
},
2929
"homepage": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin#readme",
30-
"dependencies": {},
30+
"dependencies": {
31+
"eslint-utils": "^2.1.0"
32+
},
3133
"devDependencies": {
3234
"@not-an-aardvark/node-release-script": "^0.1.0",
3335
"chai": "^4.1.0",

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

+41
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ ruleTester.run('require-meta-schema', rule, {
3232
create(context) {}
3333
};
3434
`,
35+
`
36+
const schema = [];
37+
module.exports = {
38+
meta: { schema },
39+
create(context) {}
40+
};
41+
`,
42+
`
43+
const foo = {};
44+
module.exports = {
45+
meta: { schema: foo },
46+
create(context) {}
47+
};
48+
`,
49+
`
50+
let schema;
51+
schema = foo ? [] : {};
52+
module.exports = {
53+
meta: { schema },
54+
create(context) {}
55+
};
56+
`,
57+
`
58+
const schema = [],
59+
created = (context) => {};
60+
module.exports = {
61+
meta: { schema },
62+
create
63+
};
64+
`,
3565
],
3666

3767
invalid: [
@@ -78,5 +108,16 @@ schema: [] },
78108
output: null,
79109
errors: [{ messageId: 'wrongType', type: 'Literal' }],
80110
},
111+
{
112+
code: `
113+
const schema = null;
114+
module.exports = {
115+
meta: { schema },
116+
create(context) {}
117+
};
118+
`,
119+
output: null,
120+
errors: [{ messageId: 'wrongType', type: 'Literal' }],
121+
},
81122
],
82123
});

0 commit comments

Comments
 (0)