chore(jsdoc): remove jsdoc/env from astnode

This commit is contained in:
Jeff Williams 2020-12-24 14:59:36 -08:00
parent ad6d74e937
commit e816766f6c
2 changed files with 26 additions and 71 deletions

View File

@ -1,7 +1,7 @@
// TODO: docs // TODO: docs
/** @module jsdoc/src/astnode */ /** @module jsdoc/src/astnode */
const _ = require('lodash');
const { cast } = require('@jsdoc/util'); const { cast } = require('@jsdoc/util');
const env = require('jsdoc/env');
const { SCOPE } = require('@jsdoc/core').name; const { SCOPE } = require('@jsdoc/core').name;
const { Syntax } = require('jsdoc/src/syntax'); const { Syntax } = require('jsdoc/src/syntax');
@ -46,7 +46,6 @@ exports.isScope = node => // TODO: handle blocks with "let" declarations
// TODO: docs // TODO: docs
exports.addNodeProperties = node => { exports.addNodeProperties = node => {
const debugEnabled = Boolean(env.opts.debug);
const newProperties = {}; const newProperties = {};
if (!node || typeof node !== 'object') { if (!node || typeof node !== 'object') {
@ -56,11 +55,11 @@ exports.addNodeProperties = node => {
if (!node.nodeId) { if (!node.nodeId) {
newProperties.nodeId = { newProperties.nodeId = {
value: `astnode${uid++}`, value: `astnode${uid++}`,
enumerable: debugEnabled enumerable: true
}; };
} }
if (!node.parent && node.parent !== null) { if (_.isUndefined(node.parent)) {
newProperties.parent = { newProperties.parent = {
// `null` means 'no parent', so use `undefined` for now // `null` means 'no parent', so use `undefined` for now
value: undefined, value: undefined,
@ -68,7 +67,7 @@ exports.addNodeProperties = node => {
}; };
} }
if (!node.enclosingScope && node.enclosingScope !== null) { if (_.isUndefined(node.enclosingScope)) {
newProperties.enclosingScope = { newProperties.enclosingScope = {
// `null` means 'no enclosing scope', so use `undefined` for now // `null` means 'no enclosing scope', so use `undefined` for now
value: undefined, value: undefined,
@ -76,7 +75,7 @@ exports.addNodeProperties = node => {
}; };
} }
if (debugEnabled && typeof node.parentId === 'undefined') { if (_.isUndefined(node.parentId)) {
newProperties.parentId = { newProperties.parentId = {
enumerable: true, enumerable: true,
get() { get() {
@ -85,7 +84,7 @@ exports.addNodeProperties = node => {
}; };
} }
if (debugEnabled && typeof node.enclosingScopeId === 'undefined') { if (_.isUndefined(node.enclosingScopeId)) {
newProperties.enclosingScopeId = { newProperties.enclosingScopeId = {
enumerable: true, enumerable: true,
get() { get() {

View File

@ -2,7 +2,6 @@ describe('jsdoc/src/astNode', () => {
const astBuilder = require('jsdoc/src/astbuilder'); const astBuilder = require('jsdoc/src/astbuilder');
const astNode = require('jsdoc/src/astnode'); const astNode = require('jsdoc/src/astnode');
const babelParser = require('@babel/parser'); const babelParser = require('@babel/parser');
const env = require('jsdoc/env');
const Syntax = require('jsdoc/src/syntax').Syntax; const Syntax = require('jsdoc/src/syntax').Syntax;
function parse(str) { function parse(str) {
@ -79,16 +78,6 @@ describe('jsdoc/src/astNode', () => {
}); });
describe('addNodeProperties', () => { describe('addNodeProperties', () => {
let debugEnabled;
beforeEach(() => {
debugEnabled = Boolean(env.opts.debug);
});
afterEach(() => {
env.opts.debug = debugEnabled;
});
it('should return null for undefined input', () => { it('should return null for undefined input', () => {
expect( astNode.addNodeProperties() ).toBe(null); expect( astNode.addNodeProperties() ).toBe(null);
}); });
@ -104,13 +93,13 @@ describe('jsdoc/src/astNode', () => {
expect(node.foo).toBe(1); expect(node.foo).toBe(1);
}); });
it('should add a non-enumerable nodeId if necessary', () => { it('should add a nodeId if necessary', () => {
const node = astNode.addNodeProperties({}); const node = astNode.addNodeProperties({});
const descriptor = Object.getOwnPropertyDescriptor(node, 'nodeId'); const descriptor = Object.getOwnPropertyDescriptor(node, 'nodeId');
expect(descriptor).toBeObject(); expect(descriptor).toBeObject();
expect(descriptor.value).toBeString(); expect(descriptor.value).toBeString();
expect(descriptor.enumerable).toBeFalse(); expect(descriptor.enumerable).toBeTrue();
}); });
it('should not overwrite an existing nodeId', () => { it('should not overwrite an existing nodeId', () => {
@ -120,17 +109,6 @@ describe('jsdoc/src/astNode', () => {
expect(node.nodeId).toBe(nodeId); expect(node.nodeId).toBe(nodeId);
}); });
it('should add an enumerable nodeId in debug mode', () => {
let descriptor;
let node;
env.opts.debug = true;
node = astNode.addNodeProperties({});
descriptor = Object.getOwnPropertyDescriptor(node, 'nodeId');
expect(descriptor.enumerable).toBeTrue();
});
it('should add a non-enumerable, writable parent if necessary', () => { it('should add a non-enumerable, writable parent if necessary', () => {
const node = astNode.addNodeProperties({}); const node = astNode.addNodeProperties({});
const descriptor = Object.getOwnPropertyDescriptor(node, 'parent'); const descriptor = Object.getOwnPropertyDescriptor(node, 'parent');
@ -154,34 +132,24 @@ describe('jsdoc/src/astNode', () => {
expect(node.parent).toBeNull(); expect(node.parent).toBeNull();
}); });
it('should add an enumerable parentId in debug mode', () => { it('should add an enumerable parentId', () => {
let descriptor; const node = astNode.addNodeProperties({});
let node; const descriptor = Object.getOwnPropertyDescriptor(node, 'parentId');
env.opts.debug = true;
node = astNode.addNodeProperties({});
descriptor = Object.getOwnPropertyDescriptor(node, 'parentId');
expect(descriptor).toBeObject(); expect(descriptor).toBeObject();
expect(descriptor.enumerable).toBeTrue(); expect(descriptor.enumerable).toBeTrue();
}); });
it('should provide a null parentId in debug mode for nodes with no parent', () => { it('should provide a null parentId for nodes with no parent', () => {
let node; const node = astNode.addNodeProperties({});
env.opts.debug = true;
node = astNode.addNodeProperties({});
expect(node.parentId).toBeNull(); expect(node.parentId).toBeNull();
}); });
it('should provide a non-null parentId in debug mode for nodes with a parent', () => { it('should provide a non-null parentId for nodes with a parent', () => {
let node; const node = astNode.addNodeProperties({});
let parent; const parent = astNode.addNodeProperties({});
env.opts.debug = true;
node = astNode.addNodeProperties({});
parent = astNode.addNodeProperties({});
node.parent = parent; node.parent = parent;
expect(node.parentId).toBe(parent.nodeId); expect(node.parentId).toBe(parent.nodeId);
@ -210,36 +178,24 @@ describe('jsdoc/src/astNode', () => {
expect(node.enclosingScope).toBeNull(); expect(node.enclosingScope).toBeNull();
}); });
it('should add an enumerable enclosingScopeId in debug mode', () => { it('should add an enumerable enclosingScopeId', () => {
let descriptor; const node = astNode.addNodeProperties({});
let node; const descriptor = Object.getOwnPropertyDescriptor(node, 'enclosingScopeId');
env.opts.debug = true;
node = astNode.addNodeProperties({});
descriptor = Object.getOwnPropertyDescriptor(node, 'enclosingScopeId');
expect(descriptor).toBeObject(); expect(descriptor).toBeObject();
expect(descriptor.enumerable).toBeTrue(); expect(descriptor.enumerable).toBeTrue();
}); });
it('should provide a null enclosingScopeId in debug mode for nodes with no enclosing scope', it('should provide a null enclosingScopeId for nodes with no enclosing scope', () => {
() => { const node = astNode.addNodeProperties({});
let node;
env.opts.debug = true; expect(node.enclosingScopeId).toBeNull();
node = astNode.addNodeProperties({}); });
expect(node.enclosingScopeId).toBeNull(); it('should provide a non-null enclosingScopeId for nodes with an enclosing scope', () => {
}); const node = astNode.addNodeProperties({});
const enclosingScope = astNode.addNodeProperties({});
it('should provide a non-null enclosingScopeId in debug mode for nodes with an enclosing ' +
'scope', () => {
let enclosingScope;
let node;
env.opts.debug = true;
node = astNode.addNodeProperties({});
enclosingScope = astNode.addNodeProperties({});
node.enclosingScope = enclosingScope; node.enclosingScope = enclosingScope;
expect(node.enclosingScopeId).toBe(enclosingScope.nodeId); expect(node.enclosingScopeId).toBe(enclosingScope.nodeId);