Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make esm treeshake friendly and add test #661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 26 additions & 24 deletions lib/exception.js
Expand Up @@ -22,34 +22,36 @@ function formatError(exception, compact) {
}


function YAMLException(reason, mark) {
// Super constructor
Error.call(this);

this.name = 'YAMLException';
this.reason = reason;
this.mark = mark;
this.message = formatError(this, false);

// Include stack trace in error object
if (Error.captureStackTrace) {
// Chrome and NodeJS
Error.captureStackTrace(this, this.constructor);
} else {
// FF, IE 10+ and Safari 6+. Fallback for others
this.stack = (new Error()).stack || '';
var YAMLException = /*@__PURE__*/(function _() {
function YAMLException(reason, mark) {
// Super constructor
Error.call(this);

this.name = 'YAMLException';
this.reason = reason;
this.mark = mark;
this.message = formatError(this, false);

// Include stack trace in error object
if (Error.captureStackTrace) {
// Chrome and NodeJS
Error.captureStackTrace(this, this.constructor);
} else {
// FF, IE 10+ and Safari 6+. Fallback for others
this.stack = (new Error()).stack || '';
}
}
}


// Inherit from Error
YAMLException.prototype = Object.create(Error.prototype);
YAMLException.prototype.constructor = YAMLException;
// Inherit from Error
YAMLException.prototype = Object.create(Error.prototype);
YAMLException.prototype.constructor = YAMLException;


YAMLException.prototype.toString = function toString(compact) {
return this.name + ': ' + formatError(this, compact);
};
YAMLException.prototype.toString = function toString(compact) {
return this.name + ': ' + formatError(this, compact);
};

return YAMLException;
})();

module.exports = YAMLException;
23 changes: 17 additions & 6 deletions lib/loader.js
Expand Up @@ -120,12 +120,23 @@ function charFromCodepoint(c) {
);
}

var simpleEscapeCheck = new Array(256); // integer, for fast access
var simpleEscapeMap = new Array(256);
for (var i = 0; i < 256; i++) {
simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
simpleEscapeMap[i] = simpleEscapeSequence(i);
}

var _simpleEscape = /*@__PURE__*/(function _() {
var simpleEscapeCheck = new Array(256); // integer, for fast access
var simpleEscapeMap = new Array(256);
for (var i = 0; i < 256; i++) {
simpleEscapeCheck[i] = simpleEscapeSequence(i) ? 1 : 0;
simpleEscapeMap[i] = simpleEscapeSequence(i);
}

return {
simpleEscapeCheck: simpleEscapeCheck,
simpleEscapeMap: simpleEscapeMap
};
})();

var simpleEscapeCheck = _simpleEscape.simpleEscapeCheck;
var simpleEscapeMap = _simpleEscape.simpleEscapeMap;


