Skip to content

fix: warnings for short imports should be shown correctly #4434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/services/doctor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ export class DoctorService implements IDoctorService {
for (const file of files) {
const fileContent = this.$fs.readText(file);
const strippedComments = helpers.stripComments(fileContent);
const linesWithRequireStatements = strippedComments
const linesToCheck = _.flatten(strippedComments
.split(/\r?\n/)
.map(line => line.split(";")));
const linesWithRequireStatements = linesToCheck
.filter(line => /\btns-core-modules\b/.exec(line) === null && (/\bimport\b/.exec(line) || /\brequire\b/.exec(line)));

for (const line of linesWithRequireStatements) {
Expand Down
17 changes: 16 additions & 1 deletion lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import * as path from "path";
import { ProjectData } from "../project-data";
import { exported } from "../common/decorators";
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME, ProjectTypes } from "../constants";
import {
NATIVESCRIPT_PROPS_INTERNAL_DELIMITER,
AssetConstants, SRC_DIR,
RESOURCES_DIR,
MAIN_DIR,
CLI_RESOURCES_DIR_NAME,
ProjectTypes,
NODE_MODULES_FOLDER_NAME
} from "../constants";

interface IProjectFileData {
projectData: any;
Expand Down Expand Up @@ -124,6 +132,7 @@ export class ProjectDataService implements IProjectDataService {
supportedFileExtension = ".ts";
}

const pathToProjectNodeModules = path.join(projectDir, NODE_MODULES_FOLDER_NAME);
const files = this.$fs.enumerateFilesInDirectorySync(
projectData.appDirectoryPath,
(filePath, fstat) => {
Expand All @@ -132,6 +141,12 @@ export class ProjectDataService implements IProjectDataService {
}

if (fstat.isDirectory()) {
if (filePath === pathToProjectNodeModules) {
// we do not want to get the files from node_modules directory of the project.
// We'll get here only when you have nsconfig.json with appDirectoryPath set to "."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appPath 😺

return false;
}

return true;
}

Expand Down
60 changes: 49 additions & 11 deletions test/services/doctor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("doctorService", () => {
filesContents: {
file1: 'const application = require("application");'
},
expectedShortImports: [{ file: "file1", line: 'const application = require("application");' }]
expectedShortImports: [{ file: "file1", line: 'const application = require("application")' }]
},
{
filesContents: {
Expand All @@ -61,7 +61,7 @@ describe("doctorService", () => {
filesContents: {
file1: 'const Observable = require("data/observable").Observable;'
},
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable;' }]
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable' }]
},
{
filesContents: {
Expand All @@ -73,7 +73,7 @@ describe("doctorService", () => {
filesContents: {
file1: 'import * as application from "application";'
},
expectedShortImports: [{ file: "file1", line: 'import * as application from "application";' }]
expectedShortImports: [{ file: "file1", line: 'import * as application from "application"' }]
},
{
filesContents: {
Expand All @@ -85,7 +85,7 @@ describe("doctorService", () => {
filesContents: {
file1: 'import { run } from "application";'
},
expectedShortImports: [{ file: "file1", line: 'import { run } from "application";' }]
expectedShortImports: [{ file: "file1", line: 'import { run } from "application"' }]
},
{
filesContents: {
Expand All @@ -98,7 +98,7 @@ describe("doctorService", () => {
filesContents: {
file1: "import { run } from 'application';"
},
expectedShortImports: [{ file: "file1", line: "import { run } from 'application';" }]
expectedShortImports: [{ file: "file1", line: "import { run } from 'application'" }]
},
{
// Using single quotes
Expand All @@ -114,8 +114,8 @@ const Observable = require("data/observable").Observable;
`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application");' },
{ file: "file1", line: 'const Observable = require("data/observable").Observable;' },
{ file: "file1", line: 'const application = require("application")' },
{ file: "file1", line: 'const Observable = require("data/observable").Observable' },
]
},
{
Expand All @@ -125,7 +125,7 @@ const Observable = require("tns-core-modules/data/observable").Observable;
`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application");' },
{ file: "file1", line: 'const application = require("application")' },
]
},
{
Expand All @@ -137,8 +137,8 @@ const Observable = require("tns-core-modules/data/observable").Observable;
const Observable = require("data/observable").Observable;`
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application");' },
{ file: "file2", line: 'const Observable = require("data/observable").Observable;' },
{ file: "file1", line: 'const application = require("application")' },
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
]
},
{
Expand All @@ -150,7 +150,45 @@ const Observable = require("tns-core-modules/data/observable").Observable;
file2: `const application = require("some-name-tns-core-modules-widgets/application");
const Observable = require("tns-core-modules-widgets/data/observable").Observable;`
},
expectedShortImports: [ ]
expectedShortImports: []
},
{
filesContents: {
// several statements on one line
file1: 'const _ = require("lodash");console.log("application");'
},
expectedShortImports: []
},
{
filesContents: {
// several statements on one line with actual short imports
file1: 'const _ = require("lodash");const application = require("application");console.log("application");',
file2: 'const _ = require("lodash");const application = require("application");const Observable = require("data/observable").Observable;'
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file2", line: 'const application = require("application")' },
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
]
},
{
filesContents: {
// several statements on one line withoutshort imports
file1: 'const _ = require("lodash");const application = require("tns-core-modules/application");console.log("application");',
file2: 'const _ = require("lodash");const application = require("tns-core-modules/application");const Observable = require("tns-core-modules/data/observable").Observable;'
},
expectedShortImports: []
},
{
// Incorrect behavior, currently by design
// In case you have a multiline string and one of the lines matches our RegExp we'll detect it as short import
filesContents: {
file1: 'const _ = require("lodash");const application = require("application");console.log("application");console.log(`this is line\nyou should import some long words here "application" module and other words here`)',
},
expectedShortImports: [
{ file: "file1", line: 'const application = require("application")' },
{ file: "file1", line: 'you should import some long words here "application" module and other words here`)' },
]
},
];

Expand Down