Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds server import lint restriction #528

Merged
merged 3 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

## Master

## 0.9.0-alpha.3 08/10/17

- Adds lint rule that restricts server imports. See more in [eslint-config-kyt 0.5.0 changelog](/packages/eslint-config-kyt/README.md#changelog).

## 0.9.0-alpha.2 08/08/17

- Fixes assets manifest merging by only merging server public assets
Expand Down
5 changes: 5 additions & 0 deletions e2e_tests/fixtures/lintScript-server-import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"lint-script": "kyt lint-script"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../server';
Empty file.
6 changes: 6 additions & 0 deletions e2e_tests/tests/kyt-lintScript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ describe('kyt lint-script', () => {
expect(output.code).toBe(1);
});

it('should restrict imports from server', () => {
util.setupStageWithFixture(stageName, 'lintScript-server-import');
const output = shell.exec('npm run lint-script');
expect(output.code).toBe(1);
});

afterEach(() => {
util.teardownStage(stageName);
});
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-config-kyt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ To keep things formatted as you develop, install an editor package. See more [he

## Changelog

**0.5.0-alpha.1 - 08/10/17

- Adds rule to restrict server imports. If you have a `src/server` directory, you will need to add this [.eslintrc.js](https://github.com/NYTimes/kyt/blob/965bb7b7cd244822e353795195c7d3f22c50fac1/packages/kyt-starter-universal/starter-src/src/server/.eslintrc.js) into the root of the server directory. This may be a breaking change if you have any server imports in your project from outside of the `src/server` directory.

**0.4.0 - 07/07/17

### BREAKING CHANGES
Expand Down
54 changes: 31 additions & 23 deletions packages/eslint-config-kyt/eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"extends": [
"airbnb",
"prettier",
"prettier/react"
],
"extends": ["airbnb", "prettier", "prettier/react"],

"env": {
"jest": true,
"browser": true
},

"plugins": [
"json",
"prettier"
],
"plugins": ["json", "prettier"],

"parser": "babel-eslint",
"parserOptions": {
Expand All @@ -32,25 +25,40 @@
"constructor-super": 2,
"no-this-before-super": 2,
"prefer-spread": 2,
"no-warning-comments": [1, { "terms": ["todo", "fixme"], "location": "start" }],
"no-warning-comments": [
1,
{ "terms": ["todo", "fixme"], "location": "start" }
],
"react/sort-comp": 0,
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"import/extensions": [1, { "js": "never" }],
"react/require-extension": "off",
"import/no-extraneous-dependencies": [0],
"no-param-reassign": 0,
"comma-dangle": ["error", {
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "never"
}],
"prettier/prettier": ["error", {
"printWidth": 100,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}]
"comma-dangle": [
"error",
{
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "never"
}
],
"import/no-restricted-paths": [
"error",
{
"zones": [{ "target": "./src", "from": "./src/server" }]
}
],
"prettier/prettier": [
"error",
{
"printWidth": 100,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}
]
}
}
9 changes: 2 additions & 7 deletions packages/eslint-config-kyt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-kyt",
"version": "0.4.0",
"version": "0.5.0-alpha.1",
"description": "ESLint configuration for kyt projects.",
"main": "eslintrc.json",
"author": "NYTimes",
Expand All @@ -20,10 +20,5 @@
"prettier": "1.5.2",
"prettier-eslint-cli": "4.1.1"
},
"keywords": [
"kyt",
"eslint",
"config",
"eslint-config"
]
"keywords": ["kyt", "eslint", "config", "eslint-config"]
}
40 changes: 17 additions & 23 deletions packages/kyt-core/cli/actions/lintScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,25 @@ const glob = require('glob');
const { userRootPath } = require('kyt-utils/paths')();

