mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
tools: require value before done in iterator results
V8 has a fast path for `{ value, done }` literals, but it must be that
order.
Also, the ES spec technically requires that order.
Assisted-by: claude:opus-5.5
Signed-off-by: Kevin Gibbons <bakkot@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66837
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
This commit is contained in:
1 parent
81d2321054
commit
1686e8a99f
4 files changed
+68
-64
No files matched your search
@@ -435,7 +435,7 @@ export default [
|
||||
'node-core/alphabetize-errors': 'error',
|
||||
'node-core/alphabetize-primordials': 'error',
|
||||
'node-core/avoid-prototype-pollution': 'error',
|
||||
'node-core/iterator-result-done-first': 'error',
|
||||
'node-core/iterator-result-value-first': 'error',
|
||||
'node-core/lowercase-name-for-primitive': 'error',
|
||||
'node-core/non-ascii-character': 'error',
|
||||
'node-core/no-array-destructuring': 'error',
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if ((!common.hasCrypto) || (!common.hasIntl)) {
|
||||
common.skip('ESLint tests require crypto and Intl');
|
||||
}
|
||||
|
||||
common.skipIfEslintMissing();
|
||||
|
||||
const { RuleTester } = require('../../tools/eslint/node_modules/eslint');
|
||||
const rule = require('../../tools/eslint-rules/iterator-result-done-first');
|
||||
|
||||
const message = 'Iterator result objects should place `done` before `value`.';
|
||||
|
||||
new RuleTester().run('iterator-result-done-first', rule, {
|
||||
valid: [
|
||||
'function next() { return { done: true, value: undefined }; }',
|
||||
'function next() { return { __proto__: null, done: false, value: chunk }; }',
|
||||
'function next() { return { done, value }; }',
|
||||
'function next() { return { value }; }',
|
||||
'function next() { return { done }; }',
|
||||
'function next() { return { value: 1, other: 2 }; }',
|
||||
'function next() { return { [value]: 1, done: true }; }',
|
||||
'function next() { return { value: 1, [done]: true }; }',
|
||||
'function next() { return { "done": true, "value": undefined }; }',
|
||||
'function next() { return { ["done"]: true, ["value"]: undefined }; }',
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: 'function next() { return { value: undefined, done: true }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { done: true, value: undefined }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { __proto__: null, value: chunk, done: false }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { __proto__: null, done: false, value: chunk }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { value, done }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { done, value }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { "value": undefined, "done": true }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { "done": true, "value": undefined }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { ["value"]: undefined, ["done"]: true }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { ["done"]: true, ["value"]: undefined }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { value: result, extra: true, done: false }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { done: false, extra: true, value: result }; }',
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if ((!common.hasCrypto) || (!common.hasIntl)) {
|
||||
common.skip('ESLint tests require crypto and Intl');
|
||||
}
|
||||
|
||||
common.skipIfEslintMissing();
|
||||
|
||||
const { RuleTester } = require('../../tools/eslint/node_modules/eslint');
|
||||
const rule = require('../../tools/eslint-rules/iterator-result-value-first');
|
||||
|
||||
const message = 'Iterator result objects should place `value` before `done`.';
|
||||
|
||||
new RuleTester().run('iterator-result-value-first', rule, {
|
||||
valid: [
|
||||
'function next() { return { value: undefined, done: true }; }',
|
||||
'function next() { return { __proto__: null, value: chunk, done: false }; }',
|
||||
'function next() { return { value, done }; }',
|
||||
'function next() { return { value }; }',
|
||||
'function next() { return { done }; }',
|
||||
'function next() { return { done: 1, other: 2 }; }',
|
||||
'function next() { return { [done]: 1, value: true }; }',
|
||||
'function next() { return { done: 1, [value]: true }; }',
|
||||
'function next() { return { "value": undefined, "done": true }; }',
|
||||
'function next() { return { ["value"]: undefined, ["done"]: true }; }',
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: 'function next() { return { done: true, value: undefined }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { value: undefined, done: true }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { __proto__: null, done: false, value: chunk }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { __proto__: null, value: chunk, done: false }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { done, value }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { value, done }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { "done": true, "value": undefined }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { "value": undefined, "done": true }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { ["done"]: true, ["value"]: undefined }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { ["value"]: undefined, ["done"]: true }; }',
|
||||
},
|
||||
{
|
||||
code: 'function next() { return { done: false, extra: true, value: result }; }',
|
||||
errors: [{ message }],
|
||||
output: 'function next() { return { value: result, extra: true, done: false }; }',
|
||||
},
|
||||
],
|
||||
});
|
||||
+7
-3
@@ -1,6 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const MESSAGE = 'Iterator result objects should place `done` before `value`.';
|
||||
// V8 has a fast path for `{ value, done }` literals, but it must be that order.
|
||||
// https://source.chromium.org/chromium/chromium/src/+/main:v8/src/init/bootstrapper.cc;l=746-754;drc=e8ac4914ee6b24442aed773d3a041451692b6a9c
|
||||
// Also, the ES spec technically requires that order.
|
||||
// https://tc39.es/ecma262/multipage/abstract-operations.html#sec-createiterresultobject
|
||||
const MESSAGE = 'Iterator result objects should place `value` before `done`.';
|
||||
|
||||
function getStaticPropertyName(property) {
|
||||
const { key } = property;
|
||||
@@ -48,9 +52,9 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
if (doneProperty && valueProperty && valueProperty.range[0] < doneProperty.range[0]) {
|
||||
if (doneProperty && valueProperty && doneProperty.range[0] < valueProperty.range[0]) {
|
||||
context.report({
|
||||
node: valueProperty,
|
||||
node: doneProperty,
|
||||
message: MESSAGE,
|
||||
fix(fixer) {
|
||||
return [
|
||||
Reference in new issue
Block a user