-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### `require-types-at-top` | ||
|
||
Requires all type declarations to be at the top of the file, after any import declarations. | ||
|
||
#### Options | ||
|
||
The rule has a string option: | ||
|
||
* `"never"` | ||
* `"always"` | ||
|
||
The default value is `"always"`. | ||
|
||
<!-- assertions require-types-at-top --> |
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,73 @@ | ||
import _ from 'lodash'; | ||
|
||
const schema = [ | ||
{ | ||
enum: ['always', 'never'], | ||
type: 'string' | ||
} | ||
]; | ||
|
||
const create = (context) => { | ||
const always = (context.options[0] || 'always') === 'always'; | ||
|
||
if (always) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
// nodes representing type and import declarations | ||
const ignoredNodes = [ | ||
// import ... | ||
(node) => { return node.type === 'ImportDeclaration'; }, | ||
// export type Foo = ... | ||
// export opaque type Foo = ... | ||
// export type Foo from ... | ||
// export opaque type Foo from ... | ||
(node) => { return node.type === 'ExportNamedDeclaration' && node.exportKind === 'type'; }, | ||
// type Foo = ... | ||
(node) => { return node.type === 'TypeAlias'; }, | ||
// opaque type Foo = ... | ||
(node) => { return node.type === 'OpaqueType'; } | ||
]; | ||
|
||
const isIgnoredNode = (node) => { | ||
for (const predicate of ignoredNodes) { | ||
if (predicate(node)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
let regularCodeStartRange; | ||
|
||
for (const node of sourceCode.ast.body) { | ||
if (!isIgnoredNode(node)) { | ||
regularCodeStartRange = node.range; | ||
break; | ||
} | ||
} | ||
|
||
if (!_.isArray(regularCodeStartRange)) { | ||
// a source with only ignored nodes | ||
return {}; | ||
} | ||
|
||
return { | ||
'TypeAlias, OpaqueType' (node) { | ||
if (node.range[0] > regularCodeStartRange[0]) { | ||
context.report({ | ||
message: 'All type declaration should be at the top of the file, after any import declarations.', | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
} else { | ||
return {}; | ||
} | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; |
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,86 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'const foo = 3;\ntype Foo = number;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
}, | ||
{ | ||
code: 'const foo = 3;\nopaque type Foo = number;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
}, | ||
{ | ||
code: 'const foo = 3;\nexport type Foo = number;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
}, | ||
{ | ||
code: 'const foo = 3;\nexport opaque type Foo = number;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
}, | ||
{ | ||
code: 'const foo = 3;\ntype Foo = number | string;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
}, | ||
{ | ||
code: 'import bar from "./bar";\nconst foo = 3;\ntype Foo = number;', | ||
errors: [{message: 'All type declaration should be at the top of the file, after any import declarations.'}] | ||
} | ||
], | ||
misconfigured: [ | ||
{ | ||
errors: [ | ||
{ | ||
data: 'sometimes', | ||
dataPath: '[0]', | ||
keyword: 'enum', | ||
message: 'should be equal to one of the allowed values', | ||
params: { | ||
allowedValues: [ | ||
'always', | ||
'never' | ||
] | ||
}, | ||
parentSchema: { | ||
enum: [ | ||
'always', | ||
'never' | ||
], | ||
type: 'string' | ||
}, | ||
schema: [ | ||
'always', | ||
'never' | ||
], | ||
schemaPath: '#/items/0/enum' | ||
} | ||
], | ||
options: ['sometimes'] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: 'type Foo = number;\nconst foo = 3;' | ||
}, | ||
{ | ||
code: 'opaque type Foo = number;\nconst foo = 3;' | ||
}, | ||
{ | ||
code: 'export type Foo = number;\nconst foo = 3;' | ||
}, | ||
{ | ||
code: 'export opaque type Foo = number;\nconst foo = 3;' | ||
}, | ||
{ | ||
code: 'type Foo = number;\nconst foo = 3;' | ||
}, | ||
{ | ||
code: 'import bar from "./bar";\ntype Foo = number;' | ||
}, | ||
{ | ||
code: 'type Foo = number;\nimport bar from "./bar";' | ||
}, | ||
{ | ||
code: 'const foo = 3;\ntype Foo = number;', | ||
options: ['never'] | ||
} | ||
] | ||
}; |
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