MCPcopy Create free account
hub / github.com/ampproject/amphtml / checkSourcemapMappings

Function checkSourcemapMappings

build-system/tasks/check-sourcemaps.js:107–152  ·  view source on GitHub ↗

* Performs a sanity check on the mappings field in the sourcemap file. * * Today, the first line of amp.js after resolving imports comes from * src/polyfills/array-includes.js. (The import chain is src/amp.js -> * src/polyfills/index.js -> src/polyfills/array-includes.js.) This sequence * chang

(sourcemapJson, map)

Source from the content-addressed store, hash-verified

105 * @param {string} map The map filepath to check
106 */
107function checkSourcemapMappings(sourcemapJson, map) {
108 log('Inspecting', cyan('mappings'), 'in', cyan(map) + '...');
109 if (!sourcemapJson.mappings) {
110 log(red('ERROR:'), 'Could not find', cyan('mappings'));
111 throw new Error('Could not find mappings array');
112 }
113
114 // See https://www.npmjs.com/package/sourcemap-codec#usage.
115 const firstLineMapping = decode(sourcemapJson.mappings)[1][0];
116 const [, sourceIndex = 0, sourceCodeLine = 0, sourceCodeColumn] =
117 firstLineMapping;
118
119 const firstLineFile = sourcemapJson.sources[sourceIndex];
120 const contents = fs.readFileSync(firstLineFile, 'utf8').split('\n');
121 const firstLineCode = contents[sourceCodeLine].slice(sourceCodeColumn);
122 const helpMessage =
123 'If this change is intentional, update the mapping related constants in ' +
124 cyan('build-system/tasks/check-sourcemaps.js') +
125 '.';
126
127 // Mapping related constants
128 const expectedFirstLine = map.includes('mjs')
129 ? {
130 file: 'src/polyfills/abort-controller.js',
131 code: 'class AbortController {',
132 }
133 : {
134 file: 'node_modules/@babel/runtime/helpers/esm/createClass.js',
135 code: 'function _defineProperties(target, props) {',
136 };
137
138 if (firstLineFile != expectedFirstLine.file) {
139 log(red('ERROR:'), 'Found mapping for incorrect file.');
140 log('Actual:', cyan(firstLineFile));
141 log('Expected:', cyan(expectedFirstLine.file));
142 log(helpMessage);
143 throw new Error('Found mapping for incorrect file');
144 }
145 if (firstLineCode != expectedFirstLine.code) {
146 log(red('ERROR:'), 'Found mapping for incorrect code.');
147 log('Actual:', cyan(firstLineCode));
148 log('Expected:', cyan(expectedFirstLine.code));
149 log(helpMessage);
150 throw new Error('Found mapping for incorrect code');
151 }
152}
153
154/**
155 * @param {string} map The map filepath to check

Callers 1

checkSourceMapFunction · 0.85

Calls 4

cyanFunction · 0.85
redFunction · 0.85
logFunction · 0.50
decodeFunction · 0.50

Tested by

no test coverage detected