Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: add buffer-constructor eslint rule #5740

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Mar 16, 2016

Pull Request check-list

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to [CONTRIBUTING.md][0]?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?
  • Is a documentation update included (if this change modifies
    existing APIs, or introduces new ones)?

Affected core subsystem(s)

tools (eslint)

Description of change

Now that the Buffer.alloc, allocUnsafe, and from methods have landed,
add a linting rule that requires their use within lib. Tests and
benchmarks are explicitly excluded by the rule.

Requires: #4682

/cc @Trott @ChALkeR @nodejs/testing

@jasnell jasnell added semver-minor PRs that contain new features and should be released in the next minor version. tools Issues and PRs related to the tools directory. dont-land-on-v5.x labels Mar 16, 2016
@Trott
Copy link
Member

Trott commented Mar 16, 2016

This custom rule isn't working for me, but maybe I'm misunderstanding what it is supposed to capture. Can you provide a code sample that this should flag? I've been assuming something like this is what you're trying to avoid:

'use strict';

var foo = new Buffer(12).fill('a');
console.log(foo);

'or Buffer.from()';

function test(context, node, which) {
if (node.type === which && node.callee.name === 'Buffer') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible for node.type === which to be false in this situation. I don't think you need the which variable at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@jasnell
Copy link
Member Author

jasnell commented Mar 16, 2016

@Trott .. thank you for that, I'm afraid I'm not that familiar with eslint yet so those comments were very helpful. As for the rule itself, yes, it should catch uses of new Buffer() and Buffer(), but in our source it should only apply to the lib/*.js and src/*.js directory (to catch src/nodej.s).

The rule appears to be working just fine for me on this end. If I add a var foo = new Buffer(10).fill(0) to lib/assert.js for instance, I get:

bash-3.2$ make jslint
./node tools/eslint/bin/eslint.js benchmark lib src test tools/doc \
                tools/eslint-rules --rulesdir tools/eslint-rules

/Users/james/Node/main/node/lib/assert.js
  26:5   error  'foo' is defined but never used                                                                                                no-unused-vars
  26:11  error  Use of the Buffer() constructor has been deprecated. Please use either Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from()  buffer-constructor


✖ 2 problems (2 errors, 0 warnings)

make: *** [jslint] Error 1

Changing that to Buffer.allocUnsafe() removes the buffer-constructor error.

@jasnell
Copy link
Member Author

jasnell commented Mar 16, 2016

Updated to address that feedback :-)

@Trott
Copy link
Member

Trott commented Mar 16, 2016

LGTM

Now that the Buffer.alloc, allocUnsafe, and from methods have landed,
add a linting rule that requires their use within lib. Tests and
benchmarks are explicitly excluded by the rule.
@jasnell
Copy link
Member Author

jasnell commented Mar 16, 2016

@jasnell
Copy link
Member Author

jasnell commented Mar 17, 2016

CI is green.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 18, 2016

LGTM, but perhaps you should try getting this upstreamed to help the community.

@jasnell
Copy link
Member Author

jasnell commented Mar 18, 2016

That's a possibility. I'd want to get this landed here first tho. If it did hit upstream we could revert and use that later.

@jasnell
Copy link
Member Author

jasnell commented Mar 19, 2016

Opened an issue to get this upstreamed. Will land this now. If/When it lands up stream we can update to use that version

jasnell added a commit that referenced this pull request Mar 19, 2016
Now that the Buffer.alloc, allocUnsafe, and from methods have landed,
add a linting rule that requires their use within lib. Tests and
benchmarks are explicitly excluded by the rule.

PR-URL: #5740
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@jasnell
Copy link
Member Author

jasnell commented Mar 19, 2016

Landed in 9e30129

@jasnell jasnell closed this Mar 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor PRs that contain new features and should be released in the next minor version. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants