-
-
Save zenparsing/29ff706ac4c49bdd87b317ce6d4ccce0 to your computer and use it in GitHub Desktop.
Built-time macros strawman
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
import { deprecated } from './macros/deprecated.js'; | |
// Using Rust-like syntax only to make it clear these | |
// are macros and not "decorators" | |
#[deprecated] | |
function dontUseMeAnymore() { | |
eval('You wrote a bad song, Petey!'); | |
} | |
/* | |
Built-time tooling (e.g. a Webpack plugin) would | |
transform this into: | |
function dontUseMeAnymore() { | |
console.warn('This function is deprecated'); | |
eval('You wrote a bad song Petey!'); | |
} | |
*/ |
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
export function deprecated(ast) { | |
// In practice, macros would use a library to give | |
// AST mutation and construction better DX | |
ast.body.statements.unshift({ | |
type: 'ExpressionStatement', | |
expression: { | |
type: 'CallExpression', | |
callee: { | |
type: 'MemberExpression', | |
object: { | |
type: 'Identifier', | |
value: 'console', | |
}, | |
property: { | |
type: 'Identifier', | |
value: 'warn', | |
} | |
}, | |
arguments: [{ | |
type: 'StringLiteral', | |
value: 'This function is deprecated', | |
}], | |
}, | |
}); | |
return ast; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment