Skip to content

Instantly share code, notes, and snippets.

@danman113
Last active February 28, 2020 01:15
Show Gist options
  • Save danman113/22f194553cf2edc720de943281debc22 to your computer and use it in GitHub Desktop.
Save danman113/22f194553cf2edc720de943281debc22 to your computer and use it in GitHub Desktop.
Checks if input javascript files will instantly crash on IE11
#!/usr/bin/env node
const fs = require('fs')
const acorn = require("acorn")
const glob = require('glob')
const getNextCharPosition = (idx, str, offset = 1, matchChar = ' ') => {
let i = idx
while (str[i] !== matchChar) {
i += offset
}
return i
}
const outputs = process.argv.slice(2).map(path => glob.sync(path)).flat()
console.log('Checking files: ')
// IE11 supports let and const, but ES5 doesn't. So we rename all instances of let and const.
// This results in a lot of false positives ('let me in' => 'var me in'), but this shouldn't matter
// because we're only parsing the code, not evaluating it.
outputs.forEach(file => {
console.log(' ', file)
const code = fs.readFileSync(file, { encoding: 'utf8' })
.replace(/const |let /g, 'var ')
try {
acorn.parse(code, { ecmaVersion: 5, ranges: true })
} catch (e) {
const { pos, loc: { line, column } } = e
console.error(`Error @ ${file} - ${line}:${column}`)
console.error(
code.substring(
getNextCharPosition(pos, code, -1, '\n') + 1,
getNextCharPosition(pos, code)
)
)
return process.exit(1)
}
})
console.log('Pass')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment