<commit_before>import test from 'ava-spec'
import {transform} from 'babel-core'

import plugin from '../lib/index'

const options = {
  presets: [
    'es2015'
  ],
  plugins: [
    plugin
  ]
}

const helper = 'var _isNilWrapper = function _isNilWrapper(val) { return val === null || typeof val === \'undefined\'; };'

const specs = [
  {
    description: 'expect isNil replace to `=== null || === void 0`',
    before: 'hoge.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(hoge);`
  },
  {
    description: 'expect ! isNil replace to `! (=== null || === void 0)`',
    before: '!hoge.isNil',
    after: `"use strict";\n\n${helper}\n\n!_isNilWrapper(hoge);`
  },
  {
    description: 'expect isNil() dont replace',
    before: 'R.isNil()',
    after: '"use strict";\n\nR.isNil();'
  },
  {
    description: 'function call test 1',
    before: 'foo.bar().isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar());`
  },
  {
    description: 'function call test 2',
    before: 'foo.bar(hoge).isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar(hoge));`
  },
  {
    description: 'Array test 1',
    before: 'foo[0].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo[0]);`
  },
  {
    description: 'Array test 2',
    before: 'foo.bar["hoge"].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar["hoge"]);`
  },
  {
    description: 'Array test 3',
    before: 'bar[hoge].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(bar[hoge]);`
  },
  {
    description: 'Complex test 1',
    before: 'foo.bar.hoge("poge")[1].bar.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar.hoge("poge")[1].bar);`
  },
  {
    description: 'Complex test 2',
    before: 'foo.bar["hoge"]["poge"].foo[bar][2].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar["hoge"]["poge"].foo[bar][2]);`
  },
  {
    description: 'Complex test 3',
    before: '(hoge.poge || { }).foo.bar["hoge"][3].poge.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper((hoge.poge || { }).foo.bar[3]["hoge"].poge);`
  }
]

specs.forEach(spec => {
  test(spec.description, it => {
    const result = transform(spec.before, options)
    it.is(result.code, spec.after)
  })
})
<commit_msg>Added eval tests<commit_after>import vm from 'vm'
import test from 'ava-spec'
import {transform} from 'babel-core'

import plugin from '../lib/index'

const options = {
  presets: [
    'es2015'
  ],
  plugins: [
    plugin
  ]
}

const helper = 'var _isNilWrapper = function _isNilWrapper(val) { return val === null || typeof val === \'undefined\'; };'

const transformSpecs = [
  {
    description: 'expect isNil replace to `=== null || === void 0`',
    before: 'hoge.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(hoge);`
  },
  {
    description: 'expect ! isNil replace to `! (=== null || === void 0)`',
    before: '!hoge.isNil',
    after: `"use strict";\n\n${helper}\n\n!_isNilWrapper(hoge);`
  },
  {
    description: 'expect isNil() dont replace',
    before: 'R.isNil()',
    after: '"use strict";\n\nR.isNil();'
  },
  {
    description: 'function call test 1',
    before: 'foo.bar().isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar());`
  },
  {
    description: 'function call test 2',
    before: 'foo.bar(hoge).isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar(hoge));`
  },
  {
    description: 'Array test 1',
    before: 'foo[0].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo[0]);`
  },
  {
    description: 'Array test 2',
    before: 'foo.bar["hoge"].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar["hoge"]);`
  },
  {
    description: 'Array test 3',
    before: 'bar[hoge].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(bar[hoge]);`
  },
  {
    description: 'Complex test 1',
    before: 'foo.bar.hoge("poge")[1].bar.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar.hoge("poge")[1].bar);`
  },
  {
    description: 'Complex test 2',
    before: 'foo.bar["hoge"]["poge"].foo[bar][2].isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar["hoge"]["poge"].foo[bar][2]);`
  },
  {
    description: 'Complex test 3',
    before: '(hoge.poge || { }).foo.bar["hoge"][3].poge.isNil',
    after: `"use strict";\n\n${helper}\n\n_isNilWrapper((hoge.poge || { }).foo.bar[3]["hoge"].poge);`
  }
]

transformSpecs.forEach(spec => {
  test(`Transform: ${spec.description}`, it => {
    const result = transform(spec.before, options)
    it.is(result.code, spec.after)
  })
})

const evalSpecs = [
  {
    description: 'expect "null" to be true',
    code: 'null.isNil',
    result: true
  }, {
    description: 'expect "undefined" to be true',
    code: 'undefined.isNil',
    result: true
  }, {
    description: 'expect "{ }" to be false',
    code: '({ }).isNil',
    result: false
  }, {
    description: 'expect empty string to be false',
    code: '"".isNil',
    result: false
  }, {
    description: 'expect "0" to be false',
    code: '(0).isNil',
    result: false
  }
]

evalSpecs.forEach(spec => {
  test(`Eval: ${spec.description}`, it => {
    const transformed = transform(spec.code, options)
    it.is(vm.runInNewContext(transformed.code), spec.result)
  })
})
{'fraction_non_alphanumeric': 0.21889616463985032, 'fraction_numerical': 0.008606173994387277, 'mean_word_length': 3.4036243822075782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894984', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2047, 'n_words': 375}