Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42985a7
commit d68c3db
Showing
6 changed files
with
272 additions
and
162 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,62 @@ | ||
"use strict"; | ||
|
||
const getReferenceOrigin = require("./utils/get-reference-origin"); | ||
|
||
function reportNoPlugin(context /*: Context */, node /*: Node */) { | ||
context.report({ | ||
node, | ||
message: "This file does not export a Babel plugin", | ||
}); | ||
} | ||
|
||
function is(type /*: string */) /*: (node: Node) => boolean */ { | ||
return node => node.type === type; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
schema: [], | ||
}, | ||
create(context /*: Context */) { | ||
return { | ||
Program(program /*: Node */) { | ||
if (!program.body.some(is("ExportDefaultDeclaration"))) { | ||
return reportNoPlugin(context, program); | ||
} | ||
}, | ||
ExportDefaultDeclaration(exportDefaultDecl) { | ||
let plugin = exportDefaultDecl.declaration; | ||
|
||
if (plugin.type === "CallExpression") { | ||
// export default declare(api => { ... }); | ||
const origin = getReferenceOrigin(plugin.callee, context.getScope()); | ||
|
||
if ( | ||
origin && | ||
origin.kind === "import" && | ||
origin.name === "declare" && | ||
origin.source === "@babel/helper-plugin-utils" | ||
) { | ||
plugin = plugin.arguments[0]; | ||
} | ||
} | ||
|
||
if (!plugin.type.includes("Function")) { | ||
return reportNoPlugin(context, exportDefaultDecl.parent); | ||
} | ||
|
||
const returnNode = plugin.body.body.find(is("ReturnStatement")); | ||
if (!returnNode || returnNode.argument.type !== "ObjectExpression") { | ||
return reportNoPlugin(context, exportDefaultDecl.parent); | ||
} | ||
|
||
if (!returnNode.argument.properties.some(p => p.key.name === "name")) { | ||
context.report( | ||
returnNode, | ||
"This Babel plugin doesn't have a 'name' property." | ||
); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,34 @@ | ||
/*:: // ESLint types | ||
type Node = { type: string, [string]: any }; | ||
type Definition = { | ||
type: "ImportedBinding", | ||
name: Node, | ||
node: Node, | ||
parent: Node, | ||
}; | ||
type Variable = { | ||
defs: Definition[], | ||
}; | ||
type Scope = { | ||
set: Map<string, Variable>, | ||
upper: ?Scope, | ||
}; | ||
type Context = { | ||
report(options: { | ||
node: Node, | ||
message: string, | ||
fix?: (fixer: Fixer) => ?Fixer, | ||
}): void, | ||
getScope(): Scope, | ||
}; | ||
type Fixer = { | ||
replaceText(node: Node, replacement: string): Fixer, | ||
}; | ||
*/ |
Oops, something went wrong.