module.exports = (config, flags) => {
shell.config.silent = false;
const eslintrc = glob.sync(`${userRootPath}/.*eslintrc*`);
const configFile = eslintrc.length
? eslintrc[0]
: path.join(__dirname, '../../config/.eslintrc.base.json');
const backupFile = path.join(__dirname, '../../config/.eslintrc.base.json');
const configFile = eslintrc.length ? '' : `-c ${backupFile}`;
const eslintLib = require.resolve('eslint');
const eslint = eslintLib.replace(/(.*)(lib\/api\.js)/, '$1bin/eslint.js');
const sources = 'src/ kyt.config.js package.json';
const userFlags = `${flags.join(' ')}`;
const extensions = '--ext .js --ext .json';

logger.info(`Using ESLint file: ${configFile}`);
logger.info(`Using ESLint file: ${eslintrc.length ? eslintrc[0] : backupFile}`);

const lint = () => {
shell.config.silent = false;
const eslintLib = require.resolve('eslint');
const eslint = eslintLib.replace(/(.*)(lib\/api\.js)/, '$1bin/eslint.js');
const sources = 'src/ kyt.config.js package.json';
const userFlags = `${flags.join(' ')}`;
const extensions = '--ext .js --ext .json';
const cmd = `${eslint} ${sources} ${configFile} --color ${userFlags} ${extensions}`;
const output = shell.exec(cmd);
if (output.code === 0) {
logger.end(
`Your JS looks ${output.stdout === '' ? 'great ✨' : 'OK, though there were warnings 🤔👆'}`
);
}

const cmd = `${eslint} ${sources} -c ${configFile} --color ${userFlags} ${extensions}`;
const output = shell.exec(cmd);
if (output.code === 0) {
logger.end(
`Your JS looks ${output.stdout === '' ? 'great ✨' : 'OK, though there were warnings 🤔👆'}`
);
}

process.exit(output.code > 0 ? 1 : 0);
};

lint();
process.exit(output.code > 0 ? 1 : 0);
};
10 changes: 3 additions & 7 deletions packages/kyt-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kyt",
"version": "0.9.0-alpha.2",
"version": "0.9.0-alpha.3",
"description": "kyt is a toolkit that encapsulates and manages the configuration for web apps.",
"author": "NYTimes",
"license": "Apache-2.0",
Expand Down Expand Up @@ -30,7 +30,7 @@
"detect-port": "1.0.1",
"eslint": "3.19.0",
"eslint-config-airbnb": "15.0.2",
"eslint-config-kyt": "0.4.0",
"eslint-config-kyt": "0.5.0-alpha.1",
"eslint-config-prettier": "2.3.0",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-json": "1.2.0",
Expand Down Expand Up @@ -77,9 +77,5 @@
"webpack-merge": "4.1.0",
"webpack-node-externals": "1.6.0"
},
"keywords": [
"kyt",
"core",
"kyt-core"
]
"keywords": ["kyt", "core", "kyt-core"]
}
2 changes: 1 addition & 1 deletion packages/kyt-starter-static/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kyt-starter-static",
"version": "0.6.0-alpha.2",
"version": "0.6.0-alpha.3",
"description": "",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/kyt-starter-static/starter-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"devDependencies": {
"enzyme": "2.4.1",
"kyt": "0.9.0-alpha.2",
"kyt": "0.9.0-alpha.3",
"react-addons-test-utils": "15.3.0"
}
}
2 changes: 1 addition & 1 deletion packages/kyt-starter-universal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kyt-starter-universal",
"version": "0.6.0-alpha.2",
"version": "0.6.0-alpha.3",
"description": "",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/kyt-starter-universal/starter-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"devDependencies": {
"enzyme": "2.4.1",
"kyt": "0.9.0-alpha.2",
"kyt": "0.9.0-alpha.3",
"react-addons-test-utils": "15.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Disable the eslint-config-kyt `import/no-restricted-paths` rule.
// It's initially set to restrict all imports from `src/server`.
//
module.exports = {
rules: {
"import/no-restricted-paths": "off",
},
};