Skip to content

Commit

Permalink
feat: add is-date-object
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Jul 17, 2024
1 parent 01d4f40 commit d59ff17
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 4 deletions.
9 changes: 6 additions & 3 deletions codemods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import isArrayBuffer from './is-array-buffer/index.js';

import isBooleanObject from './is-boolean-object/index.js';

import isDateObject from './is-date-object/index.js';

export const codemods = {
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-whitespace': isWhitespace,
'is-array-buffer': isArrayBuffer,
'is-boolean-object': isBooleanObject,
'is-date-object': isDateObject
};
67 changes: 67 additions & 0 deletions codemods/is-date-object/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import jscodeshift from 'jscodeshift';
import { removeImport } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'is-date-object',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

removeImport('is-date-object', root, j);

// Replace all calls to isDate with Object.prototype.toString.call
root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: 'isDate',
},
})
.replaceWith((path) => {
const arg = path.node.arguments[0];
return j.callExpression(
j.memberExpression(
j.memberExpression(
j.memberExpression(
j.identifier('Object'),
j.identifier('prototype'),
),
j.identifier('toString'),
),
j.identifier('call'),
),
[arg],
);
})
.forEach((path) => {
const parent = path.parent.node;
if (j.BinaryExpression.check(parent)) {
parent.operator = '===';
parent.right = j.literal('[object Date]');
} else if (
j.CallExpression.check(parent) &&
parent.arguments.length === 1
) {
const newExpression = j.binaryExpression(
'===',
path.node,
j.literal('[object Date]'),
);
parent.arguments[0] = newExpression;
}
});

return root.toSource({ quote: 'single' });
},
};
}
2 changes: 1 addition & 1 deletion scripts/scaffold-codemod.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fs.writeFileSync(
export default function(options) {
return {
name: '${name}',
transform: ({ file, jscodeshift }) => {
transform: ({ file }) => {
return '';
},
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/is-date-object/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var assert = require('assert');

assert.notOk(Object.prototype.toString.call(undefined) === '[object Date]');
assert.notOk(Object.prototype.toString.call(null) === '[object Date]');
assert.notOk(Object.prototype.toString.call(false) === '[object Date]');
assert.notOk(Object.prototype.toString.call(true) === '[object Date]');
assert.notOk(Object.prototype.toString.call(42) === '[object Date]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Date]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Date]');
assert.notOk(Object.prototype.toString.call([]) === '[object Date]');
assert.notOk(Object.prototype.toString.call({}) === '[object Date]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Date]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Date]');

assert.ok(Object.prototype.toString.call(new Date()) === '[object Date]');
16 changes: 16 additions & 0 deletions test/fixtures/is-date-object/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var assert = require('assert');
var isDate = require('is-date-object');

assert.notOk(isDate(undefined));
assert.notOk(isDate(null));
assert.notOk(isDate(false));
assert.notOk(isDate(true));
assert.notOk(isDate(42));
assert.notOk(isDate('foo'));
assert.notOk(isDate(function () {}));
assert.notOk(isDate([]));
assert.notOk(isDate({}));
assert.notOk(isDate(/a/g));
assert.notOk(isDate(new RegExp('a', 'g')));

assert.ok(isDate(new Date()));
15 changes: 15 additions & 0 deletions test/fixtures/is-date-object/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var assert = require('assert');

assert.notOk(Object.prototype.toString.call(undefined) === '[object Date]');
assert.notOk(Object.prototype.toString.call(null) === '[object Date]');
assert.notOk(Object.prototype.toString.call(false) === '[object Date]');
assert.notOk(Object.prototype.toString.call(true) === '[object Date]');
assert.notOk(Object.prototype.toString.call(42) === '[object Date]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Date]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Date]');
assert.notOk(Object.prototype.toString.call([]) === '[object Date]');
assert.notOk(Object.prototype.toString.call({}) === '[object Date]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Date]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Date]');

assert.ok(Object.prototype.toString.call(new Date()) === '[object Date]');
15 changes: 15 additions & 0 deletions test/fixtures/is-date-object/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';

assert.notOk(Object.prototype.toString.call(undefined) === '[object Date]');
assert.notOk(Object.prototype.toString.call(null) === '[object Date]');
assert.notOk(Object.prototype.toString.call(false) === '[object Date]');
assert.notOk(Object.prototype.toString.call(true) === '[object Date]');
assert.notOk(Object.prototype.toString.call(42) === '[object Date]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Date]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Date]');
assert.notOk(Object.prototype.toString.call([]) === '[object Date]');
assert.notOk(Object.prototype.toString.call({}) === '[object Date]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Date]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Date]');

assert.ok(Object.prototype.toString.call(new Date()) === '[object Date]');
16 changes: 16 additions & 0 deletions test/fixtures/is-date-object/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert from 'assert';
import isDate from 'is-date-object';

assert.notOk(isDate(undefined));
assert.notOk(isDate(null));
assert.notOk(isDate(false));
assert.notOk(isDate(true));
assert.notOk(isDate(42));
assert.notOk(isDate('foo'));
assert.notOk(isDate(function () {}));
assert.notOk(isDate([]));
assert.notOk(isDate({}));
assert.notOk(isDate(/a/g));
assert.notOk(isDate(new RegExp('a', 'g')));

assert.ok(isDate(new Date()));
15 changes: 15 additions & 0 deletions test/fixtures/is-date-object/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';

assert.notOk(Object.prototype.toString.call(undefined) === '[object Date]');
assert.notOk(Object.prototype.toString.call(null) === '[object Date]');
assert.notOk(Object.prototype.toString.call(false) === '[object Date]');
assert.notOk(Object.prototype.toString.call(true) === '[object Date]');
assert.notOk(Object.prototype.toString.call(42) === '[object Date]');
assert.notOk(Object.prototype.toString.call('foo') === '[object Date]');
assert.notOk(Object.prototype.toString.call(function () {}) === '[object Date]');
assert.notOk(Object.prototype.toString.call([]) === '[object Date]');
assert.notOk(Object.prototype.toString.call({}) === '[object Date]');
assert.notOk(Object.prototype.toString.call(/a/g) === '[object Date]');
assert.notOk(Object.prototype.toString.call(new RegExp('a', 'g')) === '[object Date]');

assert.ok(Object.prototype.toString.call(new Date()) === '[object Date]');

0 comments on commit d59ff17

Please sign in to comment.