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

Experimental eslint integration #392

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"chalk": "^4.1.0",
"cosmiconfig": "^6.0.0",
"debug": "^4.3.1",
"eslint-plugin-package-json": "file:./src/eslint",
"globby": "^11.0.2",
"ignore": "^5.1.8",
"is-plain-obj": "^3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/eslint/index.js
@@ -0,0 +1,5 @@
const rule = require('./rule');

module.exports.rules = {
'package-json': rule,
};
11 changes: 11 additions & 0 deletions src/eslint/package.json
@@ -0,0 +1,11 @@
{
"name": "eslint-plugin-package-json",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"npm-package-json-lint": "file:../../"
},
"author": "",
"license": "ISC"
}
21 changes: 21 additions & 0 deletions src/eslint/parser.js
@@ -0,0 +1,21 @@
module.exports = {
parseForESLint(code, {filePath}) {
// We don't have JS, so this AST is for empty JS file
return {
ast: {
type: 'Program',
start: 0,
end: 0,
loc: {start: {line: 1, column: 0}, end: {line: 1, column: 0}},
range: [0, 0],
body: [],
tokens: [],
comments: [],
},
services: {
getPackageJson: () => code,
getPath: () => filePath,
},
};
},
};
41 changes: 41 additions & 0 deletions src/eslint/rule.js
@@ -0,0 +1,41 @@
const {NpmPackageJsonLint} = require('npm-package-json-lint');

module.exports = {
meta: {
type: 'problem',
fixable: 'code',
schema: [
{
type: 'object',
properties: {
rules: {
type: 'object',
additionalProperties: true,
},
},
additionalProperties: false,
},
],
},
create(context) {
return {
Program(node) {
const linter = new NpmPackageJsonLint({
packageJsonObject: JSON.parse(context.parserServices.getPackageJson()),
packageJsonFilePath: context.parserServices.getPath(),
config: context.options[0],
});
const results = linter.lint();

if (results.results.length > 0) {
results.results[0].issues.forEach(({lintMessage, lintId}) => {
context.report({
node,
message: `(${lintId}) ${lintMessage}`,
});
});
}
},
};
},
};
19 changes: 19 additions & 0 deletions test/integration/eslint/.eslintrc.js
@@ -0,0 +1,19 @@
module.exports = {
root: true,
overrides: [
{
plugins: ['package-json'],
files: ['**/package.json'],
parser: '../../../src/eslint/parser.js',
rules: {
'package-json/package-json': ['error', {
rules: {
'require-name': 'error',
'require-version': 'error',
'prefer-alphabetical-scripts': 'error',
}
}],
},
},
],
};
10 changes: 10 additions & 0 deletions test/integration/eslint/example1/package.json
@@ -0,0 +1,10 @@
{
"main": "index.js",
"scripts": {
"build": "build script",
"test": "jest",
"start": "node index.js"
},
"author": "",
"license": "ISC"
}