Skip to content

Fix: handle spreads in rule meta objects #100

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 1 commit into from
Jun 10, 2020
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
4 changes: 4 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ module.exports = {
* @returns {string|null} The key name, or `null` if the name cannot be determined statically.
*/
getKeyName (property) {
if (!property.key) {
// likely a SpreadElement or another non-standard node
return null;
}
if (!property.computed && property.key.type === 'Identifier') {
return property.key.name;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/require-meta-fixable.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ ruleTester.run('require-meta-fixable', rule, {
create(context) { context.report(node, message, data, fix); }
};
`,
{
code: `
const meta = {};
module.exports = {
...meta,
meta: {},
create(context) { context.report(node, message, data, fix); }
};
`,
parserOptions: {
ecmaVersion: 9,
},
},
],

invalid: [
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ describe('utils', () => {
assert.strictEqual(utils.getKeyName(ast.body[0].expression.properties[0]), CASES[objectSource]);
});
});

const CASES_ES9 = {
'({ ...foo })': null,
};
Object.keys(CASES_ES9).forEach(objectSource => {
it(objectSource, () => {
const ast = espree.parse(objectSource, { ecmaVersion: 9 });

assert.strictEqual(utils.getKeyName(ast.body[0].expression.properties[0]), CASES_ES9[objectSource]);
});
});
});

describe('getTestInfo', () => {
Expand Down