Skip to content

Commit

Permalink
feat(upgrade): adding exports of private eslint api on first rule run
Browse files Browse the repository at this point in the history
  • Loading branch information
Святослав Зайцев committed Jul 10, 2022
1 parent e85de61 commit 582543e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 2021,
sourceType: 'script',
},
plugins: ['eslint-plugin', 'filenames', 'import', 'jest', 'node', 'prettier'],
Expand Down
93 changes: 57 additions & 36 deletions lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
'use strict';

const { CLIEngine: v7AndEarlier, ESLint: v8Plus } = require('eslint');

const ESLint = v7AndEarlier || v8Plus;
const isEslint7 = Boolean(v7AndEarlier);

const cli = new ESLint();

function getConfig(filePath) {
// < 8 support
if ('getConfigForFile' in cli) {
return cli.getConfigForFile(filePath) || {};
}

// This is not supported in v8 -- config checks are all async, yet rules are sync
return {};
const { join } = require('path');
const { existsSync: isExists, writeFileSync: write, readFileSync: read } = require('fs');

let CLIEngine;
try {
({ CLIEngine } = require('eslint/eslint-plugin-decorator-position'));
} catch {
setupEslintExports();
({ CLIEngine } = require('eslint/eslint-plugin-decorator-position'));
}

const cli = CLIEngine ? new CLIEngine() : undefined;

module.exports = {
meta: {
type: 'layout',
Expand Down Expand Up @@ -127,12 +123,13 @@ const INTENT = {
};

const PREFER_INLINE = 'prefer-inline';
const ABOVE = 'above';

const ABOVE = 'above';
const PROPERTIES = 'properties';
const METHODS = 'methods';

const METHODS = 'methods';
// specifics set by the eslint config

const defaultOptions = {
printWidth: 100,
[PROPERTIES]: PREFER_INLINE,
Expand All @@ -142,7 +139,6 @@ const defaultOptions = {
[ABOVE]: [],
},
};

let prettier;

function decoratorPositionRule(context) {
Expand Down Expand Up @@ -341,28 +337,14 @@ function lineLength(userOptions, filePath) {
}
}

let eslintPrettierRules = [];

try {
const config = getConfig(filePath);
const rules = config || {};
const prettierConfig = rules && rules['prettier/prettier'];
eslintPrettierRules = prettierConfig || [];
} catch (e) {
// eslint-disable-next-line no-console
console.warn(e);
}
const eslintPrettierRules = cli?.getConfigForFile(filePath).rules['prettier/prettier'] || [];
const isEnabled = eslintPrettierRules[0] === 'error';

if (!isEnabled) {
// This might be a bug, in that userOptions are ignored.
// But it's been the behavior, and changing it would be a
// breaking change
if (isEslint7) {
return { printWidth: Infinity };
}
return { printWidth: Infinity };
}

// Always either use userOptions or infinite printWidth
if (!isEnabled) {
return { printWidth: userOptions.printWidth || Infinity };
}

Expand Down Expand Up @@ -537,3 +519,42 @@ function arityOfDecorator(decorator) {
return undefined;
}
}

function findNearestEslintPackage() {
let dir = __dirname;
while (!isExists(join(dir, 'node_modules', 'eslint'))) {
if (dir === join(dir, '..')) {
throw new Error('Cannot found Eslint package');
}
dir = join(dir, '..');
}
return join(dir, 'node_modules', 'eslint');
}

function setupEslintExports() {
const eslintPath = findNearestEslintPackage();
const fileName = 'eslint-plugin-decorator-position';
const filePath = join(eslintPath, `${fileName}.js`);
if (!isExists(filePath)) {
const eslintPackageJson = join(eslintPath, 'package.json');

const packageJson = JSON.parse(
read(eslintPackageJson, {
encoding: 'utf-8',
})
);

write(
filePath,
`module.exports = {
CLIEngine: require("./lib/cli-engine").CLIEngine,
}`
);

if (packageJson.exports) {
packageJson.exports[`./${fileName}`] = `./${fileName}.js`;
}

write(eslintPackageJson, JSON.stringify(packageJson));
}
}

0 comments on commit 582543e

Please sign in to comment.