first commit
Some checks failed
Types tests / Test (lts/*) (push) Has been cancelled
Lint / Lint (lts/*) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / Test (20) (push) Has been cancelled
CI / Test (22) (push) Has been cancelled
CI / Test (24) (push) Has been cancelled

This commit is contained in:
2025-10-03 22:20:19 +08:00
commit 44db9807a1
2172 changed files with 526822 additions and 0 deletions

View File

@@ -0,0 +1,302 @@
import { types as t, transformSync } from "@babel/core";
import vm from "vm";
const PDFJS_PREPROCESSOR_NAME = "PDFJSDev";
function isPDFJSPreprocessor(obj) {
return obj.type === "Identifier" && obj.name === PDFJS_PREPROCESSOR_NAME;
}
function evalWithDefines(code, defines) {
if (!code?.trim()) {
throw new Error("No JavaScript expression given");
}
return vm.runInNewContext(code, defines, { displayErrors: false });
}
function handlePreprocessorAction(ctx, actionName, args, path) {
try {
const arg = args[0];
switch (actionName) {
case "test":
if (!t.isStringLiteral(arg)) {
throw new Error("No code for testing is given");
}
return !!evalWithDefines(arg.value, ctx.defines);
case "eval":
if (!t.isStringLiteral(arg)) {
throw new Error("No code for eval is given");
}
const result = evalWithDefines(arg.value, ctx.defines);
if (
typeof result === "boolean" ||
typeof result === "string" ||
typeof result === "number" ||
typeof result === "object"
) {
return result;
}
break;
}
throw new Error("Unsupported action");
} catch (e) {
throw path.buildCodeFrameError(
`Could not process ${PDFJS_PREPROCESSOR_NAME}.${actionName}: ${e.message}`
);
}
}
function babelPluginPDFJSPreprocessor(babel, ctx) {
function removeUnusedFunctions(path) {
let removed;
do {
removed = false;
path.scope.crawl();
for (const name in path.scope.bindings) {
const binding = path.scope.bindings[name];
if (!binding.referenced) {
const { path: bindingPath } = binding;
if (bindingPath.isFunctionDeclaration()) {
bindingPath.remove();
removed = true;
}
}
}
// If we removed some functions, there might be new unused ones
} while (removed);
}
return {
name: "babel-plugin-pdfjs-preprocessor",
manipulateOptions({ parserOpts }) {
parserOpts.attachComment = false;
},
visitor: {
"ExportNamedDeclaration|ImportDeclaration": ({ node }) => {
if (node.source && ctx.map?.[node.source.value]) {
node.source.value = ctx.map[node.source.value];
}
},
"IfStatement|ConditionalExpression": {
exit(path) {
const { node } = path;
if (t.isBooleanLiteral(node.test)) {
// if (true) stmt1; => stmt1
// if (false) stmt1; else stmt2; => stmt2
if (node.test.value === true) {
path.replaceWith(node.consequent);
} else if (node.alternate) {
path.replaceWith(node.alternate);
} else {
path.remove(node);
}
}
},
},
UnaryExpression: {
exit(path) {
const { node } = path;
if (
node.operator === "typeof" &&
isPDFJSPreprocessor(node.argument)
) {
// typeof PDFJSDev => 'object'
path.replaceWith(t.stringLiteral("object"));
return;
}
if (node.operator === "!" && t.isBooleanLiteral(node.argument)) {
// !true => false, !false => true
path.replaceWith(t.booleanLiteral(!node.argument.value));
}
},
},
LogicalExpression: {
exit(path) {
const { node } = path;
if (!t.isBooleanLiteral(node.left)) {
return;
}
switch (node.operator) {
case "&&":
// true && expr => expr
// false && expr => false
path.replaceWith(
node.left.value === true ? node.right : node.left
);
break;
case "||":
// true || expr => true
// false || expr => expr
path.replaceWith(
node.left.value === true ? node.left : node.right
);
break;
}
},
},
BinaryExpression: {
exit(path) {
const { node } = path;
switch (node.operator) {
case "==":
case "===":
case "!=":
case "!==":
if (t.isLiteral(node.left) && t.isLiteral(node.right)) {
// folding == and != check that can be statically evaluated
const { confident, value } = path.evaluate();
if (confident) {
path.replaceWith(t.booleanLiteral(value));
}
}
}
},
},
CallExpression(path) {
const { node } = path;
if (
t.isMemberExpression(node.callee) &&
isPDFJSPreprocessor(node.callee.object) &&
t.isIdentifier(node.callee.property) &&
!node.callee.computed
) {
// PDFJSDev.xxxx(arg1, arg2, ...) => transform
const action = node.callee.property.name;
const result = handlePreprocessorAction(
ctx,
action,
node.arguments,
path
);
path.replaceWith(t.inherits(t.valueToNode(result), path.node));
}
if (t.isIdentifier(node.callee, { name: "__raw_import__" })) {
if (node.arguments.length !== 1) {
throw new Error("Invalid `__raw_import__` usage.");
}
// Replace it with a standard `import`-call and attempt to ensure that
// various bundlers will leave it alone; this *must* include Webpack.
const source = node.arguments[0];
source.leadingComments = [
{
type: "CommentBlock",
value: "webpackIgnore: true",
},
{
type: "CommentBlock",
value: "@vite-ignore",
},
];
path.replaceWith(t.importExpression(source));
}
},
"BlockStatement|StaticBlock": {
// Visit node in post-order so that recursive flattening
// of blocks works correctly.
exit(path) {
const { node } = path;
let subExpressionIndex = 0;
while (subExpressionIndex < node.body.length) {
switch (node.body[subExpressionIndex].type) {
case "EmptyStatement":
// Removing empty statements from the blocks.
node.body.splice(subExpressionIndex, 1);
continue;
case "BlockStatement":
// Block statements inside a block are flattened
// into the parent one.
const subChildren = node.body[subExpressionIndex].body;
node.body.splice(subExpressionIndex, 1, ...subChildren);
subExpressionIndex += Math.max(subChildren.length - 1, 0);
continue;
case "ReturnStatement":
case "ThrowStatement":
// Removing dead code after return or throw.
node.body.splice(
subExpressionIndex + 1,
node.body.length - subExpressionIndex - 1
);
break;
}
subExpressionIndex++;
}
if (node.type === "StaticBlock" && node.body.length === 0) {
path.remove();
}
},
},
Function: {
exit(path) {
if (!t.isBlockStatement(path.node.body)) {
// Arrow function with expression body
return;
}
const { body } = path.node.body;
if (
body.length > 0 &&
t.isReturnStatement(body.at(-1), { argument: null })
) {
// Function body ends with return without arg -- removing it.
body.pop();
}
removeUnusedFunctions(path);
},
},
ClassMethod: {
exit(path) {
const {
node,
parentPath: { parent: classNode },
} = path;
if (
// Remove empty constructors. We only do this for
// base classes, as the default constructor of derived
// classes is not empty (and an empty constructor
// must throw at runtime when constructed).
node.kind === "constructor" &&
node.body.body.length === 0 &&
node.params.every(p => p.type === "Identifier") &&
!classNode.superClass
) {
path.remove();
}
},
},
Program: {
exit(path) {
if (path.node.sourceType === "module") {
removeUnusedFunctions(path);
}
},
},
MemberExpression(path) {
// The Emscripten Compiler (emcc) generates code that allows the caller
// to provide the Wasm module (thorugh Module.instantiateWasm), with
// a fallback in case .instantiateWasm is not provided.
// We always define instantiateWasm, so we can hard-code the check
// and let our dead code elimination logic remove the unused fallback.
if (
path.parentPath.isIfStatement({ test: path.node }) &&
path.matchesPattern("Module.instantiateWasm")
) {
path.replaceWith(t.booleanLiteral(true));
}
},
},
};
}
function preprocessPDFJSCode(ctx, content) {
return transformSync(content, {
configFile: false,
plugins: [[babelPluginPDFJSPreprocessor, ctx]],
}).code;
}
export { babelPluginPDFJSPreprocessor, preprocessPDFJSCode };

232
external/builder/builder.mjs vendored Normal file
View File

@@ -0,0 +1,232 @@
import fs from "fs";
import path from "path";
import vm from "vm";
const AllWhitespaceRegexp = /^\s+$/g;
/**
* A simple preprocessor that is based on the Firefox preprocessor
* (https://dxr.mozilla.org/mozilla-central/source/build/docs/preprocessor.rst).
* The main difference is that this supports a subset of the commands and it
* supports preprocessor commands in HTML-style comments.
*
* Currently supported commands:
* - if
* - elif
* - else
* - endif
* - include
* - expand
* - error
*
* Every #if must be closed with an #endif. Nested conditions are supported.
*
* Within an #if or #else block, one level of comment tokens is stripped. This
* allows us to write code that can run even without preprocessing. For example:
*
* //#if SOME_RARE_CONDITION
* // // Decrement by one
* // --i;
* //#else
* // // Increment by one.
* ++i;
* //#endif
*/
function preprocess(inFilename, outFilename, defines) {
let lineNumber = 0;
function loc() {
return fs.realpathSync(inFilename) + ":" + lineNumber;
}
function expandCssImports(content, baseUrl) {
return content.replaceAll(
/^\s*@import\s+url\(([^)]+)\);\s*$/gm,
function (all, url) {
if (defines.GECKOVIEW) {
switch (url) {
case "annotation_editor_layer_builder.css":
return "";
}
}
const file = path.join(path.dirname(baseUrl), url);
const imported = fs.readFileSync(file, "utf8").toString();
return expandCssImports(imported, file);
}
);
}
// TODO make this really read line by line.
let content = fs.readFileSync(inFilename, "utf8").toString();
// Handle CSS-imports first, when necessary.
if (/\.css$/i.test(inFilename)) {
content = expandCssImports(content, inFilename);
}
const lines = content.split("\n"),
totalLines = lines.length;
const out = [];
let i = 0;
function readLine() {
if (i < totalLines) {
return lines[i++];
}
return null;
}
const writeLine =
typeof outFilename === "function"
? outFilename
: function (line) {
if (!line || AllWhitespaceRegexp.test(line)) {
const prevLine = out.at(-1);
if (!prevLine || AllWhitespaceRegexp.test(prevLine)) {
return; // Avoid adding consecutive blank lines.
}
}
out.push(line);
};
function evaluateCondition(code) {
if (!code?.trim()) {
throw new Error("No JavaScript expression given at " + loc());
}
try {
return vm.runInNewContext(code, defines, { displayErrors: false });
} catch (e) {
throw new Error(
'Could not evaluate "' +
code +
'" at ' +
loc() +
"\n" +
e.name +
": " +
e.message
);
}
}
function include(file) {
const realPath = fs.realpathSync(inFilename);
const dir = path.dirname(realPath);
try {
let fullpath;
if (file.indexOf("$ROOT/") === 0) {
fullpath = path.join(
__dirname,
"../..",
file.substring("$ROOT/".length)
);
} else {
fullpath = path.join(dir, file);
}
preprocess(fullpath, writeLine, defines);
} catch (e) {
if (e.code === "ENOENT") {
throw new Error('Failed to include "' + file + '" at ' + loc());
}
throw e; // Some other error
}
}
function expand(line) {
line = line.replaceAll(/__[\w]+__/g, function (variable) {
variable = variable.substring(2, variable.length - 2);
if (variable in defines) {
return defines[variable];
}
return "";
});
writeLine(line);
}
// not inside if or else (process lines)
const STATE_NONE = 0;
// inside if, condition false (ignore until #else or #endif)
const STATE_IF_FALSE = 1;
// inside else, #if was false, so #else is true (process lines until #endif)
const STATE_ELSE_TRUE = 2;
// inside if, condition true (process lines until #else or #endif)
const STATE_IF_TRUE = 3;
// inside else or elif, #if/#elif was true, so following #else or #elif is
// false (ignore lines until #endif)
const STATE_ELSE_FALSE = 4;
let line;
let state = STATE_NONE;
const stack = [];
const control =
/^(?:\/\/|\s*\/\*|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:\*\/|-->)?$)?/;
while ((line = readLine()) !== null) {
++lineNumber;
const m = control.exec(line);
if (m) {
switch (m[1]) {
case "if":
stack.push(state);
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
break;
case "elif":
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
state = STATE_ELSE_FALSE;
} else if (state === STATE_IF_FALSE) {
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
} else if (state === STATE_ELSE_TRUE) {
throw new Error("Found #elif after #else at " + loc());
} else {
throw new Error("Found #elif without matching #if at " + loc());
}
break;
case "else":
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
state = STATE_ELSE_FALSE;
} else if (state === STATE_IF_FALSE) {
state = STATE_ELSE_TRUE;
} else {
throw new Error("Found #else without matching #if at " + loc());
}
break;
case "endif":
if (state === STATE_NONE) {
throw new Error("Found #endif without #if at " + loc());
}
state = stack.pop();
break;
case "expand":
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
expand(m[2]);
}
break;
case "include":
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
include(m[2]);
}
break;
case "error":
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
throw new Error("Found #error " + m[2] + " at " + loc());
}
break;
}
} else if (state === STATE_NONE) {
writeLine(line);
} else if (
(state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
!stack.includes(STATE_IF_FALSE) &&
!stack.includes(STATE_ELSE_FALSE)
) {
writeLine(
line
.replaceAll(/^\/\/|^<!--/g, " ")
.replaceAll(/(^\s*)\/\*/g, "$1 ")
.replaceAll(/\*\/$|-->$/g, "")
);
}
}
if (state !== STATE_NONE || stack.length !== 0) {
throw new Error(
"Missing #endif in preprocessor for " + fs.realpathSync(inFilename)
);
}
if (typeof outFilename !== "function") {
fs.writeFileSync(outFilename, out.join("\n"));
}
}
export { preprocess };

