Report unused rules (Closes #200)
This commit is contained in:
parent
851d8edfdd
commit
6500189d58
|
@ -6,6 +6,7 @@ const generateJS = require( "./passes/generate-js" );
|
||||||
const removeProxyRules = require( "./passes/remove-proxy-rules" );
|
const removeProxyRules = require( "./passes/remove-proxy-rules" );
|
||||||
const reportDuplicateLabels = require( "./passes/report-duplicate-labels" );
|
const reportDuplicateLabels = require( "./passes/report-duplicate-labels" );
|
||||||
const reportDuplicateRules = require( "./passes/report-duplicate-rules" );
|
const reportDuplicateRules = require( "./passes/report-duplicate-rules" );
|
||||||
|
const reportUnusedRules = require( "./passes/report-unused-rules" );
|
||||||
const reportInfiniteRecursion = require( "./passes/report-infinite-recursion" );
|
const reportInfiniteRecursion = require( "./passes/report-infinite-recursion" );
|
||||||
const reportInfiniteRepetition = require( "./passes/report-infinite-repetition" );
|
const reportInfiniteRepetition = require( "./passes/report-infinite-repetition" );
|
||||||
const reportUndefinedRules = require( "./passes/report-undefined-rules" );
|
const reportUndefinedRules = require( "./passes/report-undefined-rules" );
|
||||||
|
@ -37,6 +38,7 @@ const compiler = {
|
||||||
check: {
|
check: {
|
||||||
reportUndefinedRules: reportUndefinedRules,
|
reportUndefinedRules: reportUndefinedRules,
|
||||||
reportDuplicateRules: reportDuplicateRules,
|
reportDuplicateRules: reportDuplicateRules,
|
||||||
|
reportUnusedRules: reportUnusedRules,
|
||||||
reportDuplicateLabels: reportDuplicateLabels,
|
reportDuplicateLabels: reportDuplicateLabels,
|
||||||
reportInfiniteRecursion: reportInfiniteRecursion,
|
reportInfiniteRecursion: reportInfiniteRecursion,
|
||||||
reportInfiniteRepetition: reportInfiniteRepetition
|
reportInfiniteRepetition: reportInfiniteRepetition
|
||||||
|
|
31
lib/compiler/passes/report-unused-rules.js
Normal file
31
lib/compiler/passes/report-unused-rules.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// Checks that all rules are used.
|
||||||
|
function reportUnusedRules( ast, session, options ) {
|
||||||
|
|
||||||
|
const used = {};
|
||||||
|
function yes( node ) {
|
||||||
|
|
||||||
|
used[ node.name || node ] = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
options.allowedStartRules.forEach( yes );
|
||||||
|
session.buildVisitor( { rule_ref: yes } )( ast );
|
||||||
|
|
||||||
|
ast.rules.forEach( rule => {
|
||||||
|
|
||||||
|
if ( used[ rule.name ] !== true ) {
|
||||||
|
|
||||||
|
session.warn(
|
||||||
|
`Rule "${ rule.name }" is not referenced.`,
|
||||||
|
rule.location
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = reportUnusedRules;
|
64
test/spec/unit/compiler/passes/report-unused-rules.spec.js
Normal file
64
test/spec/unit/compiler/passes/report-unused-rules.spec.js
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const chai = require( "chai" );
|
||||||
|
const helpers = require( "./helpers" );
|
||||||
|
const pass = require( "pegjs-dev" ).compiler.passes.check.reportUnusedRules;
|
||||||
|
|
||||||
|
chai.use( helpers );
|
||||||
|
|
||||||
|
const expect = chai.expect;
|
||||||
|
|
||||||
|
describe( "compiler pass |reportUnusedRules|", function () {
|
||||||
|
|
||||||
|
it( "should report rules that are not referenced", function () {
|
||||||
|
|
||||||
|
expect( pass ).to.reportWarning(
|
||||||
|
`
|
||||||
|
start = .
|
||||||
|
unused = .
|
||||||
|
`,
|
||||||
|
`Rule "unused" is not referenced.`
|
||||||
|
);
|
||||||
|
|
||||||
|
expect( pass ).to.reportWarning(
|
||||||
|
`
|
||||||
|
start = .
|
||||||
|
unused = .
|
||||||
|
used = .
|
||||||
|
`,
|
||||||
|
[
|
||||||
|
`Rule "used" is not referenced.`,
|
||||||
|
`Rule "unused" is not referenced.`
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( "does not report rules that are referenced", function () {
|
||||||
|
|
||||||
|
expect( pass ).not.to.reportWarning( `start = .` );
|
||||||
|
|
||||||
|
expect( pass ).not.to.reportWarning( `
|
||||||
|
start = used
|
||||||
|
used = .
|
||||||
|
` );
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
it( "does not report any rules that the generated parser starts parsing from", function () {
|
||||||
|
|
||||||
|
expect( pass ).not.to.reportWarning(
|
||||||
|
`
|
||||||
|
a = "x"
|
||||||
|
b = a
|
||||||
|
c = .+
|
||||||
|
`,
|
||||||
|
null,
|
||||||
|
{
|
||||||
|
allowedStartRules: [ "b", "c" ]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
} );
|
Loading…
Reference in a new issue