From 4f1571b75c5a39a6096ec4d2f197cda82481d81b Mon Sep 17 00:00:00 2001 From: ssube Date: Tue, 19 Nov 2019 06:03:48 -0600 Subject: [PATCH] lint(tests): clean up magic numbers --- config/eslint.json | 23 +---------------------- src/app.ts | 3 ++- src/rule/index.ts | 6 +++--- test/TestApp.ts | 5 ++++- test/parser/TestYamlParser.ts | 3 ++- test/rule/TestLoadRule.ts | 6 ++++-- test/rule/TestRule.ts | 12 +++++++++--- test/rule/TestRuleVisitor.ts | 11 ++++++----- test/rule/TestSchemaRule.ts | 2 +- 9 files changed, 32 insertions(+), 39 deletions(-) diff --git a/config/eslint.json b/config/eslint.json index a388976..d670859 100644 --- a/config/eslint.json +++ b/config/eslint.json @@ -212,29 +212,9 @@ "error", { "ignore": [ - -3, - -2, - -1, 0, 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 20, - 30, - 40, - 50, - 60, - 70, - 80, - 90, - 100 + 10 ] } ], @@ -344,7 +324,6 @@ true, "check-multiline-start" ], - "no-boolean-literal-compare": true, "no-dynamic-delete": true, "no-inferred-empty-object-type": true, "no-reference-import": true, diff --git a/src/app.ts b/src/app.ts index c45a215..efbd60e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -10,12 +10,13 @@ import { readSource, writeSource } from './source'; import { VERSION_INFO } from './version'; import { VisitorContext } from './visitor/VisitorContext'; +const ARGS_START = 2; export const STATUS_SUCCESS = 0; export const STATUS_ERROR = 1; export const STATUS_MAX = 255; export async function main(argv: Array): Promise { - const { args, mode } = await parseArgs(argv.slice(2)); + const { args, mode } = await parseArgs(argv.slice(ARGS_START)); if (mode === MODE.complete) { showCompletionScript(); return STATUS_SUCCESS; diff --git a/src/rule/index.ts b/src/rule/index.ts index c253ae7..f64d913 100644 --- a/src/rule/index.ts +++ b/src/rule/index.ts @@ -200,13 +200,13 @@ export async function resolveRules(rules: Array, selector: RuleSelector): for (const r of rules) { let active = false; + const includedTags = intersection(selector.includeTag, r.tags); active = active || selector.includeLevel.includes(r.level); active = active || selector.includeName.includes(r.name); - const includedTags = intersection(selector.includeTag, r.tags); active = active || includedTags.length > 0; - active = active && (!selector.excludeLevel.includes(r.level)); - active = active && (!selector.excludeName.includes(r.name)); + active = active && (selector.excludeLevel.includes(r.level) === false); + active = active && (selector.excludeName.includes(r.name) === false); const excludedTags = intersection(selector.excludeTag, r.tags); active = active && (excludedTags.length === 0); diff --git a/test/TestApp.ts b/test/TestApp.ts index 2f0419c..b2eef9a 100644 --- a/test/TestApp.ts +++ b/test/TestApp.ts @@ -127,9 +127,12 @@ describeLeaks('main app', async () => { it('should validate config before running', async () => { mockFs(TEST_FILES); + const [configPath, configDocs, configName] = TEST_ARGS_CONFIG; const status = await main([ ...TEST_ARGS_PRE, - ...TEST_ARGS_CONFIG.slice(0, 3), + configPath, + configDocs, + configName, 'partial.yml', ...TEST_ARGS_RULES, ...TEST_ARGS_SOURCE, diff --git a/test/parser/TestYamlParser.ts b/test/parser/TestYamlParser.ts index 1ff4097..ac5557d 100644 --- a/test/parser/TestYamlParser.ts +++ b/test/parser/TestYamlParser.ts @@ -22,7 +22,8 @@ bar: {} `); expect(Array.isArray(data)).to.equal(true); - expect(data.length).to.equal(2); + const EXPECTED_DOCS = 2; + expect(data.length).to.equal(EXPECTED_DOCS); }); }); }); diff --git a/test/rule/TestLoadRule.ts b/test/rule/TestLoadRule.ts index e7aa052..669a6c4 100644 --- a/test/rule/TestLoadRule.ts +++ b/test/rule/TestLoadRule.ts @@ -164,7 +164,8 @@ describeLeaks('load rule path helper', async () => { mockFS.restore(); - expect(rules.length).to.equal(2); + const EXPECTED_RULES = 2; + expect(rules.length).to.equal(EXPECTED_RULES); }); }); @@ -223,7 +224,8 @@ describeLeaks('load rule module helper', async () => { $ref: 'test-rules#/definitions/foo', }); - expect(schema(2)).to.equal(false); + const NUMBER_VALUE = 2; + expect(schema(NUMBER_VALUE)).to.equal(false); expect(schema('foo')).to.equal(true); }); diff --git a/test/rule/TestRule.ts b/test/rule/TestRule.ts index 88c42b7..143ef5e 100644 --- a/test/rule/TestRule.ts +++ b/test/rule/TestRule.ts @@ -45,8 +45,10 @@ describeLeaks('rule resolver', async () => { includeLevel: [LogLevel.Warn], })); - expect(info.length).to.equal(2); + const EXPECTED_RULES = 2; + expect(info.length).to.equal(EXPECTED_RULES); expect(info[0]).to.equal(TEST_RULES[1]); + /* eslint-disable-next-line no-magic-numbers */ expect(info[1]).to.equal(TEST_RULES[2]); }); }); @@ -68,8 +70,10 @@ describeLeaks('rule resolver', async () => { includeTag: ['test'], })); - expect(rules.length).to.equal(2); + const EXPECTED_RULES = 2; + expect(rules.length).to.equal(EXPECTED_RULES); expect(rules[0]).to.equal(TEST_RULES[1]); + /* eslint-disable-next-line no-magic-numbers */ expect(rules[1]).to.equal(TEST_RULES[2]); }); }); @@ -81,8 +85,10 @@ describeLeaks('rule resolver', async () => { includeTag: ['all'], })); - expect(rules.length).to.equal(2); + const EXPECTED_RULES = 2; + expect(rules.length).to.equal(EXPECTED_RULES); expect(rules[0]).to.equal(TEST_RULES[1]); + /* eslint-disable-next-line no-magic-numbers */ expect(rules[1]).to.equal(TEST_RULES[2]); }); }); diff --git a/test/rule/TestRuleVisitor.ts b/test/rule/TestRuleVisitor.ts index 444414d..4f0c557 100644 --- a/test/rule/TestRuleVisitor.ts +++ b/test/rule/TestRuleVisitor.ts @@ -91,7 +91,7 @@ describeLeaks('rule visitor', async () => { }, }); const data = { - foo: [1, 2, 3], + foo: [Math.random(), Math.random(), Math.random()], }; const rule = new SchemaRule({ check: {}, @@ -114,7 +114,7 @@ describeLeaks('rule visitor', async () => { await visitor.visit(ctx, data); expect(pickSpy).to.have.callCount(1).and.to.have.been.calledWithExactly(ctx, data); - expect(visitStub).to.have.callCount(3); + expect(visitStub).to.have.callCount(data.foo.length); }); itLeaks('should visit individual items', async () => { @@ -127,7 +127,7 @@ describeLeaks('rule visitor', async () => { }, }); const data = { - foo: [1, 2, 3], + foo: [Math.random(), Math.random(), Math.random()], }; const rule = new SchemaRule({ check: {}, @@ -152,8 +152,9 @@ describeLeaks('rule visitor', async () => { }); await visitor.visit(ctx, data); - expect(visitStub).to.have.callCount(3); - expect(ctx.errors.length).to.equal(3); + const EXPECTED_VISITS = 3; + expect(visitStub).to.have.callCount(EXPECTED_VISITS); + expect(ctx.errors.length).to.equal(EXPECTED_VISITS); }); itLeaks('should not pick items', async () => { diff --git a/test/rule/TestSchemaRule.ts b/test/rule/TestSchemaRule.ts index c4860ac..bbb566a 100644 --- a/test/rule/TestSchemaRule.ts +++ b/test/rule/TestSchemaRule.ts @@ -113,7 +113,7 @@ describeLeaks('schema rule', async () => { tags: [], }); const results = await rule.pick(ctx, { - foo: [1, 2, 3], + foo: [Math.random(), Math.random(), Math.random()], }); expect(Array.isArray(results)).to.equal(true); });