123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- 'use strict';
- const {
- showInvisibles,
- generateDifferences,
- } = require('prettier-linter-helpers');
- const { INSERT, DELETE, REPLACE } = generateDifferences;
- let prettier;
- function reportDifference(context, difference) {
- const { operation, offset, deleteText = '', insertText = '' } = difference;
- const range = [offset, offset + deleteText.length];
- const [start, end] = range.map(index =>
- context.getSourceCode().getLocFromIndex(index),
- );
- context.report({
- messageId: operation,
- data: {
- deleteText: showInvisibles(deleteText),
- insertText: showInvisibles(insertText),
- },
- loc: { start, end },
- fix: fixer => fixer.replaceTextRange(range, insertText),
- });
- }
- module.exports = {
- configs: {
- recommended: {
- extends: ['prettier'],
- plugins: ['prettier'],
- rules: {
- 'prettier/prettier': 'error',
- 'arrow-body-style': 'off',
- 'prefer-arrow-callback': 'off',
- },
- },
- },
- rules: {
- prettier: {
- meta: {
- docs: {
- url: 'https://github.com/prettier/eslint-plugin-prettier#options',
- },
- type: 'layout',
- fixable: 'code',
- schema: [
-
- {
- type: 'object',
- properties: {},
- additionalProperties: true,
- },
- {
- type: 'object',
- properties: {
- usePrettierrc: { type: 'boolean' },
- fileInfoOptions: {
- type: 'object',
- properties: {},
- additionalProperties: true,
- },
- },
- additionalProperties: true,
- },
- ],
- messages: {
- [INSERT]: 'Insert `{{ insertText }}`',
- [DELETE]: 'Delete `{{ deleteText }}`',
- [REPLACE]: 'Replace `{{ deleteText }}` with `{{ insertText }}`',
- },
- },
- create(context) {
- const usePrettierrc =
- !context.options[1] || context.options[1].usePrettierrc !== false;
- const eslintFileInfoOptions =
- (context.options[1] && context.options[1].fileInfoOptions) || {};
- const sourceCode = context.getSourceCode();
- const filepath = context.getFilename();
-
-
-
-
-
- const onDiskFilepath = context.getPhysicalFilename();
- const source = sourceCode.text;
- return {
-
- Program() {
- if (!prettier) {
-
- prettier = require('prettier');
- }
- const eslintPrettierOptions = context.options[0] || {};
- const prettierRcOptions = usePrettierrc
- ? prettier.resolveConfig.sync(onDiskFilepath, {
- editorconfig: true,
- })
- : null;
- const { ignored, inferredParser } = prettier.getFileInfo.sync(
- onDiskFilepath,
- {
- resolveConfig: false,
- withNodeModules: false,
- ignorePath: '.prettierignore',
- plugins: prettierRcOptions ? prettierRcOptions.plugins : null,
- ...eslintFileInfoOptions,
- },
- );
-
- if (ignored) {
- return;
- }
- const initialOptions = {};
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (filepath === onDiskFilepath) {
-
-
-
-
-
-
-
-
- const parserBlocklist = [null, 'markdown', 'html'];
- let inferParserToBabel = parserBlocklist.includes(inferredParser);
- switch (inferredParser) {
-
- case 'graphql': {
- if (
-
- source.startsWith('ESLintPluginGraphQLFile`')
- ) {
- inferParserToBabel = true;
- }
- break;
- }
-
- case 'svelte': {
-
- if (!context.parserPath.includes('svelte-eslint-parser')) {
-
-
- return;
- }
- }
- }
- if (inferParserToBabel) {
- initialOptions.parser = 'babel';
- }
- } else {
-
-
-
-
-
-
-
- const parserBlocklist = [
- 'babel',
- 'babylon',
- 'flow',
- 'typescript',
- 'vue',
- 'markdown',
- 'html',
- 'mdx',
- 'angular',
- 'svelte',
- ];
- if (parserBlocklist.includes(inferredParser)) {
- return;
- }
- }
- const prettierOptions = {
- ...initialOptions,
- ...prettierRcOptions,
- ...eslintPrettierOptions,
- filepath,
- };
-
-
-
-
-
-
-
-
- let prettierSource;
- try {
- prettierSource = prettier.format(source, prettierOptions);
- } catch (err) {
- if (!(err instanceof SyntaxError)) {
- throw err;
- }
- let message = 'Parsing error: ' + err.message;
-
-
-
-
- if (err.codeFrame) {
- message = message.replace(`\n${err.codeFrame}`, '');
- }
- if (err.loc) {
- message = message.replace(/ \(\d+:\d+\)$/, '');
- }
- context.report({ message, loc: err.loc });
- return;
- }
- if (source !== prettierSource) {
- const differences = generateDifferences(source, prettierSource);
- for (const difference of differences) {
- reportDifference(context, difference);
- }
- }
- },
- };
- },
- },
- },
- };
|