Debian Codegraph @

util/build/transforms/hasFixup.js

raw symbols SCIP
1 define(["../buildControl"], function(bc){
2 //
3 // For and feature, "x", in bc.staticHasFeatures, replace the string has("x") with the value as provided by bc.staticHasFeatures.
4 //
5 // For example, consider the following code
6 //
7 // if(has("feature-a")){
8 // //...
9 // }
10 //
11 // if(has("feature-b")>=7){
12 // //...
13 // }
14 //
15 // if(has("feature-c")){
16 // //...
17 // }
18 //
19 // if(has("feature-d")){
20 // //...
21 // }
22 //
23 // if(has("feature-e")=="goodbye"){
24 // //...
25 // }
26 //
27 // Then, given bc.staticHasFeatures of
28 //
29 // {
30 // "feature-a":undefined,
31 // "feature-b":7,
32 // "feature-c":true,
33 // "feature-d":false,
34 // "feature-e":"'hello, world'"
35 // }
36 //
37 // The source is modified as follows:
38 // if(undefined){
39 // //...
40 // }
41 //
42 // if(7>=7){
43 // //...
44 // }
45 //
46 // if(true){
47 // //...
48 // }
49 //
50 // if(false){
51 // //...
52 // }
53 //
54 // if('hello, world'=="goodbye"){
55 // //...
56 // }
57 //
58 // Recall that has.add has a now parameter that causes the test to be executed immediately and return the value.
59 // If a static has value is truthy, then using || causes the first truthy operand to be returned...the truthy static value.
60 // If a static value is falsy, then using && causes the first falsy operand to be returned...the falsy static value.
61 //
62 // For example:
63 //
64 // if(has.add("feature-a", function(global, doc, element){
65 // //some really long, painful has test
66 // }, true)){
67 // //...
68 // }
69 //
70 // Consider static has values for feature-a of undefined, 7, true, false, "'hello, world'"
71 //
72 //
73 // if(undefined && has.add("feature-a", function(global, doc, element){ // NOTE: the value of the if conditional is static undefined
74 // //some really long, painful has test
75 // }, true)){
76 // //...
77 // }
78 //
79 // if(7 || has.add("feature-a", function(global, doc, element){ // NOTE: the value of the if conditional is static 7
80 // //some really long, painful has test
81 // }, true)){
82 // //...
83 // }
84 //
85 // if(true || has.add("feature-a", function(global, doc, element){ // NOTE: the value of the if conditional is static true
86 // //some really long, painful has test
87 // }, true)){
88 // //...
89 // }
90 //
91 // if(false && has.add("feature-a", function(global, doc, element){ // NOTE: the value of the if conditional is static false
92 // //some really long, painful has test
93 // }, true)){
94 // //...
95 // }
96 //
97 // if('hello, world' || has.add("feature-a", function(global, doc, element){ // NOTE: the value of the if conditional is static 'hello, world'
98 // //some really long, painful has test
99 // }, true)){
100 // //...
101 // }
102 //
103 // This technique is employed to avoid attempting to parse and find the boundaries of "some really long painful has test" with regexs.
104 // Instead, this is left to an optimizer like the the closure compiler or uglify etc.
105
106 function stringifyString(s){
107 return typeof s === "string" ? '"' + s + '"' : s;
108 }
109
110 return function(resource){
111 resource.text = resource.text.replace(/([^\w\.])has\s*\(\s*["']([^'"]+)["']\s*\)/g, function(match, prefix, featureName){
112 if(featureName in bc.staticHasFeatures){
113 if(bc.staticHasFeatures[featureName] !== undefined){
114 return prefix + " " + bc.staticHasFeatures[featureName] + " ";
115 }else{
116 return match;
117 }
118 }else{
119 return match;
120 }
121 }).replace(/([^\w\.])((has.add\s*\(\s*)["']([^'"]+)["'])/g, function(match, prefix, hasAdd, notUsed, featureName){
122 if(featureName in bc.staticHasFeatures){
123 if(bc.staticHasFeatures[featureName] !== undefined){
124 return prefix + " " + stringifyString(bc.staticHasFeatures[featureName]) + (bc.staticHasFeatures[featureName] ? " || " : " && " ) + hasAdd;
125 }else{
126 return match;
127 }
128 }else{
129 return match;
130 }
131 });
132 return 0;
133 };
134
135 });