From b7f81ae266f369fc5ab2aa6f28e69c36c25ee652 Mon Sep 17 00:00:00 2001 From: Bamieh Date: Mon, 20 Nov 2017 03:22:28 +0200 Subject: [PATCH] tools: add lint fixer for `require-buffer` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds `require-buffer` lint fixer. If the buffer module is not required while `Buffer` is used, require the `buffer` module. If the file has a `'use strict';` line, add the require after it on a separate line. If the file does not have any (currently impossible with the `strict` rule) add it after the first comment (before the real code starts). Fixes: https://github.com/nodejs/node/issues/16636 PR-URL: https://github.com/nodejs/node/pull/17144 Reviewed-By: Michaƫl Zasso --- test/parallel/test-eslint-require-buffer.js | 29 ++++++++++++++++++--- tools/eslint-rules/require-buffer.js | 18 ++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-eslint-require-buffer.js b/test/parallel/test-eslint-require-buffer.js index 21be41f995a44e..282f983c782d4e 100644 --- a/test/parallel/test-eslint-require-buffer.js +++ b/test/parallel/test-eslint-require-buffer.js @@ -12,15 +12,36 @@ const ruleTester = new RuleTester({ const message = "Use const Buffer = require('buffer').Buffer; " + 'at the beginning of this file'; +const useStrict = '\'use strict\';\n\n'; +const bufferModule = 'const { Buffer } = require(\'buffer\');\n'; +const mockComment = '// Some Comment\n//\n// Another Comment\n\n'; +const useBuffer = 'Buffer;'; ruleTester.run('require-buffer', rule, { valid: [ 'foo', - 'const Buffer = require("Buffer"); Buffer;' + 'const Buffer = require("Buffer"); Buffer;', + 'const { Buffer } = require(\'buffer\'); Buffer;', ], invalid: [ { - code: 'Buffer;', - errors: [{ message }] - } + code: useBuffer, + errors: [{ message }], + output: bufferModule + useBuffer, + }, + { + code: useStrict + useBuffer, + errors: [{ message }], + output: useStrict + bufferModule + useBuffer, + }, + { + code: mockComment + useBuffer, + errors: [{ message }], + output: mockComment + bufferModule + useBuffer, + }, + { + code: mockComment + useStrict + useBuffer, + errors: [{ message }], + output: mockComment + useStrict + bufferModule + useBuffer, + }, ] }); diff --git a/tools/eslint-rules/require-buffer.js b/tools/eslint-rules/require-buffer.js index c9818cb758f7bd..c6040b43a181fb 100644 --- a/tools/eslint-rules/require-buffer.js +++ b/tools/eslint-rules/require-buffer.js @@ -1,10 +1,26 @@ 'use strict'; +const BUFFER_REQUIRE = 'const { Buffer } = require(\'buffer\');\n'; module.exports = function(context) { + function flagIt(reference) { const msg = 'Use const Buffer = require(\'buffer\').Buffer; ' + 'at the beginning of this file'; - context.report(reference.identifier, msg); + + context.report({ + node: reference.identifier, + message: msg, + fix: (fixer) => { + const sourceCode = context.getSourceCode(); + + const useStrict = /'use strict';\n\n?/g; + const hasUseStrict = !!useStrict.exec(sourceCode.text); + const firstLOC = sourceCode.ast.range[0]; + const rangeNeedle = hasUseStrict ? useStrict.lastIndex : firstLOC; + + return fixer.insertTextBeforeRange([rangeNeedle], BUFFER_REQUIRE); + } + }); } return {