New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add 12 missing NODE_FIELDS #13577
Merged
Merged
add 12 missing NODE_FIELDS #13577
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b1802e
test: add fields test
jedwards1211 f25f554
fix(babel-types): add missing NODE_FIELDS and tests
jedwards1211 93bd61a
chore: avoid using fs-extra
jedwards1211 ce43998
chore: code cleanup
jedwards1211 27dd0f9
chore: avoid util.promisify
jedwards1211 60f3126
fix: remove bad ts-expect-error suppressions
jedwards1211 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as t from "../lib"; | ||
import glob from "glob"; | ||
import path from "path"; | ||
import fs from "fs-extra"; | ||
import { inspect } from "util"; | ||
|
||
// eslint-disable-next-line no-restricted-globals | ||
const packages = path.resolve(__dirname, "..", ".."); | ||
|
||
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 fs.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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can replace
fs-extra
byJSON.parse(fs.readFileSync(#))
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just switched it to
promisify(fs.readFile)
, wouldn't want to block the entire processThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argh, didn't realize tests run on node 8 though,I'll promisify it manually