Skip to content

Commit

Permalink
Debugging utils WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 21, 2020
1 parent 2d152f8 commit 53b0c17
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
coverage

example/build
example/intermediate
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
node_modules

example/build
example/intermediate
16 changes: 16 additions & 0 deletions lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* to insert tracking code.
* ------------------*/

/* eeslint-disable import/no-extraneous-dependencies */
/* eeslint-disable node/no-extraneous-require */
/* eslint-disable global-require */

'use strict';

// Catalog globals etc
Expand Down Expand Up @@ -114,10 +118,22 @@ function register(options) {
}
});

const {dirname, join: pathJoin, sep: pathSep} = require('path'),
{writeFileSync} = require('fs'),
{ensureDirSync} = require('fs-extra');
const rootDir = pathJoin(__dirname, '..', pathSep);

// Add pirates hook to capture code post-babel
if (piratesRevert) piratesRevert();
piratesRevert = addHook((code, filename) => {
transpiledFiles[filename] = parseSourceMapFromCode(code);

if (filename.startsWith(rootDir)) {
const writePath = pathJoin(rootDir, 'example/intermediate', filename.slice(rootDir.length));
ensureDirSync(dirname(writePath));
writeFileSync(writePath, code);
}

return code;
}, {ignoreNodeModules: false});
}
Expand Down
12 changes: 12 additions & 0 deletions lib/serialize/records.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ function updateBlockParent(block, parent) {
}
}

// TODO Delete this
function traceBlock(name, block) {
console.log(`${name}.id:`, block.id); // eslint-disable-line no-console
if (block.parent) traceBlock(`${name}.parent`, block.parent); // eslint-disable-line no-unused-vars
}

/**
* Create scope object
* @param {number} [id] - Scope ID
Expand Down Expand Up @@ -178,6 +184,12 @@ function updateScopeParent(scope, parent) {
}
}

// TODO Delete this
function traceScope(name, scope) {
console.log(`${name}.block.id:`, scope.block.id); // eslint-disable-line no-console
if (scope.parent) traceScope(`${name}.parent`, scope.parent); // eslint-disable-line no-unused-vars
}

/**
* Create dependency relationship between two values.
* @param {Object} srcRecord - Record for value which depends on the other
Expand Down
34 changes: 34 additions & 0 deletions lib/serialize/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,40 @@ class Serializer {
importNodes = [],
{mangle} = this.options;

/*
// TODO Delete this
const found = new Set();
function findCircularDependency(record, stack) { // eslint-disable-line no-shadow
if (found.has(record)) return;
const newStack = new Set(stack);
newStack.add(record);
// console.log('stack:', [...newStack].map(rec => rec.varNode.name));
if (stack.has(record)) {
// eslint-disable-next-line no-console
console.log('circular:', [...newStack].map(rec => rec.varNode.name));
process.exit(); // eslint-disable-line no-process-exit
}
for (const dependency of record.dependencies) {
findCircularDependency(dependency.record, newStack);
}
found.add(record);
if (record.assignments) {
for (const assignment of record.assignments) {
for (const dependency of assignment.dependencies) {
findCircularDependency(dependency.record, new Set());
}
}
}
}
findCircularDependency(record, new Set());
*/

const queue = [record],
processing = new Map(); // Keyed by record, values = `true` for processed, `false` for processing
function processQueue() {
Expand Down
41 changes: 41 additions & 0 deletions validateSourcemaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable no-console */

'use strict';

const pathJoin = require('path').join,
{readFile} = require('fs').promises, // eslint-disable-line node/no-unsupported-features/node-builtins
readdir = require('recursive-readdir'),
validate = require('sourcemap-validator');

const dirPath = pathJoin(__dirname, 'example/intermediate/example');
// const dirPath = pathJoin(__dirname, 'node_modules/is-it-type/dist');

(async () => {
const paths = await readdir(dirPath);

for (const path of paths) {
if (!path.match(/\.js$/)) continue;

const code = await readFile(path, 'utf8');

let map;
const mapPath = `${path}.map`;
try {
map = await readFile(mapPath, 'utf8');
} catch (err) {} // eslint-disable-line no-empty

const pathShort = path.slice(dirPath.length);

try {
validate(code, map);
console.log('VALID:', pathShort);
} catch (err) {
if (err.message === 'No map argument provided, and no inline sourcemap found') {
console.log('NO SOURCE MAP:', pathShort);
} else {
console.log('INVALID:', pathShort, err.message);
}
// console.log(path, err.message);
}
}
})();

0 comments on commit 53b0c17

Please sign in to comment.