Skip to content

Commit

Permalink
Enable prefer-includes eslint rule and pre-apply fixes (#10272)
Browse files Browse the repository at this point in the history
From the documentation on the rule:

Until ES5, we were using String#indexOf method to check whether a string contains an arbitrary substring or not. Until ES2015, we were using Array#indexOf method to check whether an array contains an arbitrary value or not.

ES2015 has added String#includes and ES2016 has added Array#includes. It makes code more understandable if we use those includes methods for the purpose.
  • Loading branch information
tylerbutler authored May 17, 2022
1 parent 090b6e9 commit 66f305a
Show file tree
Hide file tree
Showing 18 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions common/build/eslint-config-fluid/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ module.exports = {
// This rule ensures that our Intellisense looks good by verifying the TSDoc syntax.
"tsdoc/syntax": "error",

"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-optional-chain": "error",
},
"overrides": [
Expand Down
3 changes: 3 additions & 0 deletions common/build/eslint-config-fluid/printed-configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
"@typescript-eslint/prefer-function-type": [
"error"
],
"@typescript-eslint/prefer-includes": [
"error"
],
"@typescript-eslint/prefer-namespace-keyword": [
"error"
],
Expand Down
3 changes: 3 additions & 0 deletions common/build/eslint-config-fluid/printed-configs/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
"@typescript-eslint/prefer-function-type": [
"error"
],
"@typescript-eslint/prefer-includes": [
"error"
],
"@typescript-eslint/prefer-namespace-keyword": [
"error"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"project": ["./tsconfig.json", "./src/test/tsconfig.json"]
},
"rules": {
// Many rules are disabled in PropertyDDS projects. See https://github.com/microsoft/FluidFramework/pull/10272
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/consistent-type-definitions": "off",
Expand All @@ -27,6 +28,7 @@ module.exports = {
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/prefer-for-of": "off",
"@typescript-eslint/prefer-includes": "off",
"@typescript-eslint/prefer-optional-chain": "off",
"@typescript-eslint/quotes": "off",
"@typescript-eslint/restrict-plus-operands": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Collection<T> {

if (_.isArray(in_filterKey)) {
filterCb = function(in_key, in_item) {
if (in_filterKey.indexOf(in_key) >= 0) {
if (in_filterKey.includes(in_key)) {
rtn.add(in_key, in_item);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class HTTPError extends Error {
const isFirefox = typeof window !== "undefined" &&
typeof window.navigator !== "undefined" &&
typeof window.navigator.userAgent !== "undefined" &&
window.navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
window.navigator.userAgent.toLowerCase().includes("firefox");

return isFirefox ? `${this.message}, stack:${stack}` : `stack:${stack}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const copyHandler = (rowData: IInspectorRow, ref: React.MutableRefObject<HTMLTex
const isStaticProperty = (parent: BaseProperty, rowName: string) => {
if (typeof (parent as NodeProperty).getDynamicIds === "function") {
const dynamicIds = (parent as NodeProperty).getDynamicIds();
if (dynamicIds.indexOf(rowName) > -1) {
if (dynamicIds.includes(rowName)) {
return false;
}
} else if (parent.getContext() !== "single") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const NewDataForm: React.FunctionComponent<INewDataFormProps> = (props) =
</Button>
);

const isSiblingFound = siblingIds.indexOf(inputName) >= 0;
const isSiblingFound = siblingIds.includes(inputName);
const createBtn = (
<Button
id="createDataButton"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const fetchRegisteredTemplates = () => {
const primitiveLocalTemplates: string[] = [];
const customLocalTemplates: string[] = [];
Object.keys(localTemplates).forEach((item) => {
if (!TypeIdHelper.isTemplateTypeid(item) && EXCLUDE_PROPS.indexOf(item) === -1) {
if (!TypeIdHelper.isTemplateTypeid(item) && !EXCLUDE_PROPS.includes(item)) {
primitiveLocalTemplates.push(item);
} else if (TypeIdHelper.isTemplateTypeid(item)) {
customLocalTemplates.push(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"project": ["./tsconfig.json", "./src/test/tsconfig.json"]
},
"rules": {
// Many rules are disabled in PropertyDDS projects. See https://github.com/microsoft/FluidFramework/pull/10272
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/comma-spacing": "off",
"@typescript-eslint/dot-notation": "off",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class PropertyTemplateWrapper {
const isCreationType = (typeid) => {
const creationTypes =
['ContainerProperty', 'NodeProperty', 'Enum', 'BinaryProperty', 'Binary', 'RepositoryReferenceProperty'];
return creationTypes.indexOf(typeid) !== -1;
return creationTypes.includes(typeid);
};

if (isCreationType(in_typeid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
"project": ["./tsconfig.json", "./src/test/tsconfig.json"]
},
"rules": {
// TODO(marcus): remove the linting issues
// Many rules are disabled in PropertyDDS projects. See https://github.com/microsoft/FluidFramework/pull/10272
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
Expand Down
2 changes: 1 addition & 1 deletion experimental/dds/tree/src/Forest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export class Forest {
const parent = this.get(node.parentId);
const trait = parent.traits.get(node.traitParent);
assert(trait !== undefined);
assert(trait.indexOf(node.identifier) >= 0, 'node is parented incorrectly');
assert(trait.includes(node.identifier), 'node is parented incorrectly');
}

for (const trait of node.traits.values()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/odsp-driver/src/odspUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function fetchHelper(
"Fetch Timeout (AbortError)", OdspErrorType.fetchTimeout, { driverVersion });
}
// TCP/IP timeout
if (errorText.indexOf("ETIMEDOUT") !== -1) {
if (errorText.includes("ETIMEDOUT")) {
throw new RetryableError(
"Fetch Timeout (ETIMEDOUT)", OdspErrorType.fetchTimeout, { driverVersion });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/driver-utils/src/blobAggregationStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class BlobAggregationStorage extends SnapshotExtractor implements IDocume
}
// Ensure only whole data stores can be reused, no reusing at deeper level!
assert(level === 0, 0x0fc /* "tree reuse at lower level" */);
assert(handlePath.indexOf("/") === -1,
assert(!handlePath.includes("/"),
0x0fd /* "data stores are writing incremental summaries!" */);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/container-runtime/src/dataStoreContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export abstract class FluidDataStoreContext extends TypedEventEmitter<IFluidData

// URIs use slashes as delimiters. Handles use URIs.
// Thus having slashes in types almost guarantees trouble down the road!
assert(this.id.indexOf("/") === -1, 0x13a /* `Data store ID contains slash: ${id}` */);
assert(!this.id.includes("/"), 0x13a /* `Data store ID contains slash: ${id}` */);

this._attachState = this.containerRuntime.attachState !== AttachState.Detached && this.existing ?
this.containerRuntime.attachState : AttachState.Detached;
Expand Down
2 changes: 1 addition & 1 deletion packages/test/test-service-load/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FileLogger extends TelemetryLogger implements ITelemetryBufferedLogger {
send(event: ITelemetryBaseEvent): void {
if (typeof event.testCategoryOverride === "string") {
event.category = event.testCategoryOverride;
} else if (typeof event.message === "string" && event.message.indexOf("FaultInjectionNack") > -1) {
} else if (typeof event.message === "string" && event.message.includes("FaultInjectionNack")) {
event.category = "generic";
}
this.baseLogger?.send({ ...event, hostName: pkgName });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ describe("normalizeError", () => {
assert.equal(actual.message, expected.message, "message should match");
const actualStack = actual.stack;
assert(actualStack !== undefined, "stack should be present as a string");
if (actualStack.indexOf("at normalizeError") >= 0) { // This indicates the stack was populated naturally by new SimpleFluidError
if (actualStack.includes("at normalizeError")) { // This indicates the stack was populated naturally by new SimpleFluidError
assert.equal(expected.stack, "<<natural stack>>", "<<natural stack>> hint should be used if not overwritten");
expected.withExpectedTelemetryProps({ stack: actualStack });
} else {
Expand Down

0 comments on commit 66f305a

Please sign in to comment.