View File

@@ -0,0 +1,4 @@
'use strict';
var i = 0;
while(i-->0) {
}

View File

@@ -0,0 +1,6 @@
'use strict';
//#if TRUE
var i = 0;
while(i-->0) {
}
//#endif

View File

@@ -0,0 +1,5 @@
/* Comment here... */
div {
margin: 0;
padding: 0;
}

View File

@@ -0,0 +1,7 @@
/* Comment here... */
/*#if TRUE*/
/*div {*/
/* margin: 0;*/
/*padding: 0;*/
/*}*/
/*#endif*/

View File

@@ -0,0 +1 @@
//Error: Found #elif without matching #if at __filename:2

4
external/builder/fixtures/elif.js vendored Normal file
View File

@@ -0,0 +1,4 @@
'use strict';
//#elif TRUE
var a;
//#endif

View File

@@ -0,0 +1 @@
//Error: Found #else without matching #if at __filename:2

3
external/builder/fixtures/else.js vendored Normal file
View File

@@ -0,0 +1,3 @@
'use strict';
//#else
//#endif

View File

@@ -0,0 +1 @@
//Error: Found #error "Some Error" at __filename:3

View File

@@ -0,0 +1,2 @@
'use strict';
var a;

View File

@@ -0,0 +1,5 @@
'use strict';
//#if FALSE
//#error "Some Error"
//#endif
var a;

