@protobuf-ts/plugin-framework
Advanced tools
Comparing version 2.0.0-alpha.17 to 2.0.0-alpha.18
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TypeScriptImports = void 0; | ||
exports.findNamedImports = exports.createNamedImport = exports.ensureNamedImportPresent = exports.TypeScriptImports = void 0; | ||
const runtime_1 = require("@protobuf-ts/runtime"); | ||
@@ -20,5 +20,5 @@ const ts = require("typescript"); | ||
*/ | ||
name(source, importName, importFrom) { | ||
name(source, importName, importFrom, isTypeOnly = false) { | ||
const blackListedNames = this.symbols.list(source).map(e => e.name); | ||
return ensureNamedImportPresent(source.getSourceFile(), importName, importFrom, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
return ensureNamedImportPresent(source.getSourceFile(), importName, importFrom, isTypeOnly, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
} | ||
@@ -44,3 +44,3 @@ /** | ||
*/ | ||
type(source, descriptor, kind = 'default') { | ||
type(source, descriptor, kind = 'default', isTypeOnly = false) { | ||
const symbolReg = this.symbols.get(descriptor, kind); | ||
@@ -55,3 +55,3 @@ // symbol in this file? | ||
const blackListedNames = this.symbols.list(source).map(e => e.name); | ||
return ensureNamedImportPresent(source.getSourceFile(), symbolReg.name, importPath, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
return ensureNamedImportPresent(source.getSourceFile(), symbolReg.name, importPath, isTypeOnly, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
} | ||
@@ -106,3 +106,4 @@ } | ||
/** | ||
* Import {importName} from "importFrom"; | ||
* import {importName} from "importFrom"; | ||
* import type {importName} from "importFrom"; | ||
* | ||
@@ -123,5 +124,5 @@ * If the import is already present, just return the | ||
*/ | ||
function ensureNamedImportPresent(currentFile, importName, importFrom, blacklistedNames, addStatementFn, escapeCharacter = '$') { | ||
function ensureNamedImportPresent(currentFile, importName, importFrom, isTypeOnly, blacklistedNames, addStatementFn, escapeCharacter = '$') { | ||
var _a; | ||
const all = findNamedImports(currentFile), taken = all.map(ni => { var _a; return (_a = ni.as) !== null && _a !== void 0 ? _a : ni.name; }).concat(blacklistedNames), match = all.find(ni => ni.name === importName && ni.from === importFrom); | ||
const all = findNamedImports(currentFile), taken = all.map(ni => { var _a; return (_a = ni.as) !== null && _a !== void 0 ? _a : ni.name; }).concat(blacklistedNames), match = all.find(ni => ni.name === importName && ni.from === importFrom && ni.isTypeOnly === isTypeOnly); | ||
if (match) { | ||
@@ -141,21 +142,27 @@ return (_a = match.as) !== null && _a !== void 0 ? _a : match.name; | ||
} | ||
const statementToAdd = createNamedImport(importName, importFrom, as); | ||
const statementToAdd = createNamedImport(importName, importFrom, as, isTypeOnly); | ||
addStatementFn(statementToAdd); | ||
return as !== null && as !== void 0 ? as : importName; | ||
} | ||
exports.ensureNamedImportPresent = ensureNamedImportPresent; | ||
/** | ||
* import {<name>} from '<from>'; | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
function createNamedImport(name, from, as) { | ||
function createNamedImport(name, from, as, isTypeOnly = false) { | ||
if (as) { | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(ts.createIdentifier(name), ts.createIdentifier(as))]), false), ts.createStringLiteral(from)); | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(ts.createIdentifier(name), ts.createIdentifier(as))]), isTypeOnly), ts.createStringLiteral(from)); | ||
} | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ | ||
ts.createImportSpecifier(undefined, ts.createIdentifier(name)) | ||
])), ts.createStringLiteral(from)); | ||
]), isTypeOnly), ts.createStringLiteral(from)); | ||
} | ||
exports.createNamedImport = createNamedImport; | ||
/** | ||
* import {<name>} from '<from>'; | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
@@ -174,3 +181,4 @@ function findNamedImports(sourceFile) { | ||
as: importSpecifier.name.escapedText.toString(), | ||
from: s.moduleSpecifier.text | ||
from: s.moduleSpecifier.text, | ||
isTypeOnly: s.importClause.isTypeOnly | ||
}); | ||
@@ -182,3 +190,4 @@ } | ||
as: undefined, | ||
from: s.moduleSpecifier.text | ||
from: s.moduleSpecifier.text, | ||
isTypeOnly: s.importClause.isTypeOnly | ||
}); | ||
@@ -192,2 +201,3 @@ } | ||
} | ||
exports.findNamedImports = findNamedImports; | ||
/** | ||
@@ -194,0 +204,0 @@ * Create a relative path for an import statement like |
@@ -17,5 +17,5 @@ import { assert } from "@protobuf-ts/runtime"; | ||
*/ | ||
name(source, importName, importFrom) { | ||
name(source, importName, importFrom, isTypeOnly = false) { | ||
const blackListedNames = this.symbols.list(source).map(e => e.name); | ||
return ensureNamedImportPresent(source.getSourceFile(), importName, importFrom, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
return ensureNamedImportPresent(source.getSourceFile(), importName, importFrom, isTypeOnly, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
} | ||
@@ -41,3 +41,3 @@ /** | ||
*/ | ||
type(source, descriptor, kind = 'default') { | ||
type(source, descriptor, kind = 'default', isTypeOnly = false) { | ||
const symbolReg = this.symbols.get(descriptor, kind); | ||
@@ -52,3 +52,3 @@ // symbol in this file? | ||
const blackListedNames = this.symbols.list(source).map(e => e.name); | ||
return ensureNamedImportPresent(source.getSourceFile(), symbolReg.name, importPath, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
return ensureNamedImportPresent(source.getSourceFile(), symbolReg.name, importPath, isTypeOnly, blackListedNames, statementToAdd => source.addStatement(statementToAdd, true)); | ||
} | ||
@@ -102,3 +102,4 @@ } | ||
/** | ||
* Import {importName} from "importFrom"; | ||
* import {importName} from "importFrom"; | ||
* import type {importName} from "importFrom"; | ||
* | ||
@@ -119,5 +120,5 @@ * If the import is already present, just return the | ||
*/ | ||
function ensureNamedImportPresent(currentFile, importName, importFrom, blacklistedNames, addStatementFn, escapeCharacter = '$') { | ||
export function ensureNamedImportPresent(currentFile, importName, importFrom, isTypeOnly, blacklistedNames, addStatementFn, escapeCharacter = '$') { | ||
var _a; | ||
const all = findNamedImports(currentFile), taken = all.map(ni => { var _a; return (_a = ni.as) !== null && _a !== void 0 ? _a : ni.name; }).concat(blacklistedNames), match = all.find(ni => ni.name === importName && ni.from === importFrom); | ||
const all = findNamedImports(currentFile), taken = all.map(ni => { var _a; return (_a = ni.as) !== null && _a !== void 0 ? _a : ni.name; }).concat(blacklistedNames), match = all.find(ni => ni.name === importName && ni.from === importFrom && ni.isTypeOnly === isTypeOnly); | ||
if (match) { | ||
@@ -137,3 +138,3 @@ return (_a = match.as) !== null && _a !== void 0 ? _a : match.name; | ||
} | ||
const statementToAdd = createNamedImport(importName, importFrom, as); | ||
const statementToAdd = createNamedImport(importName, importFrom, as, isTypeOnly); | ||
addStatementFn(statementToAdd); | ||
@@ -145,10 +146,12 @@ return as !== null && as !== void 0 ? as : importName; | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
function createNamedImport(name, from, as) { | ||
export function createNamedImport(name, from, as, isTypeOnly = false) { | ||
if (as) { | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(ts.createIdentifier(name), ts.createIdentifier(as))]), false), ts.createStringLiteral(from)); | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ts.createImportSpecifier(ts.createIdentifier(name), ts.createIdentifier(as))]), isTypeOnly), ts.createStringLiteral(from)); | ||
} | ||
return ts.createImportDeclaration(undefined, undefined, ts.createImportClause(undefined, ts.createNamedImports([ | ||
ts.createImportSpecifier(undefined, ts.createIdentifier(name)) | ||
])), ts.createStringLiteral(from)); | ||
]), isTypeOnly), ts.createStringLiteral(from)); | ||
} | ||
@@ -158,4 +161,6 @@ /** | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
function findNamedImports(sourceFile) { | ||
export function findNamedImports(sourceFile) { | ||
let r = []; | ||
@@ -172,3 +177,4 @@ for (let s of sourceFile.statements) { | ||
as: importSpecifier.name.escapedText.toString(), | ||
from: s.moduleSpecifier.text | ||
from: s.moduleSpecifier.text, | ||
isTypeOnly: s.importClause.isTypeOnly | ||
}); | ||
@@ -180,3 +186,4 @@ } | ||
as: undefined, | ||
from: s.moduleSpecifier.text | ||
from: s.moduleSpecifier.text, | ||
isTypeOnly: s.importClause.isTypeOnly | ||
}); | ||
@@ -183,0 +190,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import * as ts from "typescript"; | ||
import { SymbolTable } from "./symbol-table"; | ||
@@ -16,3 +17,3 @@ import { AnyTypeDescriptorProto } from "./descriptor-info"; | ||
*/ | ||
name(source: TypescriptFile, importName: string, importFrom: string): string; | ||
name(source: TypescriptFile, importName: string, importFrom: string, isTypeOnly?: boolean): string; | ||
/** | ||
@@ -35,3 +36,41 @@ * Import * as importAs from "importFrom"; | ||
*/ | ||
type(source: TypescriptFile, descriptor: AnyTypeDescriptorProto, kind?: string): string; | ||
type(source: TypescriptFile, descriptor: AnyTypeDescriptorProto, kind?: string, isTypeOnly?: boolean): string; | ||
} | ||
/** | ||
* import {importName} from "importFrom"; | ||
* import type {importName} from "importFrom"; | ||
* | ||
* If the import is already present, just return the | ||
* identifier. | ||
* | ||
* If the import is not present, create the import | ||
* statement and call `addStatementFn`. | ||
* | ||
* If the import name is taken by another named import | ||
* or is in the list of blacklisted names, an | ||
* alternative name is used: | ||
* | ||
* Import {importName as alternativeName} from "importFrom"; | ||
* | ||
* Returns the imported name or the alternative name. | ||
*/ | ||
export declare function ensureNamedImportPresent(currentFile: ts.SourceFile, importName: string, importFrom: string, isTypeOnly: boolean, blacklistedNames: string[], addStatementFn: (statementToAdd: ts.ImportDeclaration) => void, escapeCharacter?: string): string; | ||
/** | ||
* import {<name>} from '<from>'; | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
export declare function createNamedImport(name: string, from: string, as?: string, isTypeOnly?: boolean): ts.ImportDeclaration; | ||
/** | ||
* import {<name>} from '<from>'; | ||
* import {<name> as <as>} from '<from>'; | ||
* import type {<name>} from '<from>'; | ||
* import type {<name> as <as>} from '<from>'; | ||
*/ | ||
export declare function findNamedImports(sourceFile: ts.SourceFile): { | ||
name: string; | ||
as: string | undefined; | ||
from: string; | ||
isTypeOnly: boolean; | ||
}[]; |
{ | ||
"name": "@protobuf-ts/plugin-framework", | ||
"version": "2.0.0-alpha.17", | ||
"version": "2.0.0-alpha.18", | ||
"description": "framework to create protoc plugins", | ||
@@ -36,6 +36,6 @@ "license": "(Apache-2.0 AND BSD-3-Clause)", | ||
"dependencies": { | ||
"@protobuf-ts/runtime": "^2.0.0-alpha.17", | ||
"@protobuf-ts/runtime": "^2.0.0-alpha.18", | ||
"typescript": ">=3.8.3 <4" | ||
}, | ||
"gitHead": "630221b8a3ded00c2bab35343c2c79f1121447d6" | ||
"gitHead": "541877f464ddbeb528470f9e899489bb25066a3e" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
391330
9027