Skip to content

Commit

Permalink
Format code using prettier and check for formatting during CI builds
Browse files Browse the repository at this point in the history
  • Loading branch information
rpetrich committed Aug 4, 2019
1 parent 004b60d commit 2954be4
Show file tree
Hide file tree
Showing 5 changed files with 1,572 additions and 413 deletions.
9 changes: 9 additions & 0 deletions .prettierrc.json
@@ -0,0 +1,9 @@
{
"trailingComma": "es5",
"useTabs": true,
"tabWidth": 4,
"singleQuote": false,
"printWidth": 120,
"quoteProps": "preserve",
"arrowParens": "always"
}
124 changes: 107 additions & 17 deletions async-to-promises.test.js
Expand Up @@ -37,7 +37,41 @@ const environments = {
},
};

const helperNames = ["_Pact", "_settle", "_isSettledPact", "_async", "_await", "_awaitIgnored", "_continue", "_continueIgnored", "_forTo", "_forValues", "_forIn", "_forOwn", "_forOf", "_forAwaitOf", "_for", "_do", "_switch", "_call", "_callIgnored", "_invoke", "_invokeIgnored", "_catch", "_finallyRethrows", "_finally", "_rethrow", "_empty", "_earlyReturn", "_catchInGenerator", "_wrapReturnedValue", "_wrapYieldedValue", "_AsyncGenerator", "_iteratorSymbol", "_asyncIteratorSymbol"];
const helperNames = [
"_Pact",
"_settle",
"_isSettledPact",
"_async",
"_await",
"_awaitIgnored",
"_continue",
"_continueIgnored",
"_forTo",
"_forValues",
"_forIn",
"_forOwn",
"_forOf",
"_forAwaitOf",
"_for",
"_do",
"_switch",
"_call",
"_callIgnored",
"_invoke",
"_invokeIgnored",
"_catch",
"_finallyRethrows",
"_finally",
"_rethrow",
"_empty",
"_earlyReturn",
"_catchInGenerator",
"_wrapReturnedValue",
"_wrapYieldedValue",
"_AsyncGenerator",
"_iteratorSymbol",
"_asyncIteratorSymbol",
];

const stripHelpersVisitor = {
FunctionDeclaration(path) {
Expand All @@ -56,7 +90,9 @@ const stripHelpersVisitor = {
path.skip();
} else if (path.isVariableDeclaration()) {
const allDeclarations = path.get("declarations");
const declarationsToRemove = allDeclarations.filter(declaration => /^_async/.test(declaration.node.id.name));
const declarationsToRemove = allDeclarations.filter((declaration) =>
/^_async/.test(declaration.node.id.name)
);
if (declarationsToRemove.length === allDeclarations.length) {
path.remove();
} else {
Expand All @@ -80,11 +116,15 @@ const stripHelpersVisitor = {
path.parentPath.remove();
}
}
}
},
};

function extractOnlyUserCode(babel, result) {
return babel.transformFromAst(result.ast, result.code, { plugins: [{ visitor: stripHelpersVisitor }], compact: true, ast: false }).code;
return babel.transformFromAst(result.ast, result.code, {
plugins: [{ visitor: stripHelpersVisitor }],
compact: true,
ast: false,
}).code;
}

function extractJustFunction(babel, result) {
Expand All @@ -106,7 +146,7 @@ function writeOutput(name, myCode, outputCode) {
}
}

const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;