5
external/builder/fixtures/error.js vendored Normal file
View File

@@ -0,0 +1,5 @@
'use strict';
//#if TRUE
//#error "Some Error"
//#endif
var b;

View File

@@ -0,0 +1 @@
prefixtruesuffix

1
external/builder/fixtures/expand.html vendored Normal file
View File

@@ -0,0 +1 @@
<!--#expand prefix__TRUE__suffix-->

View File

@@ -0,0 +1 @@
//Error: No JavaScript expression given at __filename:2

6
external/builder/fixtures/if-empty.js vendored Normal file
View File

@@ -0,0 +1,6 @@
'use strict';
//#if
var a;
//#else
var b;
//#endif

View File

@@ -0,0 +1,2 @@
'use strict';
var c;

View File

@@ -0,0 +1,8 @@
'use strict';
//#if FALSE
var a;
//#elif FALSE
var b;
//#else
var c;
//#endif

View File

@@ -0,0 +1,2 @@
'use strict';
var b;

View File

@@ -0,0 +1,8 @@
'use strict';
//#if FALSE
var a;
//#elif TRUE
var b;
//#else
var c;
//#endif

View File

@@ -0,0 +1,2 @@
'use strict';
var b;

View File

@@ -0,0 +1,6 @@
'use strict';
//#if FALSE
var a;
//#else
var b;
//#endif

