Skip to content
Prev Previous commit
Fix: ESLint 4.x compat (handle shebangs and no deprecated APIs)
  • Loading branch information
zertosh committed May 15, 2017
commit 7ba59f61992135f57a855f061f17946cfe9549fa
19 changes: 13 additions & 6 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function reportDifferences(context, prettierSource) {
// and another's beginning does not have line endings (i.e. issues that occur
// on contiguous lines).

const source = context.getSource();
const source = context.getSourceCode().text;
const results = diff(source, prettierSource);

const batch = [];
Expand Down Expand Up @@ -271,14 +271,22 @@ module.exports.rules = {
? context.options[1].slice(1) // Remove leading @
: null;

const sourceCode = context.getSourceCode();
const source = sourceCode.text;

// The pragma is only valid if it is found in a block comment at the very
// start of the file.
if (pragma) {
// The pragma is only valid if it is found in a block comment at the
// very start of the file.
const firstComment = context.getAllComments()[0];
// ESLint 3.x reports the shebang as a "Line" node, while ESLint 4.x
// reports it as a "Shebang" node. This works for both versions:
const hasShebang = source.startsWith('#!');
const allComments = sourceCode.getAllComments();
const firstComment = hasShebang ? allComments[1] : allComments[0];
if (
!(firstComment &&
firstComment.type === 'Block' &&
firstComment.start === 0)
firstComment.loc.start.line === (hasShebang ? 2 : 1) &&
firstComment.loc.start.column === 0)
) {
return {};
}
Expand All @@ -294,7 +302,6 @@ module.exports.rules = {
// Prettier is expensive to load, so only load it if needed.
prettier = require('prettier');
}
const source = context.getSource();
const prettierSource = prettier.format(source, prettierOptions);
if (source !== prettierSource) {
reportDifferences(context, prettierSource);
Expand Down
20 changes: 20 additions & 0 deletions test/invalid/19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CODE:
#!/usr/bin/env node
/** @format */
var foo = '';

OUTPUT:
#!/usr/bin/env node
/** @format */
var foo = "";

OPTIONS:
[null, '@format']

ERRORS:
[
{
message: 'Replace `\'\'` with `""`',
line: 3, column: 11, endLine: 3, endColumn: 13,
},
]
7 changes: 5 additions & 2 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ruleTester.run('prettier', rule, {
// Facebook style but missing pragma.
{ code: `"";\n`, options: ['fb', '@format'] },
// Facebook style with pragma.
{ code: `/** @format */\n'';\n`, options: ['fb', '@format'] }
{ code: `/** @format */\n'';\n`, options: ['fb', '@format'] },
// Shebang with pragma.
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] }
],
invalid: [
'01',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't a strong objection, but I have doubts about whether putting the invalid cases in separate files actually improves debuggability. If a single test case is failing, it seems like it would be easier to search for the corresponding code within a single file rather than opening/closing a bunch of test files to try to find the right one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, it's actually more about easily looking at a fixture file and easily converting line & column in the test vs the message. It's also very convenient to be able to quickly focus on a few tests by commenting out.

Expand All @@ -57,7 +59,8 @@ ruleTester.run('prettier', rule, {
'15',
'16',
'17',
'18'
'18',
'19'
].map(loadInvalidFixture)
});

Expand Down