This is for my talk A beginner's guide to ASTs for JS Remote Conf 2016.
Last active
January 30, 2021 04:18
-
-
Save kentcdodds/b97e9c1f837accdae4dd to your computer and use it in GitHub Desktop.
A beginner's guide to ASTs files
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
const getRandomStarWarsName = uniqueRandomArray(getStarWarsNames()); | |
const globals = myGlobals() | |
export default function ({types: t}) { | |
return { | |
visitor: { | |
Identifier(path) { | |
if (globals.indexOf(path.node.name) !== -1) { | |
return; | |
} | |
const starWarsName = getRandomStarWarsName(); | |
const originalName = path.node.name; | |
path.scope.rename(originalName, starWarsName) | |
} | |
} | |
}; | |
} | |
function myGlobals() { | |
return [ | |
'require', 'module', 'exports' | |
]; | |
} | |
function uniqueRandomArray(arr) { | |
var rand = uniqueRandom(0, arr.length - 1); | |
return function () { | |
return arr[rand()]; | |
}; | |
} | |
function uniqueRandom(min, max) { | |
var prev = []; | |
return function rand() { | |
var num = Math.floor(Math.random() * (max - min + 1) + min); | |
if (prev.indexOf(num) !== -1) { | |
return rand(); | |
} | |
prev.push(num); | |
return num; | |
}; | |
}; | |
function getStarWarsNames() { | |
return [ | |
"4-LOM", | |
"Aayla Secura", | |
"Admiral Ackbar", | |
"Admiral Thrawn", | |
"Ahsoka Tano", | |
"Anakin Solo", | |
"Asajj Ventress", | |
"Aurra Sing", | |
"Senator Bail Organa", | |
"Barriss Offee", | |
"Bastila Shan", | |
"Ben Skywalker", | |
"Bib Fortuna", | |
"Biggs Darklighter", | |
"Boba Fett", | |
"Bossk", | |
"Brakiss", | |
"C-3PO", | |
"Cad Bane", | |
"Cade Skywalker", | |
"Callista Ming", | |
"Captain Rex", | |
"Carnor Jax", | |
"Chewbacca", | |
"Clone Commander Cody", | |
"Count Dooku", | |
"Darth Bane", | |
"Darth Krayt", | |
"Darth Maul", | |
"Darth Nihilus", | |
"Darth Vader", | |
"Dash Rendar", | |
"Dengar", | |
"Durge", | |
"Emperor Palpatine", | |
"Exar Kun", | |
"Galen Marek", | |
"General Crix Madine", | |
"General Dodonna", | |
"General Grievous", | |
"General Veers", | |
"Gilad Pellaeon", | |
"Grand Moff Tarkin", | |
"Greedo", | |
"Han Solo", | |
"IG 88", | |
"Jabba The Hutt", | |
"Jacen Solo", | |
"Jaina Solo", | |
"Jango Fett", | |
"Jarael", | |
"Jerec", | |
"Joruus C'Baoth", | |
"Ki-Adi-Mundi", | |
"Kir Kanos", | |
"Kit Fisto", | |
"Kyle Katarn", | |
"Kyp Durron", | |
"Lando Calrissian", | |
"Luke Skywalker", | |
"Luminara Unduli", | |
"Lumiya", | |
"Mace Windu", | |
"Mara Jade", | |
"Mission Vao", | |
"Natasi Daala", | |
"Nom Anor", | |
"Obi-Wan Kenobi", | |
"Padmé Amidala", | |
"Plo Koon", | |
"Pre Vizsla", | |
"Prince Xizor", | |
"Princess Leia", | |
"PROXY", | |
"Qui-Gon Jinn", | |
"Quinlan Vos", | |
"R2-D2", | |
"Rahm Kota", | |
"Revan", | |
"Satele Shan", | |
"Savage Opress", | |
"Sebulba", | |
"Shaak Ti", | |
"Shmi Skywalker", | |
"Talon Karrde", | |
"Ulic Qel-Droma", | |
"Visas Marr", | |
"Watto", | |
"Wedge Antilles", | |
"Yoda", | |
"Zam Wesell", | |
"Zayne Carrick", | |
"Zuckuss" | |
].map(name => { | |
const letterOnlyName = name | |
.replace(/[^a-z]/ig, '') | |
return letterOnlyName.substr(0, 1).toLowerCase() + letterOnlyName.substring(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
const getRandomStarWarsName = uniqueRandomArray(getStarWarsNames()); | |
export default function ({types: t}) { | |
return { | |
visitor: { | |
} | |
}; | |
} | |
function uniqueRandomArray(arr) { | |
var rand = uniqueRandom(0, arr.length - 1); | |
return function () { | |
return arr[rand()]; | |
}; | |
} | |
function uniqueRandom(min, max) { | |
var prev = []; | |
return function rand() { | |
var num = Math.floor(Math.random() * (max - min + 1) + min); | |
if (prev.indexOf(num) !== -1) { | |
return rand(); | |
} | |
prev.push(num); | |
return num; | |
}; | |
}; | |
function getStarWarsNames() { | |
return [ | |
"4-LOM", | |
"Aayla Secura", | |
"Admiral Ackbar", | |
"Admiral Thrawn", | |
"Ahsoka Tano", | |
"Anakin Solo", | |
"Asajj Ventress", | |
"Aurra Sing", | |
"Senator Bail Organa", | |
"Barriss Offee", | |
"Bastila Shan", | |
"Ben Skywalker", | |
"Bib Fortuna", | |
"Biggs Darklighter", | |
"Boba Fett", | |
"Bossk", | |
"Brakiss", | |
"C-3PO", | |
"Cad Bane", | |
"Cade Skywalker", | |
"Callista Ming", | |
"Captain Rex", | |
"Carnor Jax", | |
"Chewbacca", | |
"Clone Commander Cody", | |
"Count Dooku", | |
"Darth Bane", | |
"Darth Krayt", | |
"Darth Maul", | |
"Darth Nihilus", | |
"Darth Vader", | |
"Dash Rendar", | |
"Dengar", | |
"Durge", | |
"Emperor Palpatine", | |
"Exar Kun", | |
"Galen Marek", | |
"General Crix Madine", | |
"General Dodonna", | |
"General Grievous", | |
"General Veers", | |
"Gilad Pellaeon", | |
"Grand Moff Tarkin", | |
"Greedo", | |
"Han Solo", | |
"IG 88", | |
"Jabba The Hutt", | |
"Jacen Solo", | |
"Jaina Solo", | |
"Jango Fett", | |
"Jarael", | |
"Jerec", | |
"Joruus C'Baoth", | |
"Ki-Adi-Mundi", | |
"Kir Kanos", | |
"Kit Fisto", | |
"Kyle Katarn", | |
"Kyp Durron", | |
"Lando Calrissian", | |
"Luke Skywalker", | |
"Luminara Unduli", | |
"Lumiya", | |
"Mace Windu", | |
"Mara Jade", | |
"Mission Vao", | |
"Natasi Daala", | |
"Nom Anor", | |
"Obi-Wan Kenobi", | |
"Padmé Amidala", | |
"Plo Koon", | |
"Pre Vizsla", | |
"Prince Xizor", | |
"Princess Leia", | |
"PROXY", | |
"Qui-Gon Jinn", | |
"Quinlan Vos", | |
"R2-D2", | |
"Rahm Kota", | |
"Revan", | |
"Satele Shan", | |
"Savage Opress", | |
"Sebulba", | |
"Shaak Ti", | |
"Shmi Skywalker", | |
"Talon Karrde", | |
"Ulic Qel-Droma", | |
"Visas Marr", | |
"Watto", | |
"Wedge Antilles", | |
"Yoda", | |
"Zam Wesell", | |
"Zayne Carrick", | |
"Zuckuss" | |
].map(name => { | |
const letterOnlyName = name | |
.replace(/[^a-z]/ig, '') | |
return letterOnlyName.substr(0, 1).toLowerCase() + letterOnlyName.substring(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
module.exports = function(context) { | |
return { | |
IfStatement(node) { | |
if (isBlock(node.consequent) && isBlock(node.alternate)) { | |
return | |
} | |
context.report({ | |
node: node, | |
message: 'yo, y u no block?', | |
fix: function(fixer) { | |
// fix the codez | |
} | |
}) | |
function isBlock(n) { | |
return !n || n.type === 'BlockStatement'; | |
} | |
} | |
} | |
}; |
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
const uniqueRandomArray = require('unique-random-array') | |
const starWarsNames = require('./starwars-names.json') | |
const getRandomItem = uniqueRandomArray(starWarsNames) | |
module.exports = { | |
all: starWarsNames, | |
random: random, | |
} | |
function random(number) { | |
if (number === undefined) { | |
return getRandomItem() | |
} else { | |
var randomItems = [] | |
for (var i = 0; i < number; i++) { | |
randomItems.push(getRandomItem()) | |
} | |
return randomItems | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment