Skip to content

require-meta-schema: Fix false positive #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { findVariable } = require('eslint-utils');
const utils = require('../utils');

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -34,7 +35,8 @@ module.exports = {

create (context) {
const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode.ast, sourceCode.scopeManager);
const { ast, scopeManager } = sourceCode;
const info = utils.getRuleInfo(ast, scopeManager);

return {
Program () {
Expand All @@ -56,8 +58,33 @@ module.exports = {
return utils.insertProperty(fixer, metaNode, 'schema: []', sourceCode);
},
});
} else if (!['ArrayExpression', 'ObjectExpression'].includes(schemaNode.value.type)) {
context.report({ node: schemaNode.value, messageId: 'wrongType' });
return;
}

let { value } = schemaNode;
if (value.type === 'Identifier') {
const variable = findVariable(
scopeManager.acquire(value) || scopeManager.globalScope,
value
);

// If we can't find the declarator, we have to assume it's in correct type
if (
!variable ||
!variable.defs ||
!variable.defs[0] ||
!variable.defs[0].node ||
variable.defs[0].node.type !== 'VariableDeclarator' ||
!variable.defs[0].node.init
) {
return;
}

value = variable.defs[0].node.init;
}

if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
context.report({ node: value, messageId: 'wrongType' });
}
},
};
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"url": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues"
},
"homepage": "https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin#readme",
"dependencies": {},
"dependencies": {
"eslint-utils": "^2.1.0"
},
"devDependencies": {
"@not-an-aardvark/node-release-script": "^0.1.0",
"chai": "^4.1.0",
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ ruleTester.run('require-meta-schema', rule, {
create(context) {}
};
`,
`
const schema = [];
module.exports = {
meta: { schema },
create(context) {}
};
`,
`
const foo = {};
module.exports = {
meta: { schema: foo },
create(context) {}
};
`,
`
let schema;
schema = foo ? [] : {};
module.exports = {
meta: { schema },
create(context) {}
};
`,
`
const schema = [],
created = (context) => {};
module.exports = {
meta: { schema },
create
};
`,
],

invalid: [
Expand Down Expand Up @@ -78,5 +108,16 @@ schema: [] },
output: null,
errors: [{ messageId: 'wrongType', type: 'Literal' }],
},
{
code: `
const schema = null;
module.exports = {
meta: { schema },
create(context) {}
};
`,
output: null,
errors: [{ messageId: 'wrongType', type: 'Literal' }],
},
],
});