Skip to content

Commit

Permalink
fix: fixes test for hash-bang
Browse files Browse the repository at this point in the history
  • Loading branch information
yowainwright committed Jan 11, 2021
1 parent 83aaae9 commit 4269ee0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 46 deletions.
67 changes: 25 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ prog
.action((args, options, logger) => {
const configFilePath = path.resolve(process.cwd(), '.escheckrc')

let e // The ecmaVersion

/**
* @note Check for a configuration file.
* @note
* Check for a configuration file.
* - If one exists, default to those options
* - If no command line arguments are passed in
* - Ff no command line arguments are passed in
*/
const config = fs.existsSync(configFilePath) ? JSON.parse(fs.readFileSync(configFilePath)) : {}
const inputEcmaVersion = args.ecmaVersion ? args.ecmaVersion : config.ecmaVersion
const v = args.ecmaVersion ? args.ecmaVersion : config.ecmaVersion
const files = args.files.length ? args.files : [].concat(config.files)
const esmodule = options.module ? options.module : config.module
const allowHashBang = options.allowHashBang ? options.allowHashBang : config.allowHashBang
const pathsToIgnore = options.not ? options.not : config.not

if (!inputEcmaVersion) {
if (!v) {
logger.error(
'No ecmaScript version passed in or found in .escheckrc. Please set your ecmaScript version in the CLI or in .escheckrc',
)
Expand All @@ -58,61 +61,47 @@ prog
}

/**
* define ecmaScript version
* @note currently latest is not defined
* @note define ecmaScript version
*/
let ecmaVersion
switch (inputEcmaVersion) {
switch (v) {
case 'es3':
ecmaVersion = '3'
e = '3'
break
case 'es4':
ecmaVersion = '4'
e = '4'
break
case 'es5':
ecmaVersion = '5'
e = '5'
break
case 'es6':
ecmaVersion = '6'
e = '6'
break
case 'es7':
ecmaVersion = '7'
e = '7'
break
case 'es8':
ecmaVersion = '8'
e = '8'
break
case 'es9':
ecmaVersion = '9'
e = '9'
break
case 'es10':
ecmaVersion = '10'
break
case 'es11':
ecmaVersion = '11'
break
case 'es12':
ecmaVersion = '12'
e = '10'
break
case 'es2015':
ecmaVersion = '6'
e = '6'
break
case 'es2016':
ecmaVersion = '7'
e = '7'
break
case 'es2017':
ecmaVersion = '8'
e = '8'
break
case 'es2018':
ecmaVersion = '9'
e = '9'
break
case 'es2019':
ecmaVersion = '10'
break
case 'es2020':
ecmaVersion = '2020'
break
case 'es2021':
ecmaVersion = '2021'
e = '10'
break
default:
logger.error('Invalid ecmaScript version, please pass a valid version, use --help for help')
Expand All @@ -121,10 +110,7 @@ prog

const errArray = []
const globOpts = { nodir: true }
const acornOpts = {
ecmaVersion,
silent: true,
}
const acornOpts = { ecmaVersion: e, silent: true }
const filterForIgnore = (globbedFiles) => {
if (pathsToIgnore && pathsToIgnore.length > 0) {
const filtered = globbedFiles.filter(
Expand All @@ -135,7 +121,7 @@ prog
return globbedFiles
}

logger.debug(`ES-Check: Going to check files using version ${ecmaVersion}`)
logger.debug(`ES-Check: Going to check files using version ${e}`)

if (esmodule) {
acornOpts.sourceType = 'module'
Expand All @@ -148,9 +134,6 @@ prog
}

files.forEach((pattern) => {
/**
* @note pattern => glob or array
*/
const globbedFiles = glob.sync(pattern, globOpts)

if (globbedFiles.length === 0) {
Expand Down Expand Up @@ -193,7 +176,7 @@ prog
})
process.exit(1)
}
logger.info(`ES-Check: there were no ES version matching errors! 🎉`)
logger.error(`ES-Check: there were no ES version matching errors! 🎉`)
})

prog.parse(argsArray)
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ it('🎉 Es Check should pass when checking an array of es5 files as es5', (don
})

it('🎉 Es Check should pass when checking a file with a hash bang', (done) => {
exec('node index.js es6 index.js --allow-hash-bang', (err, stdout, stderr) => {
exec('node index.js es6 ./tests/scripts/hash-bang.js --allow-hash-bang', (err, stdout, stderr) => {
if (err) {
console.error(err.stack)
console.error(stdout.toString())
Expand Down Expand Up @@ -117,7 +117,7 @@ it('👌 Es Check should read from an .escheckrc file for config', (done) => {
})

it('👌 Es Check skips versions included in the not flag', (done) => {
exec('node index.js es6 ./tests/*/*.js --module --not=skipped,passed', (err, stdout, stderr) => {
exec('node index.js es6 ./tests/passed/*.js --module --not=skipped,passed', (err, stdout, stderr) => {
if (err) {
console.error(err.stack)
console.error(stdout.toString())
Expand All @@ -126,5 +126,5 @@ it('👌 Es Check skips versions included in the not flag', (done) => {
return
}
done()
});
});
})
})
7 changes: 7 additions & 0 deletions tests/scripts/hash-bang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node

'use strict'

const exec = require('child_process').exec

exec('This should pass')

0 comments on commit 4269ee0

Please sign in to comment.