View File

@@ -0,0 +1,3 @@
div {
margin: 0;
}

View File

@@ -0,0 +1,6 @@
'use strict';
var a;
var b;
var d;

14
external/builder/fixtures/if-nested.css vendored Normal file
View File

@@ -0,0 +1,14 @@
/*#if TRUE*/
div {
margin: 0;
/*#if FALSE*/
padding: 0;
/*#endif*/
}
/*#endif*/
/*#if FALSE*/
p {
margin: 0;
}
/*#endif*/

19
external/builder/fixtures/if-nested.js vendored Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
//#if TRUE
var a;
//#if TRUE
var b;
//#else
var c;
//#endif
var d;
//#else
var e;
//#if TRUE
var f;
//#endif
var g;
//#endif

View File

@@ -0,0 +1,2 @@
'use strict';
var a;

View File

@@ -0,0 +1,8 @@
'use strict';
//#if TRUE
var a;
//#elif FALSE
var b;
//#else
var c;
//#endif

View File

@@ -0,0 +1,2 @@
'use strict';
var a;

View File

@@ -0,0 +1,6 @@
'use strict';
//#if TRUE
var a;
//#else
var b;
//#endif

View File

@@ -0,0 +1 @@
//Error: Missing #endif in preprocessor for __filename

View File

@@ -0,0 +1,3 @@
'use strict';
//#if TRUE
var a;

