Skip to content

Commit 465b806

Browse files
author
Fatme
authored
Merge pull request #4434 from NativeScript/vladimirov/fix-short-imports
fix: warnings for short imports should be shown correctly
2 parents 680e5cf + f6c1ea8 commit 465b806

File tree

4 files changed

+69
-14
lines changed

4 files changed

+69
-14
lines changed

lib/services/doctor-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ export class DoctorService implements IDoctorService {
141141
for (const file of files) {
142142
const fileContent = this.$fs.readText(file);
143143
const strippedComments = helpers.stripComments(fileContent);
144-
const linesWithRequireStatements = strippedComments
144+
const linesToCheck = _.flatten(strippedComments
145145
.split(/\r?\n/)
146+
.map(line => line.split(";")));
147+
const linesWithRequireStatements = linesToCheck
146148
.filter(line => /\btns-core-modules\b/.exec(line) === null && (/\bimport\b/.exec(line) || /\brequire\b/.exec(line)));
147149

148150
for (const line of linesWithRequireStatements) {

lib/services/project-data-service.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import * as path from "path";
22
import { ProjectData } from "../project-data";
33
import { exported } from "../common/decorators";
4-
import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER, AssetConstants, SRC_DIR, RESOURCES_DIR, MAIN_DIR, CLI_RESOURCES_DIR_NAME, ProjectTypes } from "../constants";
4+
import {
5+
NATIVESCRIPT_PROPS_INTERNAL_DELIMITER,
6+
AssetConstants, SRC_DIR,
7+
RESOURCES_DIR,
8+
MAIN_DIR,
9+
CLI_RESOURCES_DIR_NAME,
10+
ProjectTypes,
11+
NODE_MODULES_FOLDER_NAME
12+
} from "../constants";
513

614
interface IProjectFileData {
715
projectData: any;
@@ -124,6 +132,7 @@ export class ProjectDataService implements IProjectDataService {
124132
supportedFileExtension = ".ts";
125133
}
126134

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

134143
if (fstat.isDirectory()) {
144+
if (filePath === pathToProjectNodeModules) {
145+
// we do not want to get the files from node_modules directory of the project.
146+
// We'll get here only when you have nsconfig.json with appDirectoryPath set to "."
147+
return false;
148+
}
149+
135150
return true;
136151
}
137152

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nativescript",
33
"preferGlobal": true,
4-
"version": "5.2.3",
4+
"version": "5.2.4",
55
"author": "Telerik <[email protected]>",
66
"description": "Command-line interface for building NativeScript projects",
77
"bin": {

test/services/doctor-service.ts

+49-11
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("doctorService", () => {
4949
filesContents: {
5050
file1: 'const application = require("application");'
5151
},
52-
expectedShortImports: [{ file: "file1", line: 'const application = require("application");' }]
52+
expectedShortImports: [{ file: "file1", line: 'const application = require("application")' }]
5353
},
5454
{
5555
filesContents: {
@@ -61,7 +61,7 @@ describe("doctorService", () => {
6161
filesContents: {
6262
file1: 'const Observable = require("data/observable").Observable;'
6363
},
64-
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable;' }]
64+
expectedShortImports: [{ file: "file1", line: 'const Observable = require("data/observable").Observable' }]
6565
},
6666
{
6767
filesContents: {
@@ -73,7 +73,7 @@ describe("doctorService", () => {
7373
filesContents: {
7474
file1: 'import * as application from "application";'
7575
},
76-
expectedShortImports: [{ file: "file1", line: 'import * as application from "application";' }]
76+
expectedShortImports: [{ file: "file1", line: 'import * as application from "application"' }]
7777
},
7878
{
7979
filesContents: {
@@ -85,7 +85,7 @@ describe("doctorService", () => {
8585
filesContents: {
8686
file1: 'import { run } from "application";'
8787
},
88-
expectedShortImports: [{ file: "file1", line: 'import { run } from "application";' }]
88+
expectedShortImports: [{ file: "file1", line: 'import { run } from "application"' }]
8989
},
9090
{
9191
filesContents: {
@@ -98,7 +98,7 @@ describe("doctorService", () => {
9898
filesContents: {
9999
file1: "import { run } from 'application';"
100100
},
101-
expectedShortImports: [{ file: "file1", line: "import { run } from 'application';" }]
101+
expectedShortImports: [{ file: "file1", line: "import { run } from 'application'" }]
102102
},
103103
{
104104
// Using single quotes
@@ -114,8 +114,8 @@ const Observable = require("data/observable").Observable;
114114
`
115115
},
116116
expectedShortImports: [
117-
{ file: "file1", line: 'const application = require("application");' },
118-
{ file: "file1", line: 'const Observable = require("data/observable").Observable;' },
117+
{ file: "file1", line: 'const application = require("application")' },
118+
{ file: "file1", line: 'const Observable = require("data/observable").Observable' },
119119
]
120120
},
121121
{
@@ -125,7 +125,7 @@ const Observable = require("tns-core-modules/data/observable").Observable;
125125
`
126126
},
127127
expectedShortImports: [
128-
{ file: "file1", line: 'const application = require("application");' },
128+
{ file: "file1", line: 'const application = require("application")' },
129129
]
130130
},
131131
{
@@ -137,8 +137,8 @@ const Observable = require("tns-core-modules/data/observable").Observable;
137137
const Observable = require("data/observable").Observable;`
138138
},
139139
expectedShortImports: [
140-
{ file: "file1", line: 'const application = require("application");' },
141-
{ file: "file2", line: 'const Observable = require("data/observable").Observable;' },
140+
{ file: "file1", line: 'const application = require("application")' },
141+
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
142142
]
143143
},
144144
{
@@ -150,7 +150,45 @@ const Observable = require("tns-core-modules/data/observable").Observable;
150150
file2: `const application = require("some-name-tns-core-modules-widgets/application");
151151
const Observable = require("tns-core-modules-widgets/data/observable").Observable;`
152152
},
153-
expectedShortImports: [ ]
153+
expectedShortImports: []
154+
},
155+
{
156+
filesContents: {
157+
// several statements on one line
158+
file1: 'const _ = require("lodash");console.log("application");'
159+
},
160+
expectedShortImports: []
161+
},
162+
{
163+
filesContents: {
164+
// several statements on one line with actual short imports
165+
file1: 'const _ = require("lodash");const application = require("application");console.log("application");',
166+
file2: 'const _ = require("lodash");const application = require("application");const Observable = require("data/observable").Observable;'
167+
},
168+
expectedShortImports: [
169+
{ file: "file1", line: 'const application = require("application")' },
170+
{ file: "file2", line: 'const application = require("application")' },
171+
{ file: "file2", line: 'const Observable = require("data/observable").Observable' },
172+
]
173+
},
174+
{
175+
filesContents: {
176+
// several statements on one line withoutshort imports
177+
file1: 'const _ = require("lodash");const application = require("tns-core-modules/application");console.log("application");',
178+
file2: 'const _ = require("lodash");const application = require("tns-core-modules/application");const Observable = require("tns-core-modules/data/observable").Observable;'
179+
},
180+
expectedShortImports: []
181+
},
182+
{
183+
// Incorrect behavior, currently by design
184+
// In case you have a multiline string and one of the lines matches our RegExp we'll detect it as short import
185+
filesContents: {
186+
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`)',
187+
},
188+
expectedShortImports: [
189+
{ file: "file1", line: 'const application = require("application")' },
190+
{ file: "file1", line: 'you should import some long words here "application" module and other words here`)' },
191+
]
154192
},
155193
];
156194

0 commit comments

Comments
 (0)