Skip to content

Commit

Permalink
Breaking: Remove pragma support
Browse files Browse the repository at this point in the history
Prettier now provides native pragma support using @Format or @prettier
notation. Use that instead of our implementation

Fixes #48, Fixes #54
  • Loading branch information
BPScott authored and not-an-aardvark committed Oct 1, 2018
1 parent 29c0506 commit 3af422c
Show file tree
Hide file tree
Showing 8 changed files with 3 additions and 167 deletions.
37 changes: 1 addition & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,46 +125,11 @@ For the list of every available exclusion rule set, please see the [readme of es
}]
```

NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))
NB: This option will merge and override any config set with `.prettierrc` files

- The second option:

- A string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`. Example:

```json
"prettier/prettier": ["error", null, "@prettier"]
```

Only files with `@prettier` in the heading docblock will be checked:

```js
/** @prettier */
console.log(1 + 2 + 3);
```

Or:

```js
/**
* @prettier
*/
console.log(4 + 5 + 6);
```

_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
- An object with the following options
- `pragma`: Also sets the aforementioned `pragma`: a string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`.
```json
"prettier/prettier": ["error", null, {
"pragma": "@prettier"
}]
```
- `usePrettierrc`: Enables loading of the Prettier configuration file, (default: `true`). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.

```json
Expand Down
48 changes: 0 additions & 48 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// ------------------------------------------------------------------------------

const diff = require('fast-diff');
const docblock = require('jest-docblock');

// ------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -285,25 +284,6 @@ function reportReplace(context, offset, deleteText, insertText) {
});
}

/**
* Get the pragma from the ESLint rule context.
* @param {RuleContext} context - The ESLint rule context.
* @returns {string|null}
*/
function getPragma(context) {
const pluginOptions = context.options[1];

if (!pluginOptions) {
return null;
}

const pragmaRef =
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;

// Remove leading @
return pragmaRef ? pragmaRef.slice(1) : null;
}

// ------------------------------------------------------------------------------
// Module Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -337,12 +317,9 @@ module.exports = {
},
{
anyOf: [
// Pragma:
{ type: 'string', pattern: '^@\\w+$' },
{
type: 'object',
properties: {
pragma: { type: 'string', pattern: '^@\\w+$' },
usePrettierrc: { type: 'boolean' }
},
additionalProperties: true
Expand All @@ -352,37 +329,12 @@ module.exports = {
]
},
create(context) {
const pragma = getPragma(context);
const usePrettierrc =
!context.options[1] || context.options[1].usePrettierrc !== false;
const sourceCode = context.getSourceCode();
const filepath = context.getFilename();
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) {
// 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.loc.start.line === (hasShebang ? 2 : 1) &&
firstComment.loc.start.column === 0
)
) {
return {};
}
const parsed = docblock.parse(firstComment.value);
if (parsed[pragma] !== '') {
return {};
}
}

if (prettier && prettier.clearConfigCache) {
prettier.clearConfigCache();
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
},
"homepage": "https://github.com/prettier/eslint-plugin-prettier#readme",
"dependencies": {
"fast-diff": "^1.1.1",
"jest-docblock": "^21.0.0"
"fast-diff": "^1.1.1"
},
"peerDependencies": {
"prettier": ">= 1.13.0"
Expand Down
22 changes: 0 additions & 22 deletions test/invalid/11-c.txt

This file was deleted.

18 changes: 0 additions & 18 deletions test/invalid/12.txt

This file was deleted.

20 changes: 0 additions & 20 deletions test/invalid/19.txt

This file was deleted.

18 changes: 1 addition & 17 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,8 @@ ruleTester.run('prettier', rule, {
valid: [
// Correct style.
{ code: '"";\n' },
// No pragma = No prettier check.
{ code: '""\n', options: [null, '@format'] },
// Facebook style uses single quotes.
{ code: `('');\n`, options: ['fb'] },
// Facebook style but missing pragma.
{ code: `"";\n`, options: ['fb', '@format'] },
// Facebook style with pragma.
{ code: `/** @format */\n('');\n`, options: ['fb', '@format'] },
// Shebang with pragma.
{ code: `#!/bin/node\n/** @format */\n"";\n`, options: [null, '@format'] },
// Shebang with pragma from options.
{
code: `#!/bin/node\n/** @format */\n"";\n`,
options: [null, { pragma: '@format' }]
},
// Single quote from .prettierrc.
{ code: `'';\n`, filename: getPrettierRcJsFilename('single-quote') },
// Override .prettierrc from object option.
Expand Down Expand Up @@ -98,15 +85,12 @@ ruleTester.run('prettier', rule, {
'10',
'11-a',
'11-b',
'11-c',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19'
'18'
].map(loadInvalidFixture)
});

Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,6 @@ isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"

jest-docblock@^21.0.0:
version "21.1.0"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.1.0.tgz#43154be2441fb91403e36bb35cb791a5017cea81"

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit 3af422c

Please sign in to comment.