View File

@@ -0,0 +1,5 @@
<script>
'use strict';
var a;
</script>

View File

@@ -0,0 +1 @@
//Error: Failed to include "some file that does not exist" at __filename:2

View File

@@ -0,0 +1,2 @@
<!-- Non-existent file -->
<!--#include some file that does not exist-->

View File

@@ -0,0 +1,5 @@
<script>
<!--#if TRUE-->
<!--#include if-true-else.js-->
<!--#endif-->
</script>

View File

@@ -0,0 +1,4 @@
'use strict';
//var a;
var b;
var c;

View File

@@ -0,0 +1,6 @@
'use strict';
//#if TRUE
////var a;
//var b;
var c;
//#endif

View File

@@ -0,0 +1,2 @@
//Error: Could not evaluate "notdefined" at __filename:2
//ReferenceError: notdefined is not defined

View File

@@ -0,0 +1,6 @@
'use strict';
//#if notdefined
var a;
//#else
var b;
//#endif

View File

@@ -0,0 +1 @@
//Error: Found #endif without #if at __filename:4

View File

@@ -0,0 +1,5 @@
'use strict';
//#ifdef TRUE
//ifdef should not be recognized
//#endif
var a;

View File

@@ -0,0 +1,11 @@
function test() {
"test";
"1";
"2";
"3";
if ("test") {
"5";
}
"4";
}
test();

View File

@@ -0,0 +1,20 @@
function test() {
{;}
;
"test";
{
"1";
if (true) {
"2";
}
;
{
"3";
if ("test") {
"5";
}
}
"4";
}
}
test();

View File

@@ -0,0 +1,20 @@
function f1() {
"1";
"2";
}
f1();
function f2() {
"1";
"2";
}
f2();
function f3() {
if ("1") {
"1";
}
"2";
if ("3") {
"4";
}
}
f3();

View File

@@ -0,0 +1,29 @@
/* globals f0 */
function f1() {
/* head */
"1";
/* mid */
"2";
/* tail */
}
f1();
function f2() {
// head
"1";
// mid
"2";
// tail
}
f2();
function f3() {
if ("1") { // begin block
"1";
}
"2"; // trailing
if (/* s */"3"/*e*/) {
"4";
}
}
f3();

View File

@@ -0,0 +1,15 @@
var a = true;
var b = false;
var c = '1';
var d = false;
var e = true;
var f = '0';
var g = '1';
var h = '0';
var i = true;
var j = false;
var k = false;
var l = true;
var m = false;
var n = false;
var o = true;

View File

