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

Replace array indexOf checks with includes #954

Merged
merged 5 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/lib/converter/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class Context {
* @param callback The callback that should be executed.
*/
withSourceFile(node: ts.SourceFile, callback: Function) {
let isExternal = this.fileNames.indexOf(node.fileName) === -1;
let isExternal = !this.fileNames.includes(node.fileName);
if (!isExternal && this.externalPattern) {
isExternal = this.externalPattern.some(mm => mm.match(node.fileName));
}
Expand Down Expand Up @@ -322,7 +322,7 @@ export class Context {

if (baseNode.symbol) {
const id = this.getSymbolID(baseNode.symbol)!;
if (this.inheritedChildren && this.inheritedChildren.indexOf(id) !== -1) {
if (this.inheritedChildren && this.inheritedChildren.includes(id)) {
return target;
} else {
this.inheritedChildren = this.inheritedChildren || [];
Expand Down
6 changes: 3 additions & 3 deletions src/lib/converter/factories/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export function createDeclaration(context: Context, node: ts.Declaration, kind:
// Test whether the node is static, when merging a module to a class make the node static
let isConstructorProperty = false;
let isStatic = false;
if (nonStaticKinds.indexOf(kind) === -1) {
if (!nonStaticKinds.includes(kind)) {
isStatic = !!(modifiers & ts.ModifierFlags.Static);
if (container.kind === ReflectionKind.Class) {
if (node.parent && node.parent.kind === ts.SyntaxKind.Constructor) {
isConstructorProperty = true;
} else if (!node.parent || nonStaticMergeKinds.indexOf(node.parent.kind) === -1) {
} else if (!node.parent || !nonStaticMergeKinds.includes(node.parent.kind)) {
isStatic = true;
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ function mergeDeclarations(context: Context, reflection: DeclarationReflection,

if (
context.isInherit &&
(context.inherited || []).indexOf(reflection.name) !== -1 &&
(context.inherited || []).includes(reflection.name) &&
(node.parent === context.inheritParent || reflection.flags.isConstructorProperty)
) {
if (!reflection.overwrites) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/nodes/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class BlockConverter extends ConverterNodeComponent<ts.SourceFile|ts.Bloc
const statements: ts.Statement[] = [];

node.statements.forEach((statement) => {
if (preferred.indexOf(statement.kind) !== -1) {
if (preferred.includes(statement.kind)) {
this.owner.convertNode(context, statement);
} else {
statements.push(statement);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/CommentPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class CommentPlugin extends ConverterComponent {
});

for (let key in project.symbolMapping) {
if (project.symbolMapping.hasOwnProperty(key) && deletedIds.indexOf(project.symbolMapping[key]) !== -1) {
if (project.symbolMapping.hasOwnProperty(key) && deletedIds.includes(project.symbolMapping[key])) {
delete project.symbolMapping[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/GitHubPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Repository {
* @returns TRUE when the file is part of the repository, otherwise FALSE.
*/
contains(fileName: string): boolean {
return this.files.indexOf(fileName) !== -1;
return this.files.includes(fileName);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/PackagePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class PackagePlugin extends ConverterComponent {
let dirName: string, parentDir = Path.resolve(Path.dirname(fileName));
do {
dirName = parentDir;
if (this.visited.indexOf(dirName) !== -1) {
if (this.visited.includes(dirName)) {
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/plugins/TypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class TypePlugin extends ConverterComponent {
}

private postpone(reflection: DeclarationReflection) {
if (this.reflections.indexOf(reflection) === -1) {
if (!this.reflections.includes(reflection)) {
this.reflections.push(reflection);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/converter/types/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class ReferenceConverter extends ConverterTypeComponent implements TypeNo
*/
private convertLiteral(context: Context, symbol: ts.Symbol, node?: ts.Node): Type | undefined {
for (let declaration of symbol.declarations) {
if (context.visitStack.indexOf(declaration) !== -1) {
if (context.visitStack.includes(declaration)) {
if (declaration.kind === ts.SyntaxKind.TypeLiteral ||
declaration.kind === ts.SyntaxKind.ObjectLiteralExpression) {
// TODO: Check if this type assertion is safe and document.
Expand Down
6 changes: 3 additions & 3 deletions src/lib/models/reflections/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ export class ReflectionFlags extends Array<string> {
private setSingleFlag(flag: ReflectionFlag, set: boolean) {
const name = ReflectionFlag[flag].replace(/(.)([A-Z])/g, (m, a, b) => a + ' ' + b.toLowerCase());
if (!set && this.hasFlag(flag)) {
if (relevantFlags.indexOf(flag) !== -1) {
if (relevantFlags.includes(flag)) {
this.splice(this.indexOf(name), 1);
}
this.flags ^= flag;
} else if (set && !this.hasFlag(flag)) {
if (relevantFlags.indexOf(flag) !== -1) {
if (relevantFlags.includes(flag)) {
this.push(name);
}
this.flags |= flag;
Expand Down Expand Up @@ -441,7 +441,7 @@ export abstract class Reflection {
target._aliases = [];
}
let suffix = '', index = 0;
while (target._aliases.indexOf(alias + suffix) !== -1) {
while (target._aliases.includes(alias + suffix)) {
suffix = '-' + (++index).toString();
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/output/plugins/NavigationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class NavigationPlugin extends RendererComponent {
item.isInPath = false;
item.isVisible = item.isGlobals;

if (item.url === page.url || (item.dedicatedUrls && item.dedicatedUrls.indexOf(page.url) !== -1)) {
if (item.url === page.url || (item.dedicatedUrls && item.dedicatedUrls.includes(page.url))) {
currentItems.push(item);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/output/plugins/TocPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class TocPlugin extends RendererComponent {
} else {
children.forEach((child: DeclarationReflection) => {

if (restriction && restriction.length > 0 && restriction.indexOf(child.name) === -1) {
if (restriction && restriction.length > 0 && !restriction.includes(child.name)) {
return;
}

Expand All @@ -72,7 +72,7 @@ export class TocPlugin extends RendererComponent {
}

const item = NavigationItem.create(child, parent, true);
if (trail.indexOf(child) !== -1) {
if (trail.includes(child)) {
item.isInPath = true;
item.isCurrent = (trail[trail.length - 1] === child);
TocPlugin.buildToc(child, trail, item);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/output/utils/resources/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class HelperStack extends ResourceStack<Helper> {
const helpers = resources[resourceName].getHelpers();

for (let name in helpers) {
if (this.registeredNames.indexOf(name) !== -1) {
if (this.registeredNames.includes(name)) {
continue;
}
this.registeredNames.push(name);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/output/utils/resources/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class PartialStack extends TemplateStack {
const resources = this.getAllResources();

for (let name in resources) {
if (this.registeredNames.indexOf(name) !== -1) {
if (this.registeredNames.includes(name)) {
continue;
}
this.registeredNames.push(name);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/options/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class OptionDeclaration {
value = map.has(key) ? map.get(key) : value;
} else if (key in map) {
value = map[key];
} else if (values.indexOf(value) === -1 && errorCallback) {
} else if (!values.includes(value) && errorCallback) {
if (this.mapError) {
errorCallback(this.mapError);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/options/sources/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ComponentSource extends OptionsComponent {
return;
}

if (this.knownComponents.indexOf(name) === -1) {
if (!this.knownComponents.includes(name)) {
this.knownComponents.push(name);
this.owner.addDeclarations(component.getOptionDeclarations());
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/options/sources/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TypeScriptSource extends OptionsComponent {
this.declarations = [];

for (let declaration of _ts.optionDeclarations) {
if (TypeScriptSource.IGNORED.indexOf(declaration.name) === -1) {
if (!TypeScriptSource.IGNORED.includes(declaration.name)) {
this.addTSOption(declaration);
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/test/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,55 @@ describe('TypeDoc', function() {
const inputFiles = Path.join(__dirname, 'converter', 'class');
const expanded = application.expandInputFiles([inputFiles]);

Assert.notEqual(expanded.indexOf(Path.join(inputFiles, 'class.ts')), -1);
Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(expanded.includes(Path.join(inputFiles, 'class.ts')));
Assert.ok(!expanded.includes(inputFiles));
bendtherules marked this conversation as resolved.
Show resolved Hide resolved
});
it('expands input files', function() {
const inputFiles = Path.join(__dirname, 'converter', 'class', 'class.ts');
const expanded = application.expandInputFiles([inputFiles]);

Assert.notEqual(expanded.indexOf(inputFiles), -1);
Assert.ok(expanded.includes(inputFiles));
});
it('honors the exclude argument even on a fixed directory list', function() {
const inputFiles = Path.join(__dirname, 'converter', 'class');
application.options.setValue('exclude', '**/class.ts');
const expanded = application.expandInputFiles([inputFiles]);

Assert.equal(expanded.indexOf(Path.join(inputFiles, 'class.ts')), -1);
Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(!expanded.includes(Path.join(inputFiles, 'class.ts')));
Assert.ok(!expanded.includes(inputFiles));
});
it('honors the exclude argument even on a fixed file list', function() {
const inputFiles = Path.join(__dirname, 'converter', 'class', 'class.ts');
application.options.setValue('exclude', '**/class.ts');
const expanded = application.expandInputFiles([inputFiles]);

Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(!expanded.includes(inputFiles));
});
it('supports multiple excludes', function() {
const inputFiles = Path.join(__dirname, 'converter');
application.options.setValue('exclude', '**/+(class|access).ts');
const expanded = application.expandInputFiles([inputFiles]);

Assert.equal(expanded.indexOf(Path.join(inputFiles, 'class', 'class.ts')), -1);
Assert.equal(expanded.indexOf(Path.join(inputFiles, 'access', 'access.ts')), -1);
Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(!expanded.includes(Path.join(inputFiles, 'class', 'class.ts')));
Assert.ok(!expanded.includes(Path.join(inputFiles, 'access', 'access.ts')));
Assert.ok(!expanded.includes(inputFiles));
});
it('supports array of excludes', function() {
const inputFiles = Path.join(__dirname, 'converter');
application.options.setValue('exclude', [ '**/class.ts', '**/access.ts' ]);
const expanded = application.expandInputFiles([inputFiles]);

Assert.equal(expanded.indexOf(Path.join(inputFiles, 'class', 'class.ts')), -1);
Assert.equal(expanded.indexOf(Path.join(inputFiles, 'access', 'access.ts')), -1);
Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(!expanded.includes(Path.join(inputFiles, 'class', 'class.ts')));
Assert.ok(!expanded.includes(Path.join(inputFiles, 'access', 'access.ts')));
Assert.ok(!expanded.includes(inputFiles));
});
it('supports excluding directories beginning with dots', function() {
const inputFiles = __dirname;
application.options.setValue('exclude', '**/+(.dot)/**');
const expanded = application.expandInputFiles([inputFiles]);

Assert.equal(expanded.indexOf(Path.join(inputFiles, '.dot', 'index.d.ts')), -1);
Assert.equal(expanded.indexOf(inputFiles), -1);
Assert.ok(!expanded.includes(Path.join(inputFiles, '.dot', 'index.d.ts')));
Assert.ok(!expanded.includes(inputFiles));
});
it('Honors the exclude option even if a module is imported', () => {
application.options.setValue('exclude', '**/b.d.ts');
Expand Down