diff --git a/scripts/internal-rules/fix-snapshot-test.js b/scripts/internal-rules/fix-snapshot-test.js index 6ee0ab006e..13bb419996 100644 --- a/scripts/internal-rules/fix-snapshot-test.js +++ b/scripts/internal-rules/fix-snapshot-test.js @@ -15,7 +15,7 @@ const messages = { }; // Top-level `test.snapshot({invalid: []})` -const selector = [ +const snapshotTestCallSelector = [ 'Program > ExpressionStatement.body > .expression', // `test.snapshot()` methodCallSelector({ @@ -23,6 +23,10 @@ const selector = [ object: 'test', method: 'snapshot', }), +].join(''); + +const propertySelector = [ + snapshotTestCallSelector, ' > ObjectExpression.arguments:first-child', /* ``` @@ -53,8 +57,7 @@ function * removeObjectProperty(node, fixer, sourceCode) { } // The fix deletes lots of code, disabled auto-fix by default, unless `/* fix */ test.snapshot()` pattern is used. -function hasFixMarkComment(propertyNode, sourceCode) { - const snapshotTestCall = propertyNode.parent.parent.parent.parent.parent; +function getFixMarkComment(snapshotTestCall, sourceCode) { assert.ok(snapshotTestCall.type === 'CallExpression'); const comment = sourceCode.getTokenBefore(snapshotTestCall, {includeComments: true}); @@ -66,7 +69,7 @@ function hasFixMarkComment(propertyNode, sourceCode) { || comment.loc.start.line === snapshotTestCall.loc.start.line - 1 ) ) { - return true; + return comment; } } @@ -75,14 +78,29 @@ module.exports = { const sourceCode = context.getSourceCode(); return { - [selector](propertyNode) { + [snapshotTestCallSelector](snapshotTestCall) { + const comment = getFixMarkComment(snapshotTestCall, sourceCode); + + if (!comment) { + return; + } + + context.report({ + node: comment, + messageId: MESSAGE_ID_REMOVE_FIX_MARK_COMMENT, + }); + }, + [propertySelector](propertyNode) { const {key} = propertyNode; switch (key.name) { case 'errors': case 'output': { const canFix = sourceCode.getCommentsInside(propertyNode).length === 0; - const hasFixMark = hasFixMarkComment(propertyNode, sourceCode); + const hasFixMark = Boolean(getFixMarkComment( + propertyNode.parent.parent.parent.parent.parent, + sourceCode, + )); context.report({ node: key, diff --git a/test/new-for-builtins.mjs b/test/new-for-builtins.mjs index 8d688b847d..6ebc585371 100644 --- a/test/new-for-builtins.mjs +++ b/test/new-for-builtins.mjs @@ -5,15 +5,7 @@ import {getTester} from './utils/test.mjs'; const {test} = getTester(import.meta); -const enforceNewError = builtin => ({ - message: `Use \`new ${builtin}()\` instead of \`${builtin}()\`.`, -}); - -const disallowNewError = builtin => ({ - message: `Use \`${builtin}()\` instead of \`new ${builtin}()\`.`, -}); - -test({ +test.snapshot({ valid: [ 'const foo = new Object()', 'const foo = new Array()', @@ -106,214 +98,7 @@ test({ 'const isObject = v => Object(v) === v;', 'const isObject = v => globalThis.Object(v) === v;', '(x) !== Object(x)', - ], - invalid: [ - { - code: 'const foo = Object()', - errors: [enforceNewError('Object')], - output: 'const foo = new Object()', - }, - { - code: 'const foo = Array()', - errors: [enforceNewError('Array')], - output: 'const foo = new Array()', - }, - { - code: 'const foo = ArrayBuffer()', - errors: [enforceNewError('ArrayBuffer')], - output: 'const foo = new ArrayBuffer()', - }, - { - code: 'const foo = BigInt64Array()', - errors: [enforceNewError('BigInt64Array')], - output: 'const foo = new BigInt64Array()', - }, - { - code: 'const foo = BigUint64Array()', - errors: [enforceNewError('BigUint64Array')], - output: 'const foo = new BigUint64Array()', - }, - { - code: 'const foo = DataView()', - errors: [enforceNewError('DataView')], - output: 'const foo = new DataView()', - }, - { - code: 'const foo = Date()', - errors: [enforceNewError('Date')], - output: 'const foo = new Date()', - }, - { - code: 'const foo = Error()', - errors: [enforceNewError('Error')], - output: 'const foo = new Error()', - }, - { - code: 'const foo = Error(\'Foo bar\')', - errors: [enforceNewError('Error')], - output: 'const foo = new Error(\'Foo bar\')', - }, - { - code: 'const foo = Float32Array()', - errors: [enforceNewError('Float32Array')], - output: 'const foo = new Float32Array()', - }, - { - code: 'const foo = Float64Array()', - errors: [enforceNewError('Float64Array')], - output: 'const foo = new Float64Array()', - }, - { - code: 'const foo = Function()', - errors: [enforceNewError('Function')], - output: 'const foo = new Function()', - }, - { - code: 'const foo = Int8Array()', - errors: [enforceNewError('Int8Array')], - output: 'const foo = new Int8Array()', - }, - { - code: 'const foo = Int16Array()', - errors: [enforceNewError('Int16Array')], - output: 'const foo = new Int16Array()', - }, - { - code: 'const foo = Int32Array()', - errors: [enforceNewError('Int32Array')], - output: 'const foo = new Int32Array()', - }, - { - code: 'const foo = (( Map ))()', - errors: [enforceNewError('Map')], - output: 'const foo = new (( Map ))()', - }, - { - code: 'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])', - errors: [enforceNewError('Map')], - output: 'const foo = new Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])', - }, - { - code: 'const foo = WeakMap()', - errors: [enforceNewError('WeakMap')], - output: 'const foo = new WeakMap()', - }, - { - code: 'const foo = Set()', - errors: [enforceNewError('Set')], - output: 'const foo = new Set()', - }, - { - code: 'const foo = WeakSet()', - errors: [enforceNewError('WeakSet')], - output: 'const foo = new WeakSet()', - }, - { - code: 'const foo = Promise()', - errors: [enforceNewError('Promise')], - output: 'const foo = new Promise()', - }, - { - code: 'const foo = RegExp()', - errors: [enforceNewError('RegExp')], - output: 'const foo = new RegExp()', - }, - { - code: 'const foo = Uint8Array()', - errors: [enforceNewError('Uint8Array')], - output: 'const foo = new Uint8Array()', - }, - { - code: 'const foo = Uint16Array()', - errors: [enforceNewError('Uint16Array')], - output: 'const foo = new Uint16Array()', - }, - { - code: 'const foo = Uint32Array()', - errors: [enforceNewError('Uint32Array')], - output: 'const foo = new Uint32Array()', - }, - { - code: 'const foo = Uint8ClampedArray()', - errors: [enforceNewError('Uint8ClampedArray')], - output: 'const foo = new Uint8ClampedArray()', - }, - { - code: 'const foo = new BigInt(123)', - errors: [disallowNewError('BigInt')], - output: 'const foo = BigInt(123)', - }, - { - code: 'const foo = new Boolean()', - errors: [disallowNewError('Boolean')], - }, - { - code: 'const foo = new Number()', - errors: [disallowNewError('Number')], - }, - { - code: 'const foo = new Number(\'123\')', - errors: [disallowNewError('Number')], - }, - { - code: 'const foo = new String()', - errors: [disallowNewError('String')], - }, - { - code: 'const foo = new Symbol()', - errors: [disallowNewError('Symbol')], - output: 'const foo = Symbol()', - }, - { - code: ` - function varCheck() { - { - var WeakMap = function() {}; - } - // This should not reported - return WeakMap() - } - function constCheck() { - { - const Array = function() {}; - } - return Array() - } - function letCheck() { - { - let Map = function() {}; - } - return Map() - } - `, - errors: [enforceNewError('Array'), enforceNewError('Map')], - output: ` - function varCheck() { - { - var WeakMap = function() {}; - } - // This should not reported - return WeakMap() - } - function constCheck() { - { - const Array = function() {}; - } - return new Array() - } - function letCheck() { - { - let Map = function() {}; - } - return new Map() - } - `, - }, - ], -}); -test.snapshot({ - valid: [ { code: 'new Symbol("")', globals: {Symbol: 'off'}, @@ -414,5 +199,58 @@ test.snapshot({ `, globals: {Array: 'off'}, }, + 'const foo = Object()', + 'const foo = Array()', + 'const foo = ArrayBuffer()', + 'const foo = BigInt64Array()', + 'const foo = BigUint64Array()', + 'const foo = DataView()', + 'const foo = Date()', + 'const foo = Error()', + 'const foo = Error(\'Foo bar\')', + 'const foo = Float32Array()', + 'const foo = Float64Array()', + 'const foo = Function()', + 'const foo = Int8Array()', + 'const foo = Int16Array()', + 'const foo = Int32Array()', + 'const foo = (( Map ))()', + 'const foo = Map([[\'foo\', \'bar\'], [\'unicorn\', \'rainbow\']])', + 'const foo = WeakMap()', + 'const foo = Set()', + 'const foo = WeakSet()', + 'const foo = Promise()', + 'const foo = RegExp()', + 'const foo = Uint8Array()', + 'const foo = Uint16Array()', + 'const foo = Uint32Array()', + 'const foo = Uint8ClampedArray()', + 'const foo = new BigInt(123)', + 'const foo = new Boolean()', + 'const foo = new Number()', + 'const foo = new Number(\'123\')', + 'const foo = new String()', + 'const foo = new Symbol()', + ` + function varCheck() { + { + var WeakMap = function() {}; + } + // This should not reported + return WeakMap() + } + function constCheck() { + { + const Array = function() {}; + } + return Array() + } + function letCheck() { + { + let Map = function() {}; + } + return Map() + } + `, ], }); diff --git a/test/no-lonely-if.mjs b/test/no-lonely-if.mjs index 3ac8a8a953..2ba5d52a4c 100644 --- a/test/no-lonely-if.mjs +++ b/test/no-lonely-if.mjs @@ -3,7 +3,7 @@ import {getTester} from './utils/test.mjs'; const {test} = getTester(import.meta); -test({ +test.snapshot({ valid: [ outdent` if (a) { @@ -30,11 +30,6 @@ test({ } `, ], - invalid: [], -}); - -test.snapshot({ - valid: [], invalid: [ outdent` if (a) { diff --git a/test/snapshots/new-for-builtins.mjs.md b/test/snapshots/new-for-builtins.mjs.md index 8e89fc677e..de1910e561 100644 --- a/test/snapshots/new-for-builtins.mjs.md +++ b/test/snapshots/new-for-builtins.mjs.md @@ -483,3 +483,594 @@ Generated by [AVA](https://avajs.dev). > 2 | Array();␊ | ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊ ` + +## Invalid #27 + 1 | const foo = Object() + +> Output + + `␊ + 1 | const foo = new Object()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Object()␊ + | ^^^^^^^^ Use \`new Object()\` instead of \`Object()\`.␊ + ` + +## Invalid #28 + 1 | const foo = Array() + +> Output + + `␊ + 1 | const foo = new Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Array()␊ + | ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊ + ` + +## Invalid #29 + 1 | const foo = ArrayBuffer() + +> Output + + `␊ + 1 | const foo = new ArrayBuffer()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = ArrayBuffer()␊ + | ^^^^^^^^^^^^^ Use \`new ArrayBuffer()\` instead of \`ArrayBuffer()\`.␊ + ` + +## Invalid #30 + 1 | const foo = BigInt64Array() + +> Output + + `␊ + 1 | const foo = new BigInt64Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = BigInt64Array()␊ + | ^^^^^^^^^^^^^^^ Use \`new BigInt64Array()\` instead of \`BigInt64Array()\`.␊ + ` + +## Invalid #31 + 1 | const foo = BigUint64Array() + +> Output + + `␊ + 1 | const foo = new BigUint64Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = BigUint64Array()␊ + | ^^^^^^^^^^^^^^^^ Use \`new BigUint64Array()\` instead of \`BigUint64Array()\`.␊ + ` + +## Invalid #32 + 1 | const foo = DataView() + +> Output + + `␊ + 1 | const foo = new DataView()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = DataView()␊ + | ^^^^^^^^^^ Use \`new DataView()\` instead of \`DataView()\`.␊ + ` + +## Invalid #33 + 1 | const foo = Date() + +> Output + + `␊ + 1 | const foo = new Date()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Date()␊ + | ^^^^^^ Use \`new Date()\` instead of \`Date()\`.␊ + ` + +## Invalid #34 + 1 | const foo = Error() + +> Output + + `␊ + 1 | const foo = new Error()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Error()␊ + | ^^^^^^^ Use \`new Error()\` instead of \`Error()\`.␊ + ` + +## Invalid #35 + 1 | const foo = Error('Foo bar') + +> Output + + `␊ + 1 | const foo = new Error('Foo bar')␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Error('Foo bar')␊ + | ^^^^^^^^^^^^^^^^ Use \`new Error()\` instead of \`Error()\`.␊ + ` + +## Invalid #36 + 1 | const foo = Float32Array() + +> Output + + `␊ + 1 | const foo = new Float32Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Float32Array()␊ + | ^^^^^^^^^^^^^^ Use \`new Float32Array()\` instead of \`Float32Array()\`.␊ + ` + +## Invalid #37 + 1 | const foo = Float64Array() + +> Output + + `␊ + 1 | const foo = new Float64Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Float64Array()␊ + | ^^^^^^^^^^^^^^ Use \`new Float64Array()\` instead of \`Float64Array()\`.␊ + ` + +## Invalid #38 + 1 | const foo = Function() + +> Output + + `␊ + 1 | const foo = new Function()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Function()␊ + | ^^^^^^^^^^ Use \`new Function()\` instead of \`Function()\`.␊ + ` + +## Invalid #39 + 1 | const foo = Int8Array() + +> Output + + `␊ + 1 | const foo = new Int8Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Int8Array()␊ + | ^^^^^^^^^^^ Use \`new Int8Array()\` instead of \`Int8Array()\`.␊ + ` + +## Invalid #40 + 1 | const foo = Int16Array() + +> Output + + `␊ + 1 | const foo = new Int16Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Int16Array()␊ + | ^^^^^^^^^^^^ Use \`new Int16Array()\` instead of \`Int16Array()\`.␊ + ` + +## Invalid #41 + 1 | const foo = Int32Array() + +> Output + + `␊ + 1 | const foo = new Int32Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Int32Array()␊ + | ^^^^^^^^^^^^ Use \`new Int32Array()\` instead of \`Int32Array()\`.␊ + ` + +## Invalid #42 + 1 | const foo = (( Map ))() + +> Output + + `␊ + 1 | const foo = new (( Map ))()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = (( Map ))()␊ + | ^^^^^^^^^^^ Use \`new Map()\` instead of \`Map()\`.␊ + ` + +## Invalid #43 + 1 | const foo = Map([['foo', 'bar'], ['unicorn', 'rainbow']]) + +> Output + + `␊ + 1 | const foo = new Map([['foo', 'bar'], ['unicorn', 'rainbow']])␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Map([['foo', 'bar'], ['unicorn', 'rainbow']])␊ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`new Map()\` instead of \`Map()\`.␊ + ` + +## Invalid #44 + 1 | const foo = WeakMap() + +> Output + + `␊ + 1 | const foo = new WeakMap()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = WeakMap()␊ + | ^^^^^^^^^ Use \`new WeakMap()\` instead of \`WeakMap()\`.␊ + ` + +## Invalid #45 + 1 | const foo = Set() + +> Output + + `␊ + 1 | const foo = new Set()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Set()␊ + | ^^^^^ Use \`new Set()\` instead of \`Set()\`.␊ + ` + +## Invalid #46 + 1 | const foo = WeakSet() + +> Output + + `␊ + 1 | const foo = new WeakSet()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = WeakSet()␊ + | ^^^^^^^^^ Use \`new WeakSet()\` instead of \`WeakSet()\`.␊ + ` + +## Invalid #47 + 1 | const foo = Promise() + +> Output + + `␊ + 1 | const foo = new Promise()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Promise()␊ + | ^^^^^^^^^ Use \`new Promise()\` instead of \`Promise()\`.␊ + ` + +## Invalid #48 + 1 | const foo = RegExp() + +> Output + + `␊ + 1 | const foo = new RegExp()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = RegExp()␊ + | ^^^^^^^^ Use \`new RegExp()\` instead of \`RegExp()\`.␊ + ` + +## Invalid #49 + 1 | const foo = Uint8Array() + +> Output + + `␊ + 1 | const foo = new Uint8Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Uint8Array()␊ + | ^^^^^^^^^^^^ Use \`new Uint8Array()\` instead of \`Uint8Array()\`.␊ + ` + +## Invalid #50 + 1 | const foo = Uint16Array() + +> Output + + `␊ + 1 | const foo = new Uint16Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Uint16Array()␊ + | ^^^^^^^^^^^^^ Use \`new Uint16Array()\` instead of \`Uint16Array()\`.␊ + ` + +## Invalid #51 + 1 | const foo = Uint32Array() + +> Output + + `␊ + 1 | const foo = new Uint32Array()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Uint32Array()␊ + | ^^^^^^^^^^^^^ Use \`new Uint32Array()\` instead of \`Uint32Array()\`.␊ + ` + +## Invalid #52 + 1 | const foo = Uint8ClampedArray() + +> Output + + `␊ + 1 | const foo = new Uint8ClampedArray()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = Uint8ClampedArray()␊ + | ^^^^^^^^^^^^^^^^^^^ Use \`new Uint8ClampedArray()\` instead of \`Uint8ClampedArray()\`.␊ + ` + +## Invalid #53 + 1 | const foo = new BigInt(123) + +> Output + + `␊ + 1 | const foo = BigInt(123)␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = new BigInt(123)␊ + | ^^^^^^^^^^^^^^^ Use \`BigInt()\` instead of \`new BigInt()\`.␊ + ` + +## Invalid #54 + 1 | const foo = new Boolean() + +> Error 1/1 + + `␊ + > 1 | const foo = new Boolean()␊ + | ^^^^^^^^^^^^^ Use \`Boolean()\` instead of \`new Boolean()\`.␊ + ` + +## Invalid #55 + 1 | const foo = new Number() + +> Error 1/1 + + `␊ + > 1 | const foo = new Number()␊ + | ^^^^^^^^^^^^ Use \`Number()\` instead of \`new Number()\`.␊ + ` + +## Invalid #56 + 1 | const foo = new Number('123') + +> Error 1/1 + + `␊ + > 1 | const foo = new Number('123')␊ + | ^^^^^^^^^^^^^^^^^ Use \`Number()\` instead of \`new Number()\`.␊ + ` + +## Invalid #57 + 1 | const foo = new String() + +> Error 1/1 + + `␊ + > 1 | const foo = new String()␊ + | ^^^^^^^^^^^^ Use \`String()\` instead of \`new String()\`.␊ + ` + +## Invalid #58 + 1 | const foo = new Symbol() + +> Output + + `␊ + 1 | const foo = Symbol()␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = new Symbol()␊ + | ^^^^^^^^^^^^ Use \`Symbol()\` instead of \`new Symbol()\`.␊ + ` + +## Invalid #59 + 1 | + 2 | function varCheck() { + 3 | { + 4 | var WeakMap = function() {}; + 5 | } + 6 | // This should not reported + 7 | return WeakMap() + 8 | } + 9 | function constCheck() { + 10 | { + 11 | const Array = function() {}; + 12 | } + 13 | return Array() + 14 | } + 15 | function letCheck() { + 16 | { + 17 | let Map = function() {}; + 18 | } + 19 | return Map() + 20 | } + 21 | + +> Output + + `␊ + 1 |␊ + 2 | function varCheck() {␊ + 3 | {␊ + 4 | var WeakMap = function() {};␊ + 5 | }␊ + 6 | // This should not reported␊ + 7 | return WeakMap()␊ + 8 | }␊ + 9 | function constCheck() {␊ + 10 | {␊ + 11 | const Array = function() {};␊ + 12 | }␊ + 13 | return new Array()␊ + 14 | }␊ + 15 | function letCheck() {␊ + 16 | {␊ + 17 | let Map = function() {};␊ + 18 | }␊ + 19 | return new Map()␊ + 20 | }␊ + 21 | ␊ + ` + +> Error 1/2 + + `␊ + 1 |␊ + 2 | function varCheck() {␊ + 3 | {␊ + 4 | var WeakMap = function() {};␊ + 5 | }␊ + 6 | // This should not reported␊ + 7 | return WeakMap()␊ + 8 | }␊ + 9 | function constCheck() {␊ + 10 | {␊ + 11 | const Array = function() {};␊ + 12 | }␊ + > 13 | return Array()␊ + | ^^^^^^^ Use \`new Array()\` instead of \`Array()\`.␊ + 14 | }␊ + 15 | function letCheck() {␊ + 16 | {␊ + 17 | let Map = function() {};␊ + 18 | }␊ + 19 | return Map()␊ + 20 | }␊ + 21 | ␊ + ` + +> Error 2/2 + + `␊ + 1 |␊ + 2 | function varCheck() {␊ + 3 | {␊ + 4 | var WeakMap = function() {};␊ + 5 | }␊ + 6 | // This should not reported␊ + 7 | return WeakMap()␊ + 8 | }␊ + 9 | function constCheck() {␊ + 10 | {␊ + 11 | const Array = function() {};␊ + 12 | }␊ + 13 | return Array()␊ + 14 | }␊ + 15 | function letCheck() {␊ + 16 | {␊ + 17 | let Map = function() {};␊ + 18 | }␊ + > 19 | return Map()␊ + | ^^^^^ Use \`new Map()\` instead of \`Map()\`.␊ + 20 | }␊ + 21 | ␊ + ` diff --git a/test/snapshots/new-for-builtins.mjs.snap b/test/snapshots/new-for-builtins.mjs.snap index dd23a6f233..bfce5e6bd9 100644 Binary files a/test/snapshots/new-for-builtins.mjs.snap and b/test/snapshots/new-for-builtins.mjs.snap differ