@@ -0,0 +1,15 @@
var a = true;
var b = false;
var c = true && '1';
var d = false && '0';
var e = true || '1';
var f = false || '0';
var g = true ? '1' : '0';
var h = false ? '1' : '0';
var i = 'test' === 'test';
var j = 'test' !== 'test';
var k = 'test' === 'test2';
var l = 'test' !== 'test2';
var m = '1' === true;
var n = !true;
var o = !false;

View File

@@ -0,0 +1,23 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {
constructor(x = console.log("Hi!")) {}
}
class C {
constructor({
x
}) {}
}
class D {}
class E extends A {
constructor() {}
}
class F {
constructor() {
var a = 0;
}
}
class G {}

View File

@@ -0,0 +1,37 @@
class A {
constructor() {
console.log("Hi!");
}
}
class B {
constructor(x = console.log("Hi!")) {}
}
class C {
constructor({ x }) {}
}
class D {
constructor(x, y, z) {}
}
class E extends A {
constructor() {}
}
class F {
constructor() {
if (PDFJSDev.test('TRUE')) {
var a = 0;
}
}
}
class G {
constructor() {
if (PDFJSDev.test('FALSE')) {
var a = 0;
}
}
}

View File

@@ -0,0 +1,25 @@
function f1() {}
f1();
function f2() {
return 1;
}
f2();
function f3() {
var i = 0;
throw "test";
}
f3();
function f4() {
var i = 0;
}
f4();
var obj = {
method1() {},
method2() {}
};
class C {
method1() {}
method2() {}
}
var arrow1 = () => {};
var arrow2 = () => {};

View File

@@ -0,0 +1,41 @@
function f1() {
return;
var i = 0;
}
f1();
function f2() {
return 1;
var i = 0;
}
f2();
function f3() {
var i = 0;
throw "test";
var j = 0;
}
f3();
function f4() {
var i = 0;
if (true) {
return;
}
throw "test";
var j = 0;
}
f4();
var obj = {
method1() { return; var i = 0; },
method2() { return; },
};
class C {
method1() { return; var i = 0; }
method2() { return; }
}
var arrow1 = () => { return; var i = 0; };
var arrow2 = () => { return; };

View File

@@ -0,0 +1,18 @@
var a = false;
var b = true;
var c = true;
var d = false;
var e = true;
var f = "text";
var g = {
obj: {
i: 1
},
j: 2
};
var i = '0';
var j = {
i: 1
};
var k = false;
var l = true;

View File

@@ -0,0 +1,11 @@
var a = typeof PDFJSDev === 'undefined';
var b = typeof PDFJSDev !== 'undefined';
var c = PDFJSDev.test('TRUE');
var d = PDFJSDev.test('FALSE');
var e = PDFJSDev.eval('TRUE');
var f = PDFJSDev.eval('TEXT');
var g = PDFJSDev.eval('OBJ');
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
var k = !PDFJSDev.test('TRUE');
var l = !PDFJSDev.test('FALSE');

View File

@@ -0,0 +1,19 @@
if ('test') {
"1";
}
{
"1";
}
{
"1";
}
{
"2";
}
if ('1') {
"1";
}
function f1() {
"1";
}
f1();

35
external/builder/fixtures_babel/ifs.js vendored Normal file
View File

@@ -0,0 +1,35 @@
if ('test') {
"1";
}
if (true) {
"1";
}
if (true) {
"1";
} else {
"2";
}
if (false) {
"1";
}
if (false) {
"1";
} else {
"2";
}
if (true && false) {
"1";
}
if (true && false || '1') {
"1";
}
function f1() {
if (true) {
"1";
}
if (false) {
"2";
}
}
f1();

View File

@@ -0,0 +1,7 @@
import { Test } from "import-name";
import { Test2 } from './non-alias';
export { Test3 } from "import-name";
await import(
/*webpackIgnore: true*/
/*@vite-ignore*/
"./non-alias");

View File

@@ -0,0 +1,4 @@
import { Test } from 'import-alias';
import { Test2 } from './non-alias';
export { Test3 } from 'import-alias';
await __raw_import__("./non-alias");

View File

@@ -0,0 +1,8 @@
class A {
static {
foo();
}
static {
var a = 0;
}
}

View File

