Skip to content

Commit

Permalink
feat(upgrade): adding postinstall script to exports private eslint api
Browse files Browse the repository at this point in the history
  • Loading branch information
Святослав Зайцев committed Jul 10, 2022
1 parent afdc876 commit 7ad2761
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
54 changes: 53 additions & 1 deletion lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

const { CLIEngine } = require('eslint/eslint-plugin-decorator-position');

const cli = new CLIEngine();

module.exports = {
meta: {
type: 'layout',
Expand Down Expand Up @@ -134,7 +138,9 @@ function decoratorPositionRule(context) {
}

const userOptions = context.options[0] || {};
const options = normalizeOptions(userOptions);
const filePath = context.getFilename();
const { printWidth } = lineLength(userOptions, filePath);
const options = normalizeOptions({ ...userOptions, printWidth });

return {
// eslint v7
Expand Down Expand Up @@ -308,6 +314,52 @@ function configuredDecoratorsInOptions(options) {
return allConfigs.map((config) => config[0]);
}

function lineLength(userOptions, filePath) {
if (!prettier) {
try {
// we acknowledge that this might not exist
// eslint-disable-next-line node/no-unpublished-require
prettier = require('prettier');
} catch (error) {
// throw an all errors that aren't "Cannot find module"
if (!error.message.includes('Cannot find module')) {
throw error;
}
}
}

const eslintPrettierRules = cli.getConfigForFile(filePath).rules['prettier/prettier'] || [];
const isEnabled = eslintPrettierRules[0] === 'error';

if (!isEnabled) {
return { printWidth: Infinity };
}

const eslintPrettierOptions = eslintPrettierRules[1] || {};
const usePrettierrc = !eslintPrettierOptions || eslintPrettierOptions.usePrettierrc !== false;
const prettierRcOptions =
prettier && usePrettierrc
? prettier.resolveConfig.sync(filePath, {
editorconfig: true,
})
: {};

const prettierOptions = Object.assign({}, prettierRcOptions, eslintPrettierOptions, {
filePath,
});

if (prettier && !('printWidth' in prettierOptions)) {
// available at prettier/src/main/core-options
// keep an eye on this. There may have been an issue with prettier 2.2.0
// but I've been unable to get the tests to pass prior to this change with *any*
// prettier version (issue-reproduction/196 reproduces issue #214)
const defaultPrintWidth = prettier.__internal.coreOptions.options.printWidth.default;
return { ...prettierOptions, ...userOptions, printWidth: defaultPrintWidth };
}

return Object.assign({}, prettierOptions, userOptions);
}

function normalizeConfig(config, intent) {
let name;
let options = {};
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"test:coverage": "jest --coverage",
"test:watch": "jest --watchAll",
"update": "node ./scripts/update-rules.js",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"postinstall": "node ./scripts/postinstall.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -58,8 +59,12 @@
},
"homepage": "https://github.com/NullVoxPopuli/eslint-plugin-decorator-position#readme",
"devDependencies": {
"@babel/core": "^7.18.6",
"@babel/eslint-parser": "^7.18.2",
"@babel/plugin-proposal-decorators": "^7.18.6",
"@commitlint/cli": "16.1.0",
"@commitlint/config-conventional": "16.0.0",
"@eslint/eslintrc": "^1.3.0",
"@semantic-release/changelog": "6.0.1",
"@semantic-release/git": "10.0.1",
"@typescript-eslint/parser": "5.30.5",
Expand All @@ -72,6 +77,7 @@
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jest": "26.5.3",
"eslint-plugin-markdown": "1.0.2",
"eslint-plugin-md": "^1.0.19",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-prettier": "4.0.0",
"jest": "27.4.7",
Expand All @@ -83,12 +89,8 @@
"typescript": "4.5.5"
},
"dependencies": {
"@babel/core": "^7.18.6",
"@babel/eslint-parser": "^7.18.2",
"@babel/plugin-proposal-decorators": "^7.18.6",
"@ember-data/rfc395-data": "^0.0.4",
"ember-rfc176-data": "^0.3.12",
"eslint-plugin-md": "^1.0.19",
"snake-case": "^3.0.3"
},
"peerDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { join } = require('path');
const { existsSync: isExists, writeFileSync: write, readFileSync: read } = require('fs');

const eslintPath = findNearestEslintPackage();
const eslintPackageJson = join(eslintPath, 'package.json');

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

const fileName = 'eslint-plugin-decorator-position';
const filePath = join(eslintPath, `${fileName}.js`);
write(
filePath,
`module.exports = {
CLIEngine: require("./lib/cli-engine").CLIEngine,
}`
);

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

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

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');
}

0 comments on commit 7ad2761

Please sign in to comment.