-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CircleCI lint step to verify ES5 syntax for Internet Explorer (#4296
) * Add 18f/identity-es5-safe package * Add es5-safe script * Configure CircleCI to check ES5 safeness * Add JSDoc type annotations * Cast stream file value to string * Remove unnecessary eslint-disable Configured in eslintrc * Bump Mocha to latest version **Why**: ESM compat * Add test specs for identity-es5-safe
- Loading branch information
Showing
12 changed files
with
401 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
fixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"no-restricted-syntax": "off", | ||
"no-console": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
import { isAllSafe } from './index.js'; | ||
|
||
const patterns = process.argv.slice(2); | ||
if (patterns.length) { | ||
isAllSafe(patterns).then((isSafe) => process.exit(isSafe ? 0 : 1)); | ||
} else { | ||
console.log('Usage: is-es5-safe <pattern>'); | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { promises as fsPromises } from 'fs'; | ||
import { cpus } from 'os'; | ||
import pAll from 'p-all'; | ||
import glob from 'fast-glob'; | ||
import acorn from 'acorn'; | ||
|
||
const { readFile } = fsPromises; | ||
|
||
/** | ||
* Returns a promise resolving to a boolean representing whether the file contains valid ES5 syntax. | ||
* | ||
* @param {string} file File path. | ||
* | ||
* @return {Promise<boolean>} Promise resolving to safety of file. | ||
*/ | ||
export async function isSafe(file) { | ||
try { | ||
const fileText = await readFile(file, 'utf8'); | ||
acorn.parse(fileText, { ecmaVersion: 5 }); | ||
return true; | ||
} catch (error) { | ||
console.error(file, error); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a promise resolving to a boolean representing whether the files corresponding to the | ||
* given glob patterns contain valid ES5 syntax. | ||
* | ||
* @param {string[]} patterns Glob patterns. | ||
* | ||
* @return {Promise<boolean>} Promise resolving to safety of files corresponding to glob patterns. | ||
*/ | ||
export async function isAllSafe(patterns) { | ||
const files = glob.stream(patterns); | ||
|
||
const queue = []; | ||
for await (const file of files) { | ||
queue.push(() => isSafe(/** @type {string} */ (file))); | ||
} | ||
|
||
return (await pAll(queue, { concurrency: cpus().length })).every(Boolean); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { resolve, dirname } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { isSafe, isAllSafe } from './index.js'; | ||
|
||
const currentDir = dirname(fileURLToPath(import.meta.url)); | ||
|
||
describe('es5-safe', () => { | ||
describe('isSafe', () => { | ||
it('returns false and logs for unsafe file', async () => { | ||
const result = await isSafe(resolve(currentDir, 'spec/fixtures/unsafe.js')); | ||
|
||
expect(result).to.be.false(); | ||
expect(console).to.have.loggedError(); | ||
}); | ||
|
||
it('returns true for safe file', async () => { | ||
const result = await isSafe(resolve(currentDir, 'spec/fixtures/safe.js')); | ||
|
||
expect(result).to.be.true(); | ||
}); | ||
}); | ||
|
||
describe('isAllSafe', () => { | ||
it('returns false and logs for pattern include unsafe file', async () => { | ||
const result = await isAllSafe([resolve(currentDir, 'spec/fixtures/*.js')]); | ||
|
||
expect(result).to.be.false(); | ||
expect(console).to.have.loggedError(); | ||
}); | ||
|
||
it('returns true for pattern include all safe files', async () => { | ||
const result = await isAllSafe([resolve(currentDir, 'spec/fixtures/safe.js')]); | ||
|
||
expect(result).to.be.true(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@18f/identity-es5-safe", | ||
"private": true, | ||
"version": "1.0.0", | ||
"type": "module", | ||
"bin": { | ||
"is-es5-safe": "./cli.js" | ||
}, | ||
"dependencies": { | ||
"acorn": "^6.4.2", | ||
"fast-glob": "^3.2.4", | ||
"p-all": "^3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var a = { a: true }; | ||
var b = { b: true }; | ||
var obj = { a: true, b: true }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const a = { a: true }; | ||
const b = { b: true }; | ||
const obj = { ...a, ...b }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.