forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 12 missing NODE_FIELDS (babel#13577)
* test: add fields test * fix(babel-types): add missing NODE_FIELDS and tests fix babel#13558 fix babel#13563 * chore: avoid using fs-extra * chore: code cleanup * chore: avoid util.promisify * fix: remove bad ts-expect-error suppressions
- Loading branch information
1 parent
01c4d78
commit c97f9c4
Showing
9 changed files
with
177 additions
and
4 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
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
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
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,116 @@ | ||
import * as t from "../lib"; | ||
import glob from "glob"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { inspect } from "util"; | ||
|
||
// eslint-disable-next-line no-restricted-globals | ||
const packages = path.resolve(__dirname, "..", ".."); | ||
|
||
function readJson(file) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(file, "utf8", (err, data) => { | ||
if (err) reject(err); | ||
else resolve(JSON.parse(data)); | ||
}); | ||
}); | ||
} | ||
|
||
function traverse(thing, visitor) { | ||
if (Array.isArray(thing)) { | ||
thing.forEach(elem => traverse(elem, visitor)); | ||
} else if (thing instanceof Object && typeof thing.type === "string") { | ||
visitor(thing); | ||
for (const key in thing) { | ||
const value = thing[key]; | ||
if (value instanceof Object) traverse(value, visitor); | ||
} | ||
} | ||
} | ||
|
||
const files = glob.sync( | ||
path.join("babel-parser", "test", "**", "output.json"), | ||
{ | ||
cwd: packages, | ||
ignore: [ | ||
path.join("**", "estree*", "**"), | ||
path.join("**", "is-expression-babel-parser", "**"), | ||
], | ||
}, | ||
); | ||
|
||
const ignoredFields = { | ||
ArrowFunctionExpression: { id: true, predicate: true }, | ||
ClassMethod: { id: true, predicate: true }, | ||
ClassPrivateMethod: { id: true, predicate: true }, | ||
ClassPrivateProperty: { declare: true, optional: true }, | ||
FunctionDeclaration: { predicate: true }, | ||
FunctionExpression: { predicate: true }, | ||
ImportDeclaration: { attributes: true }, | ||
ObjectProperty: { method: true }, | ||
ObjectMethod: { method: true, id: true, predicate: true }, | ||
StaticBlock: { static: true }, | ||
TSDeclareMethod: { id: true }, | ||
}; | ||
|
||
function isEmpty(obj) { | ||
for (const key in obj) return false; | ||
return true; | ||
} | ||
|
||
describe("NODE_FIELDS contains all fields in", function () { | ||
files.forEach(file => | ||
it(`${file}`, async function () { | ||
const ast = await readJson(path.resolve(packages, file)); | ||
if (ast.type === "File" && ast.errors && ast.errors.length) return; | ||
t[`assert${ast.type}`](ast); | ||
const missingFields = {}; | ||
traverse(ast, node => { | ||
const { type } = node; | ||
switch (type) { | ||
case "File": | ||
case "CommentBlock": | ||
case "CommentLine": | ||
return; | ||
} | ||
if (ignoredFields[type] === true) return; | ||
const fields = t.NODE_FIELDS[type]; | ||
if (!fields) { | ||
if (!missingFields[type]) { | ||
missingFields[type] = { | ||
MISSING_TYPE: true, | ||
}; | ||
} | ||
return; | ||
} | ||
for (const field in node) { | ||
switch (field) { | ||
case "type": | ||
case "start": | ||
case "end": | ||
case "loc": | ||
case "range": | ||
case "leadingComments": | ||
case "innerComments": | ||
case "trailingComments": | ||
case "comments": | ||
case "extra": | ||
continue; | ||
} | ||
if (!fields[field]) { | ||
if (ignoredFields[type] && ignoredFields[type][field]) continue; | ||
if (!missingFields[type]) missingFields[type] = {}; | ||
if (!missingFields[type][field]) { | ||
missingFields[type][field] = true; | ||
} | ||
} | ||
} | ||
}); | ||
if (!isEmpty(missingFields)) { | ||
throw new Error( | ||
`the following NODE_FIELDS were missing: ${inspect(missingFields)}`, | ||
); | ||
} | ||
}), | ||
); | ||
}); |
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