@babel/types#InterpreterDirective TypeScript Examples
The following examples show how to use
@babel/types#InterpreterDirective.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: remove-nodes-from-original-code.ts From prettier-plugin-sort-imports with Apache License 2.0 | 6 votes |
removeNodesFromOriginalCode = (
code: string,
nodes: (
| Statement
| CommentBlock
| CommentLine
| ImportDeclaration
| InterpreterDirective
)[],
) => {
let text = code;
for (const node of nodes) {
const start = Number(node.start);
const end = Number(node.end);
if (Number.isSafeInteger(start) && Number.isSafeInteger(end)) {
text = text.replace(
// only replace imports at the beginning of the line (ignoring whitespace)
// otherwise matching commented imports will be replaced
new RegExp(
'^\\s*' + escapeRegExp(code.substring(start, end)),
'm',
),
'',
);
}
}
return text;
}
Example #2
Source File: get-code-from-ast.ts From prettier-plugin-sort-imports with Apache License 2.0 | 5 votes |
getCodeFromAst = (
nodes: Statement[],
originalCode: string,
interpreter?: InterpreterDirective | null,
) => {
const allCommentsFromImports = getAllCommentsFromNodes(nodes);
const nodesToRemoveFromCode = [
...nodes,
...allCommentsFromImports,
...(interpreter ? [interpreter] : []),
];
const codeWithoutImportsAndInterpreter = removeNodesFromOriginalCode(
originalCode,
nodesToRemoveFromCode,
);
const newAST = file({
type: 'Program',
body: nodes,
directives: [],
sourceType: 'module',
interpreter: interpreter,
sourceFile: '',
leadingComments: [],
innerComments: [],
trailingComments: [],
start: 0,
end: 0,
loc: {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
},
});
const { code } = generate(newAST);
return (
code.replace(
/"PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE";/gi,
newLineCharacters,
) + codeWithoutImportsAndInterpreter.trim()
);
}