mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 01:10:08 +02:00
Merge pull request #18060 from nicolo-ribaudo/babel-plugin-class
Update the Babel plugin to remove empty class constructors
This commit is contained in:
commit
36a496b409
23 changed files with 88 additions and 8 deletions
|
@ -4,7 +4,7 @@ docs/
|
||||||
node_modules/
|
node_modules/
|
||||||
external/bcmaps/
|
external/bcmaps/
|
||||||
external/builder/fixtures/
|
external/builder/fixtures/
|
||||||
external/builder/fixtures_esprima/
|
external/builder/fixtures_babel/
|
||||||
external/quickjs/
|
external/quickjs/
|
||||||
external/openjpeg/
|
external/openjpeg/
|
||||||
test/tmp/
|
test/tmp/
|
||||||
|
|
|
@ -4,7 +4,7 @@ docs/
|
||||||
node_modules/
|
node_modules/
|
||||||
external/bcmaps/
|
external/bcmaps/
|
||||||
external/builder/fixtures/
|
external/builder/fixtures/
|
||||||
external/builder/fixtures_esprima/
|
external/builder/fixtures_babel/
|
||||||
external/quickjs/
|
external/quickjs/
|
||||||
test/tmp/
|
test/tmp/
|
||||||
test/pdfs/
|
test/pdfs/
|
||||||
|
|
|
@ -4,7 +4,7 @@ docs/
|
||||||
node_modules/
|
node_modules/
|
||||||
external/bcmaps/
|
external/bcmaps/
|
||||||
external/builder/fixtures/
|
external/builder/fixtures/
|
||||||
external/builder/fixtures_esprima/
|
external/builder/fixtures_babel/
|
||||||
external/quickjs/
|
external/quickjs/
|
||||||
test/tmp/
|
test/tmp/
|
||||||
test/pdfs/
|
test/pdfs/
|
||||||
|
|
|
@ -234,6 +234,26 @@ function babelPluginPDFJSPreprocessor(babel, ctx) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal file
23
external/builder/fixtures_babel/constructors-expected.js
vendored
Normal 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 {}
|
37
external/builder/fixtures_babel/constructors.js
vendored
Normal file
37
external/builder/fixtures_babel/constructors.js
vendored
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ var d = PDFJSDev.test('FALSE');
|
||||||
var e = PDFJSDev.eval('TRUE');
|
var e = PDFJSDev.eval('TRUE');
|
||||||
var f = PDFJSDev.eval('TEXT');
|
var f = PDFJSDev.eval('TEXT');
|
||||||
var g = PDFJSDev.eval('OBJ');
|
var g = PDFJSDev.eval('OBJ');
|
||||||
var h = PDFJSDev.json('$ROOT/external/builder/fixtures_esprima/evals.json');
|
var h = PDFJSDev.json('$ROOT/external/builder/fixtures_babel/evals.json');
|
||||||
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
|
var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0';
|
||||||
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
|
var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0';
|
||||||
var k = !PDFJSDev.test('TRUE');
|
var k = !PDFJSDev.test('TRUE');
|
|
@ -7,7 +7,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
let errors = 0;
|
let errors = 0;
|
||||||
|
|
||||||
const baseDir = path.join(__dirname, "fixtures_esprima");
|
const baseDir = path.join(__dirname, "fixtures_babel");
|
||||||
const files = fs
|
const files = fs
|
||||||
.readdirSync(baseDir)
|
.readdirSync(baseDir)
|
||||||
.filter(function (name) {
|
.filter(function (name) {
|
||||||
|
@ -49,7 +49,7 @@ files.forEach(function (expectationFilename) {
|
||||||
errors++;
|
errors++;
|
||||||
|
|
||||||
// Allow regenerating the expected output using
|
// Allow regenerating the expected output using
|
||||||
// OVERWRITE=true node ./external/builder/test-fixtures_esprima.mjs
|
// OVERWRITE=true node ./external/builder/test-fixtures_babel.mjs
|
||||||
if (process.env.OVERWRITE) {
|
if (process.env.OVERWRITE) {
|
||||||
fs.writeFileSync(expectationFilename, out + "\n");
|
fs.writeFileSync(expectationFilename, out + "\n");
|
||||||
}
|
}
|
|
@ -2408,8 +2408,8 @@ gulp.task("externaltest", function (done) {
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log();
|
console.log();
|
||||||
console.log("### Running test-fixtures_esprima.js");
|
console.log("### Running test-fixtures_babel.js");
|
||||||
safeSpawnSync("node", ["external/builder/test-fixtures_esprima.mjs"], {
|
safeSpawnSync("node", ["external/builder/test-fixtures_babel.mjs"], {
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue