-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: compute list of expected globals from ESLint config file
We don't want to rely on mutable globals for core modules. Instead of maintaining a separate list of known globals in our test files, parse the ESLint config to ensure all globals are restricted in the `lib/` directory.
- Loading branch information
Showing
2 changed files
with
47 additions
and
67 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const readline = require('readline'); | ||
|
||
const searchLines = [ | ||
' no-restricted-globals:', | ||
' node-core/prefer-primordials:', | ||
]; | ||
|
||
const restrictedGlobalLine = /^\s{4}- name:\s?([^#\s]+)/; | ||
const closingLine = /^\s{0,3}[^#\s]/; | ||
|
||
module.exports = function parseEslintConfigForGlobals() { | ||
const eslintConfig = readline.createInterface({ | ||
input: fs.createReadStream(path.join(__dirname, '..', '..', 'lib', '.eslintrc.yaml')), | ||
crlfDelay: Infinity, | ||
}); | ||
|
||
const globals = [process]; | ||
|
||
let isReadingGlobals = false; | ||
eslintConfig.on('line', (line) => { | ||
if (isReadingGlobals) { | ||
const match = restrictedGlobalLine.exec(line); | ||
if (match && match[1] in global) { | ||
globals.push(global[match[1]]); | ||
} else if (closingLine.test(line)) { | ||
isReadingGlobals = false; | ||
} | ||
} else if (line === searchLines[0]) { | ||
searchLines.shift(); | ||
isReadingGlobals = true; | ||
} | ||
}); | ||
|
||
return globals; | ||
}; |