@@ -0,0 +1,20 @@
class A {
static {}
static {
{ foo() }
}
static {
{;}
}
static {
if (PDFJSDev.test('TRUE')) {
var a = 0;
}
}
static {
if (PDFJSDev.test('FALSE')) {
var a = 1;
}
}
}

View File

@@ -0,0 +1,5 @@
function usedByUsed() {}
function used() {
usedByUsed();
}
used();

View File

@@ -0,0 +1,14 @@
function usedByUsed() {}
function usedByUnused() {}
function usedByRemovedCode() {}
function used() {
usedByUsed();
return;
usedByRemovedCode();
}
function unused() {
usedByUnused();
}
used();

68
external/builder/test-fixtures.mjs vendored Normal file
View File

@@ -0,0 +1,68 @@
import * as builder from "./builder.mjs";
import fs from "fs";
import path from "path";
const __dirname = import.meta.dirname;
let errors = 0;
const baseDir = path.join(__dirname, "fixtures");
const files = fs
.readdirSync(baseDir)
.filter(function (name) {
return /-expected\./.test(name);
})
.map(function (name) {
return path.join(baseDir, name);
});
files.forEach(function (expectationFilename) {
const inFilename = expectationFilename.replace("-expected", "");
const expectation = fs
.readFileSync(expectationFilename)
.toString()
.trim()
.replaceAll("__filename", fs.realpathSync(inFilename));
const outLines = [];
const outFilename = function (line) {
outLines.push(line);
};
const defines = {
TRUE: true,
FALSE: false,
};
let out;
try {
builder.preprocess(inFilename, outFilename, defines);
out = outLines.join("\n").trim();
} catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//");
}
if (out !== expectation) {
errors++;
// Allow regenerating the expected output using
// OVERWRITE=true node ./external/builder/test-fixtures.mjs
if (process.env.OVERWRITE) {
fs.writeFileSync(expectationFilename, out + "\n");
}
console.log("Assertion failed for " + inFilename);
console.log("--------------------------------------------------");
console.log("EXPECTED:");
console.log(expectation);
console.log("--------------------------------------------------");
console.log("ACTUAL");
console.log(out);
console.log("--------------------------------------------------");
console.log();
}
});
if (errors) {
console.error("Found " + errors + " expectation failures.");
process.exit(1);
} else {
console.log("All tests completed without errors.");
process.exit(0);
}

View File

@@ -0,0 +1,74 @@
import fs from "fs";
import path from "path";
import { preprocessPDFJSCode } from "./babel-plugin-pdfjs-preprocessor.mjs";
const __dirname = import.meta.dirname;
let errors = 0;
const baseDir = path.join(__dirname, "fixtures_babel");
const files = fs
.readdirSync(baseDir)
.filter(function (name) {
return /-expected\./.test(name);
})
.map(function (name) {
return path.join(baseDir, name);
});
files.forEach(function (expectationFilename) {
const inFilename = expectationFilename.replace("-expected", "");
const expectation = fs
.readFileSync(expectationFilename)
.toString()
.trim()
.replaceAll("__filename", fs.realpathSync(inFilename));
const input = fs.readFileSync(inFilename).toString();
const defines = {
TRUE: true,
FALSE: false,
OBJ: { obj: { i: 1 }, j: 2 },
TEXT: "text",
};
const map = {
"import-alias": "import-name",
};
const ctx = {
defines,
map,
rootPath: __dirname + "/../..",
};
let out;
try {
out = preprocessPDFJSCode(ctx, input);
} catch (e) {
out = ("Error: " + e.message).replaceAll(/^/gm, "//");
}
if (out !== expectation) {
errors++;
// Allow regenerating the expected output using
// OVERWRITE=true node ./external/builder/test-fixtures_babel.mjs
if (process.env.OVERWRITE) {
fs.writeFileSync(expectationFilename, out + "\n");
}
console.log("Assertion failed for " + inFilename);
console.log("--------------------------------------------------");
console.log("EXPECTED:");
console.log(expectation);
console.log("--------------------------------------------------");
console.log("ACTUAL");
console.log(out);
console.log("--------------------------------------------------");
console.log();
}
});
if (errors) {
console.error("Found " + errors + " expectation failures.");
process.exit(1);
} else {
console.log("All tests completed without errors.");
process.exit(0);
}