function readTest(name) {
let input;
Expand Down Expand Up @@ -134,7 +174,14 @@ function readTest(name) {
}
}
}
const { error, checkSyntax = true, module = false, plugins = [], supportedBabels = Object.keys(environments), presets = [] } = options || {};
const {
error,
checkSyntax = true,
module = false,
plugins = [],
supportedBabels = Object.keys(environments),
presets = [],
} = options || {};
return {
error,
checkSyntax,
Expand All @@ -151,7 +198,16 @@ function readTest(name) {
}

function parse(babel, input) {
return babel.parse ? babel.parse(input, { parserOpts: { allowReturnOutsideFunction: true, plugins: ["asyncGenerators"] }, sourceType: "module" }) : babylon.parse(input, { allowReturnOutsideFunction: true, sourceType: "module", plugins: ["asyncGenerators", "objectRestSpread"] });
return babel.parse
? babel.parse(input, {
parserOpts: { allowReturnOutsideFunction: true, plugins: ["asyncGenerators"] },
sourceType: "module",
})
: babylon.parse(input, {
allowReturnOutsideFunction: true,
sourceType: "module",
plugins: ["asyncGenerators", "objectRestSpread"],
});
}

for (const { babel } of Object.values(environments)) {
Expand All @@ -164,7 +220,19 @@ for (const name of fs.readdirSync("tests").sort()) {
}
if (fs.statSync(`tests/${name}`).isDirectory()) {
describe(name, () => {
const { input, output, inlined, hoisted, cases, error, checkSyntax, module, plugins, presets, supportedBabels } = readTest(name);
const {
input,
output,
inlined,
hoisted,
cases,
error,
checkSyntax,
module,
plugins,
presets,
supportedBabels,
} = readTest(name);
for (const babelName of supportedBabels) {
describe(babelName, () => {
const { babel, types, pluginUnderTest, pluginMapping } = environments[babelName];
Expand All @@ -174,7 +242,11 @@ for (const name of fs.readdirSync("tests").sort()) {
if (error) {
test("error", () => {
try {
babel.transformFromAst(ast, parseInput, { presets, plugins: [[pluginUnderTest, {}]], compact: true })
babel.transformFromAst(ast, parseInput, {
presets,
plugins: [[pluginUnderTest, {}]],
compact: true,
});
throw new Error("Expected error: " + error.toString());
} catch (e) {
expect(e.toString()).toEqual(expect.stringContaining(error));
Expand All @@ -183,20 +255,34 @@ for (const name of fs.readdirSync("tests").sort()) {
return;
}
const extractFunction = module ? extractOnlyUserCode : extractJustFunction;
const result = babel.transformFromAst(types.cloneDeep(ast), parseInput, { presets, plugins: mappedPlugins.concat([[pluginUnderTest, { target: "es6" }]]), compact: true, ast: true });
const result = babel.transformFromAst(types.cloneDeep(ast), parseInput, {
presets,
plugins: mappedPlugins.concat([[pluginUnderTest, { target: "es6" }]]),
compact: true,
ast: true,
});
const strippedResult = extractFunction(babel, result);
const inlinedResult = babel.transformFromAst(types.cloneDeep(ast), parseInput, { presets, plugins: mappedPlugins.concat([[pluginUnderTest, { inlineHelpers: true }]]), compact: true, ast: true });
const inlinedResult = babel.transformFromAst(types.cloneDeep(ast), parseInput, {
presets,
plugins: mappedPlugins.concat([[pluginUnderTest, { inlineHelpers: true }]]),
compact: true,
ast: true,
});
const inlinedAndStrippedResult = extractFunction(babel, inlinedResult);
const hoistedResult = babel.transformFromAst(types.cloneDeep(ast), parseInput, { presets, plugins: mappedPlugins.concat([[pluginUnderTest, { hoist: true, minify: true }]]), compact: true, ast: true });
const hoistedResult = babel.transformFromAst(types.cloneDeep(ast), parseInput, {
presets,
plugins: mappedPlugins.concat([[pluginUnderTest, { hoist: true, minify: true }]]),
compact: true,
ast: true,
});
const hoistedAndStrippedResult = extractFunction(babel, hoistedResult);
writeOutput(`tests/${name}/output.js`, strippedResult);
writeOutput(`tests/${name}/inlined.js`, inlinedAndStrippedResult, strippedResult);
writeOutput(`tests/${name}/hoisted.js`, hoistedAndStrippedResult, strippedResult);
let fn, rewrittenFn, inlinedFn, hoistedFn;
try {
fn = new Function(`/* ${name} original */${parseInput}`)
} catch (e) {
}
fn = new Function(`/* ${name} original */${parseInput}`);
} catch (e) {}
if (checkSyntax) {
describe("syntax", () => {
test("normal", () => {
Expand Down Expand Up @@ -241,10 +327,14 @@ for (const name of fs.readdirSync("tests").sort()) {
expect(strippedResult).toBe(output);
});
test("inlined", () => {
expect(inlinedAndStrippedResult).toBe(typeof inlined !== "undefined" ? inlined : output);
expect(inlinedAndStrippedResult).toBe(
typeof inlined !== "undefined" ? inlined : output
);
});
test("hoisted", () => {
expect(hoistedAndStrippedResult).toBe(typeof hoisted !== "undefined" ? hoisted : output);
expect(hoistedAndStrippedResult).toBe(
typeof hoisted !== "undefined" ? hoisted : output
);
});
});
}
Expand Down

0 comments on commit 2954be4

Please sign in to comment.