From 2ab4b7d4a7d130f4e45e0288d7952b4120867e3f Mon Sep 17 00:00:00 2001 From: bkaptijn Date: Wed, 18 Dec 2019 13:19:16 +0100 Subject: [PATCH 1/2] fixes #74, introduces LR0004 WARNING diagnostic on unused facts (not referenced) --- package-lock.json | 2 +- src/modelValidator.js | 28 ++++++++++++++++++-- test/modelValidator.spec.js | 53 ++++++++++++++++++++++++++++--------- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index a962054..f663799 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@discipl/law-reg", - "version": "0.3.3", + "version": "0.3.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/modelValidator.js b/src/modelValidator.js index 6935ec8..5dd1719 100644 --- a/src/modelValidator.js +++ b/src/modelValidator.js @@ -252,7 +252,7 @@ class ModelValidator { const node = jsonc.findNodeAtLocation(this.tree, [expressionCheckPath[0], index, expressionCheckPath[1]]) // console.log("ExpCheck en index", expressionCheckPath, index); if (node && typeof node.value === 'string') { - // console.log("Node", node); + // console.log("Node", node) return this._validateExpression(node.value, node.offset, expressionCheckPath[2]) } else { return this._validateParsedExpressionNode(node) @@ -260,7 +260,11 @@ class ModelValidator { }).reduce(concat, []) }).reduce(concat, []) - return createTerminateErrors.concat(expressionErrors) + const unusedFactErrors = this.model.facts.map((fact) => { + return this._checkFactUsage(fact) + }).reduce(concat, []) + + return createTerminateErrors.concat(expressionErrors).concat(unusedFactErrors) } _checkCreateTerminate (referenceString, node) { @@ -373,6 +377,26 @@ class ModelValidator { } } } + + _checkFactUsage(fact) { + if((this.referencePaths[fact.fact]) && (this.referencePaths[fact.fact].length==1)) { + const node = jsonc.findNodeAtLocation(this.tree, this.identifierPaths[fact.fact]) + const beginPosition = node.offset + const endPosition = node.offset + node.length + const path = jsonc.getNodePath(node) + return [{ + code: 'LR0004', + message: 'Unused fact: ' + fact.fact, + offset: [beginPosition, endPosition], + severity: 'WARNING', + source: fact.fact, + path : path + }] + } else { + return [] + } + } + } export { ModelValidator } diff --git a/test/modelValidator.spec.js b/test/modelValidator.spec.js index dbe0849..10d916c 100644 --- a/test/modelValidator.spec.js +++ b/test/modelValidator.spec.js @@ -66,7 +66,7 @@ describe('The Flint Model validator', () => { it('should find errors with improperly named acts, facts, duties', async () => { const model = JSON.stringify({ - 'acts': [{ 'act': 'test' }, { 'act': '<>' }], + 'acts': [{ 'act': 'test' }, { 'act': '<>' }, { 'act': '<>', 'preconditions': '[fact]' }], 'facts': [{ 'fact': 'test' }, { 'fact': '[test' }, { 'fact': '[fact]' }], 'duties': [{ 'duty': 'test' }, { 'duty': '' }] }) @@ -79,7 +79,7 @@ describe('The Flint Model validator', () => { 'code': 'LR0001', 'source': 'test', 'message': 'Invalid name for identifier', - 'offset': [139, 145], + 'offset': [183, 189], 'path': [ 'duties', 0, @@ -258,7 +258,7 @@ describe('The Flint Model validator', () => { it('should find undefined facts used in acts in fact functions', async () => { const model = JSON.stringify({ - 'acts': [], + 'acts': [{ 'act': '<>', 'preconditions': '[factname]' }], 'facts': [{ 'fact': '[factname]', 'function': '([cats] OF [dogs]) EN [sunshine]' }], 'duties': [] }) @@ -271,8 +271,8 @@ describe('The Flint Model validator', () => { 'code': 'LR0002', 'message': 'Undefined item', 'offset': [ - 54, - 60 + 100, + 106 ], 'path': [ 'facts', @@ -286,8 +286,8 @@ describe('The Flint Model validator', () => { 'code': 'LR0002', 'message': 'Undefined item', 'offset': [ - 64, - 70 + 110, + 116 ], 'path': [ 'facts', @@ -301,8 +301,8 @@ describe('The Flint Model validator', () => { 'code': 'LR0002', 'message': 'Undefined item', 'offset': [ - 75, - 85 + 121, + 131 ], 'path': [ 'facts', @@ -318,7 +318,7 @@ describe('The Flint Model validator', () => { it('should find undefined facts used in lists', async () => { const model = JSON.stringify({ - 'acts': [], + 'acts': [{ 'act': '<>', 'preconditions': '[factname]' }], 'facts': [{ 'fact': '[factname]', 'function': { 'name': 'SomeList', @@ -337,8 +337,8 @@ describe('The Flint Model validator', () => { 'code': 'LR0002', 'message': 'Undefined item', 'offset': [ - 100, - 106 + 146, + 152 ], 'path': [ 'facts', @@ -351,4 +351,33 @@ describe('The Flint Model validator', () => { } ]) }) + + it('should find unused facts (without acts relating to them)', async () => { + const model = JSON.stringify({ + 'acts': [{ 'act': '<>', 'preconditions': '[usedFact]' }], + 'facts': [{ 'fact': '[usedFact]'},{ 'fact': '[unusedFact]'}], + 'duties': [] + }) + const modelValidator = new ModelValidator(model) + + const errors = modelValidator.getDiagnostics() + + expect(errors).to.deep.equal([ + { + 'code': 'LR0004', + 'message': 'Unused fact: [unusedFact]', + 'offset': [ + 96, + 110 + ], + 'path': [ + 'facts', + 1, + 'fact' + ], + 'severity': 'WARNING', + 'source': '[unusedFact]' + } + ]) + }) }) From 5c0df84dfd56d139e85cad1b40050dcb551c29b5 Mon Sep 17 00:00:00 2001 From: bkaptijn Date: Wed, 18 Dec 2019 13:24:09 +0100 Subject: [PATCH 2/2] fixes #74, linter fix --- src/modelValidator.js | 9 ++++----- test/modelValidator.spec.js | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/modelValidator.js b/src/modelValidator.js index 5dd1719..a2e32fb 100644 --- a/src/modelValidator.js +++ b/src/modelValidator.js @@ -378,8 +378,8 @@ class ModelValidator { } } - _checkFactUsage(fact) { - if((this.referencePaths[fact.fact]) && (this.referencePaths[fact.fact].length==1)) { + _checkFactUsage (fact) { + if ((this.referencePaths[fact.fact]) && (this.referencePaths[fact.fact].length === 1)) { const node = jsonc.findNodeAtLocation(this.tree, this.identifierPaths[fact.fact]) const beginPosition = node.offset const endPosition = node.offset + node.length @@ -390,13 +390,12 @@ class ModelValidator { offset: [beginPosition, endPosition], severity: 'WARNING', source: fact.fact, - path : path + path: path }] } else { - return [] + return [] } } - } export { ModelValidator } diff --git a/test/modelValidator.spec.js b/test/modelValidator.spec.js index 10d916c..f374424 100644 --- a/test/modelValidator.spec.js +++ b/test/modelValidator.spec.js @@ -355,7 +355,7 @@ describe('The Flint Model validator', () => { it('should find unused facts (without acts relating to them)', async () => { const model = JSON.stringify({ 'acts': [{ 'act': '<>', 'preconditions': '[usedFact]' }], - 'facts': [{ 'fact': '[usedFact]'},{ 'fact': '[unusedFact]'}], + 'facts': [{ 'fact': '[usedFact]' }, { 'fact': '[unusedFact]' }], 'duties': [] }) const modelValidator = new ModelValidator(model)