Skip to content
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

chore: switch from tap to node:test and Borp #39

Merged
merged 3 commits into from
Mar 12, 2024
Merged

Commits on Feb 28, 2024

  1. chore: switch from tap to node:test and Borp

    To do this, I:
    
    - Uninstalled `tap` and replaced it with `borp`
    - Wrote a [jscodeshift] codemod (below)
    - Updated `instanbul` references to `c8`, which Borp uses internally
    - Dropped Node 16 support, which `node:test` requires
    - Made a few small manual tweaks
    
    Here's the jscodeshift transformer:
    
    ```typescript
    import type {
      Transform,
      Expression,
      FunctionExpression,
      ArrowFunctionExpression,
    } from 'jscodeshift'
    
    const isFunctionExpression = (
      expression: Readonly<Expression>
    ): expression is FunctionExpression | ArrowFunctionExpression =>
      expression.type === 'FunctionExpression' ||
      expression.type === 'ArrowFunctionExpression'
    
    const transform: Transform = (fileInfo, api) => {
      const { j } = api
      const root = j(fileInfo.source)
    
      // Update imports
    
      {
        const commonJsImport = (varName: string, moduleName: string) =>
          j.variableDeclaration('const', [
            j.variableDeclarator(
              j.identifier(varName),
              j.callExpression(j.identifier('require'), [
                j.stringLiteral(moduleName),
              ])
            ),
          ])
    
        const tapImport = root.find(j.VariableDeclaration, {
          declarations: [
            {
              init: {
                callee: { name: 'require' },
                arguments: [
                  {
                    value: 'tap',
                  },
                ],
              },
            },
          ],
        })
    
        tapImport.insertBefore(commonJsImport('test', 'node:test'))
        tapImport.insertBefore(commonJsImport('assert', 'node:assert/strict'))
    
        tapImport.remove()
      }
    
      // Drop args from test callback
    
      root
        .find(j.CallExpression, { callee: { name: 'test' } })
        .forEach((callExpression) => {
          for (const arg of callExpression.value.arguments) {
            if (!isFunctionExpression(arg)) continue
            arg.params = []
          }
        })
    
      // Drop t.pass
    
      root
        .find(j.ExpressionStatement, {
          expression: {
            callee: {
              type: 'MemberExpression',
              object: { name: 't' },
              property: { name: 'pass' },
            },
          },
        })
        .remove()
    
      // Simple replacements of various calls
    
      {
        const toReplace: Map<string, string> = new Map([
          ['comment', 'console.log'],
          ['equal', 'assert.equal'],
          ['fail', 'assert.fail'],
          ['not', 'assert.notEqual'],
          ['ok', 'assert.ok'],
          ['same', 'assert.deepEqual'],
        ])
        for (const [tMethod, memberExpressionString] of toReplace) {
          const [newObj, newProp] = memberExpressionString.split('.')
          root
            .find(j.MemberExpression, {
              object: { name: 't' },
              property: { name: tMethod },
            })
            .forEach((memberExpression) => {
              memberExpression.value.object = j.identifier(newObj)
              memberExpression.value.property = j.identifier(newProp)
            })
        }
      }
    
      // Replace `t.notOk(x)` with `assert.equal(x, false)`.
      // Doesn't always work, but works how we use it.
    
      root
        .find(j.CallExpression, {
          callee: {
            type: 'MemberExpression',
            object: { name: 't' },
            property: { name: 'notOk' },
          },
        })
        .forEach((callExpression) => {
          callExpression.value.callee = j.memberExpression(
            j.identifier('assert'),
            j.identifier('equal')
          )
          callExpression.value.arguments = [
            callExpression.value.arguments[0],
            j.booleanLiteral(false),
            ...callExpression.value.arguments.slice(1),
          ]
        })
    
      // All done!
    
      return root.toSource()
    }
    
    export default transform
    ```
    
    [jscodeshift]: https://github.com/facebook/jscodeshift
    EvanHahn committed Feb 28, 2024
    Configuration menu
    Copy the full SHA
    82b2869 View commit details
    Browse the repository at this point in the history

Commits on Mar 2, 2024

  1. Configuration menu
    Copy the full SHA
    19a5ac2 View commit details
    Browse the repository at this point in the history
  2. Test unlink before open

    EvanHahn committed Mar 2, 2024
    Configuration menu
    Copy the full SHA
    c275c9b View commit details
    Browse the repository at this point in the history