From 6da5b604a9abf330874520fdeffa156accec83b2 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Thu, 4 May 2023 17:06:29 -0600 Subject: [PATCH] add reopenIssuesFromTODOs.js, https://github.com/phetsims/chipper/issues/946 --- eslint/rules/todo-should-have-issue.js | 32 ++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/eslint/rules/todo-should-have-issue.js b/eslint/rules/todo-should-have-issue.js index a38892c3..8f9c531d 100644 --- a/eslint/rules/todo-should-have-issue.js +++ b/eslint/rules/todo-should-have-issue.js @@ -8,10 +8,20 @@ * @copyright 2015 University of Colorado Boulder */ +const process = require( 'process' ); +const path = require( 'path' ); +const fs = require( 'fs' ); + +const issueShorthandRegex = /#(\d+)/; +const urlRegex = /https:\/\/github.com\/phetsims\/[^\s]*/; +const filename = 'issuesFromTODOs.txt'; +const todoIssuesFilepath = path.resolve( __dirname, `../../dist/${filename}` ); + module.exports = function( context ) { return { Program: function() { + const filename = context.getFilename(); // Explicitly ignore files from the simula-rasa repo. simula-rasa is the template for new simulations that are // created using 'grunt create-sim'. simula-rasa's code contains TODOs that should be addressed by the creator @@ -30,8 +40,8 @@ module.exports = function( context ) { if ( comment.value.indexOf( 'TODO' ) >= 0 ) { // '#' followed by any number of digits - const missingIssueNumber = comment.value.search( /#\d+/ ) === -1; - const missingLink = comment.value.indexOf( 'https://github.com/phetsims/' ) === -1; + const missingIssueNumber = comment.value.search( issueShorthandRegex ) === -1; + const missingLink = comment.value.search( urlRegex ) === -1; if ( missingLink && missingIssueNumber ) { context.report( { @@ -40,6 +50,24 @@ module.exports = function( context ) { message: `TODO should have an issue: ${comment.value}` } ); } + else if ( process.env.saveTODOIssues ) { + let url = null; + const urlMatch = comment.value.match( urlRegex ); + if ( urlMatch ) { + url = urlMatch[ 0 ]; + } + + const issueShorthandRegex = /#(\d+)/; + const issueShorthandMatch = comment.value.match( issueShorthandRegex ); + const repoNameMatch = filename.match( /[\\/]([\w-]+)[\\/]js[\\/]/ ); + if ( issueShorthandMatch && repoNameMatch ) { + url = `https://github.com/phetsims/${repoNameMatch[ 1 ]}/issues/${issueShorthandMatch[ 1 ]}`; + } + + if ( url ) { + fs.writeFileSync( todoIssuesFilepath, fs.readFileSync( todoIssuesFilepath ).toString() + `${url}\n` ); + } + } } } }