| 1 |
"use strict"; |
| 2 |
|
| 3 |
const fs = require("fs"); |
| 4 |
const { addElectronSupportFromChromium } = require("./chromium-to-electron"); |
| 5 |
|
| 6 |
const envs = require("../build/compat-table/environments"); |
| 7 |
const parseEnvsVersions = require("../build/compat-table/build-utils/parse-envs-versions"); |
| 8 |
const interpolateAllResults = require("../build/compat-table/build-utils/interpolate-all-results"); |
| 9 |
const compareVersions = require("../build/compat-table/build-utils/compare-versions"); |
| 10 |
const legacyPluginAliases = require("./data/legacy-plugin-aliases"); |
| 11 |
|
| 12 |
const envsVersions = parseEnvsVersions(envs); |
| 13 |
|
| 14 |
const compatSources = ["es5", "es6", "es2016plus", "esnext"].map(source => { |
| 15 |
const data = require(`../build/compat-table/data-${source}`); |
| 16 |
interpolateAllResults(data.tests, envs); |
| 17 |
return data; |
| 18 |
}); |
| 19 |
|
| 20 |
// End of compat-table code adaptation |
| 21 |
|
| 22 |
exports.environments = [ |
| 23 |
"chrome", |
| 24 |
"opera", |
| 25 |
"edge", |
| 26 |
"firefox", |
| 27 |
"safari", |
| 28 |
"node", |
| 29 |
"deno", |
| 30 |
"ie", |
| 31 |
"android", |
| 32 |
"ios", |
| 33 |
"phantom", |
| 34 |
"samsung", |
| 35 |
"rhino", |
| 36 |
]; |
| 37 |
|
| 38 |
const compatibilityTests = compatSources.flatMap(data => |
| 39 |
data.tests.flatMap(test => { |
| 40 |
if (!test.subtests) return test; |
| 41 |
|
| 42 |
return test.subtests.map(subtest => |
| 43 |
Object.assign({}, subtest, { |
| 44 |
name: test.name + " / " + subtest.name, |
| 45 |
group: test.name, |
| 46 |
}) |
| 47 |
); |
| 48 |
}) |
| 49 |
); |
| 50 |
|
| 51 |
const getLowestImplementedVersion = ( |
| 52 |
{ features }, |
| 53 |
env, |
| 54 |
exclude = () => false |
| 55 |
) => { |
| 56 |
const tests = compatibilityTests.filter(test => { |
| 57 |
// TODO (Babel 9): Use ||=, &&= |
| 58 |
let ok = features.includes(test.name); |
| 59 |
ok = ok || (test.group && features.includes(test.group)); |
| 60 |
ok = ok || (features.length === 1 && test.name.startsWith(features[0])); |
| 61 |
ok = ok && !exclude(test.name); |
| 62 |
return ok; |
| 63 |
}); |
| 64 |
|
| 65 |
const envTests = tests.map(({ res }) => { |
| 66 |
const versions = envsVersions[env]; |
| 67 |
let i = versions.length - 1; |
| 68 |
|
| 69 |
// Find the last not-implemented version |
| 70 |
for (; i >= 0; i--) { |
| 71 |
const { id } = versions[i]; |
| 72 |
// Babel assumes strict mode |
| 73 |
if (res[id] !== true && res[id] !== "strict") { |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return envsVersions[env][i + 1]; |
| 79 |
}); |
| 80 |
|
| 81 |
if (envTests.length === 0 || envTests.some(t => !t)) return null; |
| 82 |
|
| 83 |
const result = envTests.reduce((a, b) => { |
| 84 |
return compareVersions(a, b) > 0 ? a : b; |
| 85 |
}); |
| 86 |
|
| 87 |
// NOTE(bng): A number of environments in compat-table changed to |
| 88 |
// include a trailing zero (node10 -> node10_0), so for now stripping |
| 89 |
// it to be consistent |
| 90 |
return result.version.join(".").replace(/\.0$/, ""); |
| 91 |
}; |
| 92 |
|
| 93 |
const expandFeatures = features => |
| 94 |
features.flatMap(feat => { |
| 95 |
if (feat.includes("/")) return [feat]; |
| 96 |
return compatibilityTests |
| 97 |
.map(test => test.name) |
| 98 |
.filter(name => name === feat || name.startsWith(feat + " / ")); |
| 99 |
}); |
| 100 |
|
| 101 |
exports.generateData = (environments, features) => { |
| 102 |
const data = {}; |
| 103 |
|
| 104 |
const normalized = {}; |
| 105 |
for (const [key, options] of Object.entries(features)) { |
| 106 |
if (!options.features) { |
| 107 |
normalized[key] = { |
| 108 |
features: expandFeatures([options]), |
| 109 |
}; |
| 110 |
} else { |
| 111 |
normalized[key] = { |
| 112 |
...options, |
| 113 |
features: expandFeatures(options.features), |
| 114 |
}; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
const overlapping = {}; |
| 119 |
|
| 120 |
// Apply bugfixes |
| 121 |
for (const [key, { features, replaces }] of Object.entries(normalized)) { |
| 122 |
if (replaces) { |
| 123 |
if (normalized[replaces].replaces) { |
| 124 |
throw new Error("Transitive replacement is not supported"); |
| 125 |
} |
| 126 |
normalized[replaces].features = normalized[replaces].features.filter( |
| 127 |
feat => !features.includes(feat) |
| 128 |
); |
| 129 |
|
| 130 |
if (!overlapping[replaces]) overlapping[replaces] = []; |
| 131 |
overlapping[replaces].push(key); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// eslint-disable-next-line prefer-const |
| 136 |
for (let [key, options] of Object.entries(normalized)) { |
| 137 |
const plugin = {}; |
| 138 |
|
| 139 |
environments.forEach(env => { |
| 140 |
const version = getLowestImplementedVersion(options, env); |
| 141 |
if (version) plugin[env] = version; |
| 142 |
}); |
| 143 |
addElectronSupportFromChromium(plugin); |
| 144 |
|
| 145 |
data[key] = plugin; |
| 146 |
} |
| 147 |
|
| 148 |
return { data, overlapping }; |
| 149 |
}; |
| 150 |
|
| 151 |
exports.writeFile = function (data, dataPath, name) { |
| 152 |
const stringified = JSON.stringify(data, null, 2) + "\n"; |
| 153 |
if (process.env.CHECK_COMPAT_DATA) { |
| 154 |
const currentData = fs.readFileSync(dataPath, "utf8"); |
| 155 |
|
| 156 |
// Compare as JSON strings to also check keys ordering |
| 157 |
if (currentData !== stringified) { |
| 158 |
console.error( |
| 159 |
`The newly generated ${name} data does not match the current ` + |
| 160 |
"files. Re-run `make build-compat-data`." |
| 161 |
); |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
} else { |
| 166 |
fs.writeFileSync(dataPath, stringified); |
| 167 |
} |
| 168 |
return true; |
| 169 |
}; |
| 170 |
|
| 171 |
// TODO(Babel 8): Remove this. |
| 172 |
exports.defineLegacyPluginAliases = function (data) { |
| 173 |
// We create a new object to inject legacy aliases in the correct |
| 174 |
// order, rather than all at the end. |
| 175 |
const result = {}; |
| 176 |
for (const key in data) { |
| 177 |
result[key] = data[key]; |
| 178 |
if (key in legacyPluginAliases) { |
| 179 |
result[legacyPluginAliases[key]] = data[key]; |
| 180 |
} |
| 181 |
} |
| 182 |
return result; |
| 183 |
}; |