function State(input, options) {
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/default.js
Expand Up @@ -8,7 +8,7 @@
'use strict';


module.exports = require('./core').extend({
module.exports = /*@__PURE__*/require('./core').extend({
implicit: [
require('../type/timestamp'),
require('../type/merge')
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/failsafe.js
Expand Up @@ -8,7 +8,7 @@
var Schema = require('../schema');


module.exports = new Schema({
module.exports = /*@__PURE__*/new Schema({
explicit: [
require('../type/str'),
require('../type/seq'),
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/json.js
Expand Up @@ -9,7 +9,7 @@
'use strict';


module.exports = require('./failsafe').extend({
module.exports = /*@__PURE__*/require('./failsafe').extend({
implicit: [
require('../type/null'),
require('../type/bool'),
Expand Down
2 changes: 1 addition & 1 deletion lib/type/binary.js
Expand Up @@ -116,7 +116,7 @@ function isBinary(obj) {
return Object.prototype.toString.call(obj) === '[object Uint8Array]';
}

module.exports = new Type('tag:yaml.org,2002:binary', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:binary', {
kind: 'scalar',
resolve: resolveYamlBinary,
construct: constructYamlBinary,
Expand Down
2 changes: 1 addition & 1 deletion lib/type/bool.js
Expand Up @@ -21,7 +21,7 @@ function isBoolean(object) {
return Object.prototype.toString.call(object) === '[object Boolean]';
}

module.exports = new Type('tag:yaml.org,2002:bool', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:bool', {
kind: 'scalar',
resolve: resolveYamlBoolean,
construct: constructYamlBoolean,
Expand Down
2 changes: 1 addition & 1 deletion lib/type/float.js
Expand Up @@ -87,7 +87,7 @@ function isFloat(object) {
(object % 1 !== 0 || common.isNegativeZero(object));
}

module.exports = new Type('tag:yaml.org,2002:float', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:float', {
kind: 'scalar',
resolve: resolveYamlFloat,
construct: constructYamlFloat,
Expand Down
2 changes: 1 addition & 1 deletion lib/type/int.js
Expand Up @@ -134,7 +134,7 @@ function isInteger(object) {
(object % 1 === 0 && !common.isNegativeZero(object));
}

module.exports = new Type('tag:yaml.org,2002:int', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:int', {
kind: 'scalar',
resolve: resolveYamlInteger,
construct: constructYamlInteger,
Expand Down
2 changes: 1 addition & 1 deletion lib/type/map.js
Expand Up @@ -2,7 +2,7 @@

var Type = require('../type');

module.exports = new Type('tag:yaml.org,2002:map', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:map', {
kind: 'mapping',
construct: function (data) { return data !== null ? data : {}; }
});
2 changes: 1 addition & 1 deletion lib/type/merge.js
Expand Up @@ -6,7 +6,7 @@ function resolveYamlMerge(data) {
return data === '<<' || data === null;
}

module.exports = new Type('tag:yaml.org,2002:merge', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:merge', {
kind: 'scalar',
resolve: resolveYamlMerge
});
2 changes: 1 addition & 1 deletion lib/type/null.js
Expand Up @@ -19,7 +19,7 @@ function isNull(object) {
return object === null;
}

module.exports = new Type('tag:yaml.org,2002:null', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:null', {
kind: 'scalar',
resolve: resolveYamlNull,
construct: constructYamlNull,
Expand Down
2 changes: 1 addition & 1 deletion lib/type/omap.js
Expand Up @@ -37,7 +37,7 @@ function constructYamlOmap(data) {
return data !== null ? data : [];
}

module.exports = new Type('tag:yaml.org,2002:omap', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:omap', {
kind: 'sequence',
resolve: resolveYamlOmap,
construct: constructYamlOmap
Expand Down
2 changes: 1 addition & 1 deletion lib/type/pairs.js
Expand Up @@ -46,7 +46,7 @@ function constructYamlPairs(data) {
return result;
}

module.exports = new Type('tag:yaml.org,2002:pairs', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:pairs', {
kind: 'sequence',
resolve: resolveYamlPairs,
construct: constructYamlPairs
Expand Down
2 changes: 1 addition & 1 deletion lib/type/seq.js
Expand Up @@ -2,7 +2,7 @@

var Type = require('../type');

module.exports = new Type('tag:yaml.org,2002:seq', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:seq', {
kind: 'sequence',
construct: function (data) { return data !== null ? data : []; }
});
2 changes: 1 addition & 1 deletion lib/type/set.js
Expand Up @@ -22,7 +22,7 @@ function constructYamlSet(data) {
return data !== null ? data : {};
}

module.exports = new Type('tag:yaml.org,2002:set', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:set', {
kind: 'mapping',
resolve: resolveYamlSet,
construct: constructYamlSet
Expand Down
2 changes: 1 addition & 1 deletion lib/type/str.js
Expand Up @@ -2,7 +2,7 @@

var Type = require('../type');

module.exports = new Type('tag:yaml.org,2002:str', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:str', {
kind: 'scalar',
construct: function (data) { return data !== null ? data : ''; }
});
2 changes: 1 addition & 1 deletion lib/type/timestamp.js
Expand Up @@ -79,7 +79,7 @@ function representYamlTimestamp(object /*, style*/) {
return object.toISOString();
}

module.exports = new Type('tag:yaml.org,2002:timestamp', {
module.exports = /*@__PURE__*/new Type('tag:yaml.org,2002:timestamp', {
kind: 'scalar',
resolve: resolveYamlTimestamp,
construct: constructYamlTimestamp,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -56,6 +56,7 @@
"eslint": "^7.0.0",
"fast-check": "^2.8.0",
"gh-pages": "^3.1.0",
"lodash.truncate": "^4.4.2",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"rollup": "^2.34.1",
Expand Down
42 changes: 42 additions & 0 deletions test/issues/0631.js
@@ -0,0 +1,42 @@
'use strict';

var assert = require('assert');
var child_process = require('child_process');
var truncate = require('lodash.truncate');

function assertPureESM(esmPath, rollupCmd = 'rollup') {
// verify rollup is able to have non-empty output, when not tree shakable.
var expected = 'const foo = "bar";\n\nexport { foo };\n';

assert.strictEqual(
expected,

// in normal worse case, may have lines > 1k
truncate(
child_process
.execSync(rollupCmd, {
stdio: [ 'pipe', 'pipe', 'ignore' ],
input: `export {} from "${esmPath}"; export const foo = "bar"; `
})
.toString(),
{
length: 4 * expected.length
}
)
);

// verify rollup js-yaml has empty output, aka tree shakable.
assert.strictEqual(
'\n',
child_process
.execSync(rollupCmd, {
stdio: [ 'pipe', 'pipe', 'ignore' ],
input: `export {} from "${esmPath}"`
})
.toString()
);
}

it('esm should be purely tree shakable', function () {
assertPureESM('./dist/js-yaml.mjs');
});