Rollup merge of #87941 - GuillaumeGomez:fix-rustdoc-js-tool, r=notriddle

Fix/improve rustdoc-js tool

This tool is run when testing `src/test/rustdoc-js*`.

r? `@notriddle`
This commit is contained in:
Guillaume Gomez 2021-08-12 13:25:08 +02:00 committed by GitHub
commit faf7fb94f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,15 +20,17 @@ function getNextStep(content, pos, stop) {
// will blow up. Template strings are not tested and might also be // will blow up. Template strings are not tested and might also be
// broken. // broken.
function extractFunction(content, functionName) { function extractFunction(content, functionName) {
var indent = 0; var level = 0;
var splitter = "function " + functionName + "("; var splitter = "function " + functionName + "(";
var stop;
var pos, start;
while (true) { while (true) {
var start = content.indexOf(splitter); start = content.indexOf(splitter);
if (start === -1) { if (start === -1) {
break; break;
} }
var pos = start; pos = start;
while (pos < content.length && content[pos] !== ')') { while (pos < content.length && content[pos] !== ')') {
pos += 1; pos += 1;
} }
@ -44,30 +46,33 @@ function extractFunction(content, functionName) {
} }
while (pos < content.length) { while (pos < content.length) {
// Eat single-line comments // Eat single-line comments
if (content[pos] === '/' && pos > 0 && content[pos-1] === '/') { if (content[pos] === '/' && pos > 0 && content[pos - 1] === '/') {
do { do {
pos += 1; pos += 1;
} while (pos < content.length && content[pos] !== '\n'); } while (pos < content.length && content[pos] !== '\n');
// Eat multiline comment.
} else if (content[pos] === '*' && pos > 0 && content[pos - 1] === '/') {
do {
pos += 1;
} while (pos < content.length && content[pos] !== '/' && content[pos - 1] !== '*');
// Eat quoted strings // Eat quoted strings
} else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") { } else if (content[pos] === '"' || content[pos] === "'" || content[pos] === "`") {
var stop = content[pos]; stop = content[pos];
var is_escaped = false;
do { do {
if (content[pos] === '\\') { if (content[pos] === '\\') {
pos += 2;
} else {
pos += 1; pos += 1;
} }
} while (pos < content.length && pos += 1;
(content[pos] !== stop || content[pos - 1] === '\\')); } while (pos < content.length && content[pos] !== stop);
// Otherwise, check for indent // Otherwise, check for block level.
} else if (content[pos] === '{') { } else if (content[pos] === '{') {
indent += 1; level += 1;
} else if (content[pos] === '}') { } else if (content[pos] === '}') {
indent -= 1; level -= 1;
if (indent === 0) { if (level === 0) {
return content.slice(start, pos + 1); return content.slice(start, pos + 1);
} }
} }