-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule for testing the files property in package.json - fixes #3
- Loading branch information
1 parent
b97ac4a
commit 0bc1413
Showing
13 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface Context { | ||
cwd: string; | ||
pkg: any; | ||
typingsFile: string; | ||
testFile: string; | ||
} | ||
|
||
export interface Diagnostic { | ||
fileName: string; | ||
message: string; | ||
severity: 'error' | 'warning'; | ||
line?: number; | ||
column?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import {Context, Diagnostic} from '../interfaces'; | ||
import {getJSONPropertyPosition} from '../utils'; | ||
|
||
/** | ||
* Rule which enforces the typings file to be present in the `files` list in `package.json`. | ||
* | ||
* @param context - The context object. | ||
* @returns A list of custom diagnostics. | ||
*/ | ||
export default (context: Context): Diagnostic[] => { | ||
const {pkg, typingsFile} = context; | ||
const typingsFileName = path.basename(typingsFile); | ||
|
||
if (!Array.isArray(pkg.files) || pkg.files.indexOf(typingsFileName) !== -1) { | ||
return []; | ||
} | ||
|
||
const content = fs.readFileSync(path.join(context.cwd, 'package.json'), 'utf8'); | ||
|
||
return [ | ||
{ | ||
fileName: 'package.json', | ||
message: 'TypeScript type definition is not part of the `files` list.', | ||
severity: 'error', | ||
...getJSONPropertyPosition(content, 'files') | ||
} | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import filesProperty from './files-property'; | ||
import {Diagnostic, Context} from '../interfaces'; | ||
|
||
type RuleFunction = (context: Context) => Diagnostic[]; | ||
|
||
// List of custom rules | ||
const rules = new Set<RuleFunction>([ | ||
filesProperty | ||
]); | ||
|
||
/** | ||
* Get a list of custom diagnostics within the current context. | ||
* | ||
* @param context - The context object. | ||
* @returns List of diagnostics | ||
*/ | ||
export default (context: Context) => { | ||
const diagnostics: Diagnostic[] = []; | ||
|
||
for (const rule of rules) { | ||
diagnostics.push(...rule(context)); | ||
} | ||
|
||
return diagnostics; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Retrieve the line and column position of a property in a JSON document. | ||
* | ||
* @param content - Content of the JSON document. | ||
* @param property - Property to search for. | ||
* @returns Position of the property or `undefined` if the property could not be found. | ||
*/ | ||
export const getJSONPropertyPosition = (content: string, property: string) => { | ||
const match = new RegExp(`([\\s\\S]*?)"${property}"`, 'm').exec(content); | ||
|
||
if (!match) { | ||
return; | ||
} | ||
|
||
const lines = match[0].split('\n'); | ||
const lastLine = lines[lines.length - 1]; | ||
|
||
return { | ||
line: lines.length, | ||
column: lastLine ? lastLine.indexOf(`"${property}"`) : 0 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare const one: { | ||
(foo: string, bar: string): string; | ||
(foo: number, bar: number): number; | ||
}; | ||
|
||
export default one; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports.default = (foo, bar) => { | ||
return foo + bar; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {expectType} from '../../..'; | ||
import one from '.'; | ||
|
||
expectType<string>(one('foo', 'bar')); | ||
expectType<number>(one(1, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "foo", | ||
"files": [ | ||
"index.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"name": "foo" | ||
"name": "foo", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters