summaryrefslogtreecommitdiffstats
path: root/devtools/shared/compatibility/bin/update.js
blob: 4caa1efe01360dcf62faa0416944434cb0f6d41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

// The Compatibility panel detects issues by comparing against official MDN compatibility data
// at https://github.com/mdn/browser-compat-data). It uses a local snapshot of the dataset.
// This dataset needs to be manually synchronized periodically

// The subsets from the dataset required by the Compatibility panel are:
// * css.properties: https://github.com/mdn/browser-compat-data/tree/master/css

// The MDN compatibility data is available as a node package ("@mdn/browser-compat-data"),
// which is used here to update `../dataset/css-properties.json`.

/* eslint-disable mozilla/reject-relative-requires */

"use strict";

const compatData = require("@mdn/browser-compat-data");
const { properties } = compatData.css;

const { TARGET_BROWSER_ID } = require("../constants.js");
const { getCompatTable } = require("../helpers.js");

// Flatten all CSS properties aliases here so we don't have to do it at runtime,
// which is costly.
flattenAliases(properties);
parseBrowserVersion(properties);
removeUnusedData(properties);
exportData(properties, "css-properties.json");

/**
 * Builds a list of aliases between CSS properties, like flex and -webkit-flex,
 * and mutates individual entries in the web compat data store for CSS properties to
 * add their corresponding aliases.
 */
function flattenAliases(compatNode) {
  for (const term in compatNode) {
    if (term.startsWith("_")) {
      // Ignore exploring if the term is _aliasOf or __compat.
      continue;
    }

    const compatTable = getCompatTable(compatNode, [term]);
    if (compatTable) {
      const aliases = findAliasesFrom(compatTable);

      for (const { alternative_name: name, prefix } of aliases) {
        const alias = name || prefix + term;
        compatNode[alias] = { _aliasOf: term };
      }

      if (aliases.length) {
        // Make the term accessible as the alias.
        compatNode[term]._aliasOf = term;
      }
    }

    // Flatten deeper node as well.
    flattenAliases(compatNode[term]);
  }
}

function findAliasesFrom(compatTable) {
  const aliases = [];

  for (const browser in compatTable.support) {
    let supportStates = compatTable.support[browser] || [];
    supportStates = Array.isArray(supportStates)
      ? supportStates
      : [supportStates];

    for (const { alternative_name: name, prefix } of supportStates) {
      if (!prefix && !name) {
        continue;
      }

      aliases.push({ alternative_name: name, prefix });
    }
  }

  return aliases;
}

function parseBrowserVersion(compatNode) {
  for (const term in compatNode) {
    if (term.startsWith("_")) {
      // Ignore exploring if the term is _aliasOf or __compat.
      continue;
    }

    const compatTable = getCompatTable(compatNode, [term]);
    if (compatTable?.support) {
      for (const [browserId, supportItem] of Object.entries(
        compatTable.support
      )) {
        // supportItem is an array when there are info for both prefixed and non-prefixed
        // versions. If it's not an array in the original data, transform it into one
        // since we'd have to do it in MDNCompatibility at runtime otherwise.
        if (!Array.isArray(supportItem)) {
          compatTable.support[browserId] = [supportItem];
        }
        for (const item of compatTable.support[browserId]) {
          replaceVersionsInBrowserSupport(item);
        }
      }
    }

    // Handle deeper node as well.
    parseBrowserVersion(compatNode[term]);
  }
}

function replaceVersionsInBrowserSupport(browserSupport) {
  browserSupport.added = asFloatVersion(browserSupport.version_added);
  browserSupport.removed = asFloatVersion(browserSupport.version_removed);
}

function asFloatVersion(version) {
  // `version` is not always a string (can be null, or a boolean) and in such case,
  // we want to keep it that way.
  if (typeof version !== "string") {
    return version;
  }

  if (version.startsWith("\u2264")) {
    // MDN compatibility data started to use an expression like "≤66" for version.
    // We just ignore the character here.
    version = version.substring(1);
  }

  return parseFloat(version);
}

/**
 * Remove all unused data from the file so it's smaller and faster to load
 */
function removeUnusedData(compatNode) {
  for (const term in compatNode) {
    if (term.startsWith("_")) {
      // Ignore exploring if the term is _aliasOf or __compat.
      continue;
    }

    const compatTable = getCompatTable(compatNode, [term]);

    // A term may only have a `_aliasOf` property (e.g. for word-wrap), so we don't have
    // compat data in it directly.
    if (compatTable) {
      // source_file references the name of the file in the MDN compat data repo where the
      // property is handled. We don't make use of it so we can remove it.
      delete compatTable.source_file;

      // Not used at the moment. Doesn't seem to have much information anyway
      delete compatTable.description;

      if (compatTable?.support) {
        for (const [browserId, supportItem] of Object.entries(
          compatTable.support
        )) {
          // Remove any browser we won't handle
          if (!TARGET_BROWSER_ID.includes(browserId)) {
            delete compatTable.support[browserId];
            continue;
          }

          // Remove `version_added` and `version_removed`, that are parsed in `replaceVersionsInBrowserSupport`
          // and which we don't need anymore.
          for (const item of supportItem) {
            delete item.version_added;
            delete item.version_removed;
            // Those might be interesting, but we're not using them at the moment, so let's
            // remove them as they can be quite lengthy
            delete item.notes;
          }
        }
      }
    }

    // Handle deeper node as well.
    removeUnusedData(compatNode[term]);
  }
}

function exportData(data, fileName) {
  const fs = require("fs");
  const path = require("path");

  const content = `${JSON.stringify(data)}`;

  fs.writeFile(
    path.resolve(__dirname, "../dataset", fileName),
    content,
    err => {
      if (err) {
        console.error(err);
      } else {
        console.log(`${fileName} downloaded`);
      }
    }
  );
}