Skip to content
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

Generation: Remove unused helpers from generated code #448

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 0 additions & 62 deletions detox/src/android/espressoapi/DetoxAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,6 @@
*/


// Globally declared helpers

function sanitize_greyDirection(action) {
switch (action) {
case "left":
return 1;
case "right":
return 2;
case "up":
return 3;
case "down":
return 4;

default:
throw new Error(`GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got ${action}`);
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;

default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}

function sanitize_uiAccessibilityTraits(value) {
let traits = 0;
for (let i = 0; i < value.length; i++) {
switch (value[i]) {
case 'button': traits |= 1; break;
case 'link': traits |= 2; break;
case 'header': traits |= 4; break;
case 'search': traits |= 8; break;
case 'image': traits |= 16; break;
case 'selected': traits |= 32; break;
case 'plays': traits |= 64; break;
case 'key': traits |= 128; break;
case 'text': traits |= 256; break;
case 'summary': traits |= 512; break;
case 'disabled': traits |= 1024; break;
case 'frequentUpdates': traits |= 2048; break;
case 'startsMedia': traits |= 4096; break;
case 'adjustable': traits |= 8192; break;
case 'allowsDirectInteraction': traits |= 16384; break;
case 'pageTurn': traits |= 32768; break;
default: throw new Error(`Unknown trait '${value[i]}', see list in https://facebook.github.io/react-native/docs/accessibility.html#accessibilitytraits-ios`);
}
}

return traits;
}



class DetoxAction {
static multiClick(times) {
Expand Down
33 changes: 0 additions & 33 deletions detox/src/ios/earlgreyapi/GREYActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/


// Globally declared helpers

function sanitize_greyDirection(action) {
switch (action) {
case "left":
Expand All @@ -22,7 +20,6 @@ function sanitize_greyDirection(action) {
throw new Error(`GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got ${action}`);
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
Expand All @@ -38,36 +35,6 @@ function sanitize_greyContentEdge(action) {
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}

function sanitize_uiAccessibilityTraits(value) {
let traits = 0;
for (let i = 0; i < value.length; i++) {
switch (value[i]) {
case 'button': traits |= 1; break;
case 'link': traits |= 2; break;
case 'header': traits |= 4; break;
case 'search': traits |= 8; break;
case 'image': traits |= 16; break;
case 'selected': traits |= 32; break;
case 'plays': traits |= 64; break;
case 'key': traits |= 128; break;
case 'text': traits |= 256; break;
case 'summary': traits |= 512; break;
case 'disabled': traits |= 1024; break;
case 'frequentUpdates': traits |= 2048; break;
case 'startsMedia': traits |= 4096; break;
case 'adjustable': traits |= 8192; break;
case 'allowsDirectInteraction': traits |= 16384; break;
case 'pageTurn': traits |= 32768; break;
default: throw new Error(`Unknown trait '${value[i]}', see list in https://facebook.github.io/react-native/docs/accessibility.html#accessibilitytraits-ios`);
}
}

return traits;
}



class GREYActions {
/*@return A GREYAction that performs multiple taps of a specified @c count.
*/static actionForMultipleTapsWithCount(count) {
Expand Down
62 changes: 0 additions & 62 deletions detox/src/ios/earlgreyapi/GREYMatchers+Detox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,6 @@
*/


// Globally declared helpers

function sanitize_greyDirection(action) {
switch (action) {
case "left":
return 1;
case "right":
return 2;
case "up":
return 3;
case "down":
return 4;

default:
throw new Error(`GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got ${action}`);
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;

default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}

function sanitize_uiAccessibilityTraits(value) {
let traits = 0;
for (let i = 0; i < value.length; i++) {
switch (value[i]) {
case 'button': traits |= 1; break;
case 'link': traits |= 2; break;
case 'header': traits |= 4; break;
case 'search': traits |= 8; break;
case 'image': traits |= 16; break;
case 'selected': traits |= 32; break;
case 'plays': traits |= 64; break;
case 'key': traits |= 128; break;
case 'text': traits |= 256; break;
case 'summary': traits |= 512; break;
case 'disabled': traits |= 1024; break;
case 'frequentUpdates': traits |= 2048; break;
case 'startsMedia': traits |= 4096; break;
case 'adjustable': traits |= 8192; break;
case 'allowsDirectInteraction': traits |= 16384; break;
case 'pageTurn': traits |= 32768; break;
default: throw new Error(`Unknown trait '${value[i]}', see list in https://facebook.github.io/react-native/docs/accessibility.html#accessibilitytraits-ios`);
}
}

return traits;
}



class GREYMatchers {
static detoxMatcherForText(text) {
Expand Down
50 changes: 14 additions & 36 deletions detox/src/ios/earlgreyapi/GREYMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,6 @@
*/


// Globally declared helpers

function sanitize_greyDirection(action) {
switch (action) {
case "left":
return 1;
case "right":
return 2;
case "up":
return 3;
case "down":
return 4;

default:
throw new Error(`GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got ${action}`);
}
}

function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;

default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}

function sanitize_uiAccessibilityTraits(value) {
let traits = 0;
for (let i = 0; i < value.length; i++) {
Expand All @@ -65,9 +31,21 @@ function sanitize_uiAccessibilityTraits(value) {

return traits;
}
function sanitize_greyContentEdge(action) {
switch (action) {
case "left":
return 0;
case "right":
return 1;
case "top":
return 2;
case "bottom":
return 3;



default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}
class GREYMatchers {
/*Matcher for application's key window.

Expand Down
12 changes: 12 additions & 0 deletions generation/__tests__/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ describe("iOS generation", () => {
});
});

describe("helper functions", () => {
it("should include them, if they are used", () => {
// GREYDirection is included, so sanitize_greyDirection should be there
expect(exampleContent.indexOf("function sanitize_greyDirection")).not.toBe(-1);
});

it("should not include them, if they are unused", () => {
// UIAccessibilityTraits is not included, so sanitize_uiAccessibilityTraits should not be there
expect(exampleContent.indexOf("function sanitize_uiAccessibilityTraits")).toBe(-1);
});
});

afterAll(() => {
// Clean up
remove.removeSync("./__tests__/generated-ios");
Expand Down
3 changes: 3 additions & 0 deletions generation/adapters/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ const typeCheckInterfaces = {
const supportedContentSanitizersMap = {
GREYDirection: {
type: "NSInteger",
name: "sanitize_greyDirection",
value: callGlobal("sanitize_greyDirection")
},
GREYContentEdge: {
type: "NSInteger",
name: "sanitize_greyContentEdge",
value: callGlobal("sanitize_greyContentEdge")
},
UIAccessibilityTraits: {
type: "NSInteger",
name: "sanitize_uiAccessibilityTraits",
value: callGlobal("sanitize_uiAccessibilityTraits")
}
};
Expand Down
16 changes: 13 additions & 3 deletions generation/core/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const generate = require("babel-generator").default;
const fs = require("fs");

const { methodNameToSnakeCase } = require("../helpers");

let globalFunctionUsage = {};
module.exports = function ({ typeCheckInterfaces, renameTypesMap, supportedTypes, classValue, supportedContentSanitizersMap }) {
/**
* the input provided by objective-c-parser looks like this:
Expand Down Expand Up @@ -118,6 +118,7 @@ module.exports = function ({ typeCheckInterfaces, renameTypesMap, supportedTypes

function addArgumentContentSanitizerCall(json) {
if (supportedContentSanitizersMap[json.type]) {
globalFunctionUsage[supportedContentSanitizersMap[json.type].name] = true;
return supportedContentSanitizersMap[json.type].value(json.name);
}

Expand Down Expand Up @@ -175,6 +176,7 @@ module.exports = function ({ typeCheckInterfaces, renameTypesMap, supportedTypes

return function (files) {
Object.entries(files).forEach(([inputFile, outputFile]) => {
globalFunctionUsage = {};
const input = fs.readFileSync(inputFile, "utf8");
const isObjectiveC = inputFile[inputFile.length - 1] === 'h';

Expand All @@ -185,8 +187,16 @@ module.exports = function ({ typeCheckInterfaces, renameTypesMap, supportedTypes
const commentBefore = "/**\n\n\tThis code is generated.\n\tFor more information see generation/README.md.\n*/\n\n";

// Add global helper functions
const globalFunctionsSource = fs.readFileSync(__dirname + "/global-functions.js", "utf8");
const globalFunctions = globalFunctionsSource.substr(0, globalFunctionsSource.indexOf("module.exports"));
const globalFunctionsStr = fs.readFileSync(__dirname + "/global-functions.js", "utf8")
const globalFunctionsSource = globalFunctionsStr.substr(0, globalFunctionsStr.indexOf("module.exports"));

// Only include global functions that are actually used
const usedGlobalFunctions = Object.entries(globalFunctionUsage).filter(([key, value]) => value).map(([key]) => key);
const globalFunctions = usedGlobalFunctions.map(name => {
const start = globalFunctionsSource.indexOf(`function ${name}`);
const end = globalFunctionsSource.indexOf(`// END ${name}`);
return globalFunctionsSource.substr(start, end - start);
}).join('\n');

const code = [commentBefore, globalFunctions, output.code].join('\n');
fs.writeFileSync(outputFile, code, "utf8");
Expand Down
8 changes: 5 additions & 3 deletions generation/core/global-functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Globally declared helpers
// Each function needs to end with }// END function_name so that it can be
// dynamically included while generating

function sanitize_greyDirection(action) {
switch (action) {
Expand All @@ -14,7 +16,7 @@ function sanitize_greyDirection(action) {
default:
throw new Error(`GREYAction.GREYDirection must be a 'left'/'right'/'up'/'down', got ${action}`);
}
}
}// END sanitize_greyDirection

function sanitize_greyContentEdge(action) {
switch (action) {
Expand All @@ -30,7 +32,7 @@ function sanitize_greyContentEdge(action) {
default:
throw new Error(`GREYAction.GREYContentEdge must be a 'left'/'right'/'top'/'bottom', got ${action}`);
}
}
}// END sanitize_greyContentEdge

function sanitize_uiAccessibilityTraits(value) {
let traits = 0;
Expand All @@ -57,7 +59,7 @@ function sanitize_uiAccessibilityTraits(value) {
}

return traits;
}
}// END sanitize_uiAccessibilityTraits


module.exports = {
Expand Down