Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _checkImportType(node: ts.ImportEqualsDeclaration | ts.ImportDeclaration): ImportType {
let modulePath: string | undefined;
if (tsutils.isImportEqualsDeclaration(node)) {
if (node.moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
const matches = node.moduleReference.getFullText().match(/require\s*\(\s*'([^']+)'\s*\)/);
if (matches && matches.length === 2) {
modulePath = matches[1];
} else {
console.log('Unknown Missed Regex: ' + node.moduleReference.kind + '/' + node.moduleReference.getFullText());
}
}
}
if (tsutils.isImportDeclaration(node)) {
modulePath = node.moduleSpecifier.getText().replace(/'/g, '');
}
if (modulePath) {
// Assume that "@" is a shortcut for a relative path.
if (modulePath[0] === '.' || modulePath[0] === '@') {
return ImportType.Relative;
} else {
return ImportType.Ambient;
}
}
return ImportType.None;
}
}
function isNotValidToUseBackticksInNode(node, sourceFile) {
return (
// This captures `export blah from "package"`
tsutils_1.isExportDeclaration(node.parent) ||
// This captures `import blah from "package"`
tsutils_1.isImportDeclaration(node.parent) ||
// This captures quoted names in object literal keys
isNameInAssignment(node) ||
// This captures quoted signatures (property or method)
isSignature(node) ||
// This captures literal types in generic type constraints
isTypeConstraint(node) ||
// Older TS doesn't narrow a type when backticks are used to compare typeof
isTypeCheckWithOldTsc(node) ||
// Enum members can't use backticks
tsutils_1.isEnumMember(node.parent) ||
// Typescript converts old octal escape sequences to just the numbers therein
containsOctalEscapeSequence(node, sourceFile) ||
// Use strict declarations have to be single or double quoted
isUseStrictDeclaration(node) ||
// Lookup type parameters must be single/double quoted
isLookupTypeParameter(node));
function walk(ctx: Lint.WalkContext): void {
const { options: ignorePattern, sourceFile } = ctx;
for (const statement of sourceFile.statements) {
if (!utils.isImportDeclaration(statement)) {
continue;
}
const { importClause, moduleSpecifier } = statement;
if (importClause !== undefined || !utils.isStringLiteral(moduleSpecifier)) {
continue;
}
if (ignorePattern === undefined || !ignorePattern.test(moduleSpecifier.text)) {
ctx.addFailureAtNode(statement, Rule.FAILURE_STRING);
}
}
}
private checkStatement(statement: ts.Statement): void {
if (
!(isImportDeclaration(statement) || isImportEqualsDeclaration(statement)) ||
/\r?\n\r?\n/.test(
this.sourceFile.text.slice(
statement.getFullStart(),
statement.getStart(this.sourceFile),
),
)
) {
this.endBlock();
}
if (isImportDeclaration(statement)) {
this.checkImportDeclaration(statement);
} else if (isImportEqualsDeclaration(statement)) {
this.checkImportEqualsDeclaration(statement);
} else if (isModuleDeclaration(statement)) {
const body = moduleDeclarationBody(statement);
private checkStatement(statement: ts.Statement): void {
if (
!(isImportDeclaration(statement) || isImportEqualsDeclaration(statement)) ||
/\r?\n\r?\n/.test(
this.sourceFile.text.slice(
statement.getFullStart(),
statement.getStart(this.sourceFile),
),
)
) {
this.endBlock();
}
if (isImportDeclaration(statement)) {
this.checkImportDeclaration(statement);
} else if (isImportEqualsDeclaration(statement)) {
this.checkImportEqualsDeclaration(statement);
} else if (isModuleDeclaration(statement)) {
const body = moduleDeclarationBody(statement);
if (body !== undefined) {
for (const subStatement of body.statements) {
this.checkStatement(subStatement);
}
this.endBlock();
}
}
}
function getImportLineNumbers(ctx) {
var result = [];
for (var _i = 0, _a = ctx.sourceFile.statements; _i < _a.length; _i++) {
var statement = _a[_i];
if (!tsutils_1.isImportDeclaration(statement)) {
continue;
}
result.push(getLineNumber(statement.getStart(), ctx));
}
return result;
}
function getLineNumber(characterPosition, ctx) {
if (tsutils.isImportEqualsDeclaration(node)) {
var name_1 = node.name.text;
if (tsutils.isExternalModuleReference(node.moduleReference)) {
var moduleRef = node.moduleReference;
if (tsutils.isStringLiteral(moduleRef.expression)) {
var moduleName = moduleRef.expression.text;
validateImport(node, name_1, moduleName);
}
}
else if (tsutils.isQualifiedName(node.moduleReference)) {
var moduleName = node.moduleReference.getText();
moduleName = moduleName.replace(/.*\./, '');
validateImport(node, name_1, moduleName);
}
}
if (tsutils.isImportDeclaration(node)) {
if (node.importClause !== undefined && node.importClause.name !== undefined) {
var name_2 = node.importClause.name.text;
if (tsutils.isStringLiteral(node.moduleSpecifier)) {
var moduleName = node.moduleSpecifier.text;
validateImport(node, name_2, moduleName);
}
}
}
return ts.forEachChild(node, cb);
}
return ts.forEachChild(ctx.sourceFile, cb);