Typescript: code looks correct, compiler complains - node.js

I'm starting to work with typescript. So far, the experience has been pretty positive.
This morning I'm taking over 2 existing projects, which are currently deployed in production and work. And thus compile. Both projects have the same tsconfig and nodejs config as far as I can tell. One of them compiled without any issue.
The other one is a different story. The compiler seems to stumble on valid code, and I have honestly no idea what's wrong.
For example, it fails on the first line of a file:
src/services/user-agent.service.ts:1:13 - error TS1005: '=' expected.
1 import type { lookup } from 'useragent';
~
src/services/user-agent.service.ts:1:29 - error TS1005: ';' expected.
1 import type { lookup } from 'useragent';
~~~~~~~~~~~
I have a limited knowledge of typescript, so maybe this is an old, deprecated way of writing code? I looked up the documentation, it looks ok to me.
Also this first line of a function:
src/handlers/my-handler.ts:15:31 - error TS1109: Expression expected.
15 const companyId = payload?.content?.customer?.id;
~
src/handlers/my-handler.ts:15:40 - error TS1109: Expression expected.
15 const companyId = payload?.content?.customer?.id;
~
src/handlers/my-handler.ts:15:50 - error TS1109: Expression expected.
15 const companyId = payload?.content?.customer?.id;
~
src/handlers/my-handler.ts:15:53 - error TS1005: ':' expected.
15 const companyId = payload?.content?.customer?.id;
~
I'm using Visual Studio Code to edit this code, and it doesn't complain about anything. Linters also pass.
Versions:
nodejs: 10.24.0
npm: 5.8.0
typescript: 4.1.2
tsc: 3.3.3333
running in Debian on WSL2
I build with npm run build, which is using tsc behind the scenes. Again, this seems to work for one project, but not for the other, so there must be a difference, but I'm not sure where to look.

Related

TypeScript NodeJs mongoose-paginate-v2 Impossible build project

I'm trying to setup mongoose-paginate-v2 in my project but there is no way, I'm trying to build my code but I get these three errors.
Did you have any idea why?
Thanks
node_modules/#types/mongoose-paginate-v2/index.d.ts:34:21 - error TS2304: Cannot find name 'CollationOptions'.
34 collation?: CollationOptions | undefined;
~~~~~~~~~~~~~~~~
node_modules/#types/mongoose-paginate-v2/index.d.ts:45:19 - error TS2304: Cannot find name 'QueryFindOptions'.
45 options?: QueryFindOptions | undefined;
~~~~~~~~~~~~~~~~
src/models/book.model.ts:150:10 - error TS2339: Property 'paginate' does not exist on type 'Model<any, any, any>'.
150 this.paginate(query, options, function (err: any, result: any) {
~~~~~~~~
My Package.json about
"mongoose-paginate-v2": "^1.3.9",
"#types/mongoose-paginate-v2": "^1.3.9",
Lets assume someone have added dependencies of mongoose in package.json and prefix on version is like "^7.2.4"
It is not causing you any issue or error for now but as time goes and versioning of mongoose changes.
Try to use "~7.2.4" instead of "^7.2.4"
~version “Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version. ~1.2.3 will use releases from 1.2.3 to <1.3.0.
^version “Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version. ^2.3.4 will use releases from 2.3.4 to <3.0.0.

Stories not rendering in 6.1.15 version of storybook

My main.js file:
Its not rendering stories inside Header.stories.js
Even I have explicitly included Header.stories.js but still the same issue, it's not being rendered.
Please see if anyone could help me with this as I am really stuck on this issue.
PS: Use following repo to reproduce the issue or to see the implementation!
click here to see the repo
You have this error in the console :
WARNING in ./src/components/Header.stories.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Storybook_demo/my_storybook/src/components/Header.stories.js: Const declarations require an initialization value (16:20)
14 | }
15 |
> 16 | export const Primary ()=>(<Header >'hii'</Header>);
| ^
I can see the =sign in your file, I honestly don't know why it doesn't work. It's look like a bug in a babel parser ?
However, you can make it work with juste a space between Primary and =:
export const Primary = ()=>(<Header >'hii'</Header>)

Parsing error: Invalid regular expression: /\p{Lu}/: Invalid escape (Unicode general category properties)

ESLint 7.0.0 appears to have a problem with Unicode general category properties in regular expressions.
In one file, I'm getting this error message:
Parsing error: Invalid regular expression: /\p{Lu}/: Invalid escape
I'm getting it in other files as well, but this is the most simple expression on which I'm getting it.
It's a valid expression. Works just fine when I run the code. But it's breaking my ESLint run.
What setting do I use to get ESLint to accept these expressions?
Problem seems to disappear by adjusting .eslintrc.json slightly:
"parserOptions": {
"ecmaVersion": 2019
}

Windows 10 failure to compile with Node and React - syntax errors?

I've been trying to start up a React project but can't avoid the following syntax error message. I've tried Yarn only to run into more errors (that would be for another post). My coworkers have no trouble running this on their Macs, so I'm wondering if it's a Windows specific issue? Totally lost on this one:
./src/ducks/auth/index.js
Syntax error: C:/Users/user/code/project/src/ducks/auth/index.js: Unexpected
token, expected ; (5:20)
3 | export * from './constants';
4 |
> 5 | export default from './reducer';
| ^
6 |
7 | export * from './selectors';
Are you importing or exporting? Your syntax looks half and half right now. If you are exporting:
export default <name of function>
if you are importing:
import <name of default function> from './reducer
edit: Now that I look at it, you're obviously importing. When importing, you have to state the exact name of what you exported. There is no keyword default when importing. Also, you need to use import, not export.

How to show full compile error messages info in Checker FrameWork with line numbers etc

I just started using Checker Framework and have a problem that is exactly reproducible on one of the example projects from authors of this framework. This project is available here:
https://github.com/typetools/checker-framework/tree/master/docs/examples/GradleExamples
When i run this command from root:
>gradle compileJava
i receive this compilation output:
public static /*#Nullable*/ Object nullable = null;
^
required: #Initialized #NonNull Object
list.add(null); // error on this line
^
required: #Initialized #NonNull String
2 errors
:compileJava FAILED
As you can see there is no any information about where errors occur like class name, line number in code etc.
I did not find any information in their official manual about any compiler parameters that can change output format appropriately. I want error messages look like this:
~\GradleExample.java:33 error: ';' expected
UPDATE:
I achieve this behaviour on 3 machines:
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_73;
Gradle: 2.14.
OS: Microsoft Windows 10 x64 Pro [version 10.0.14393];
Java: 1.8.0_121;
Gradle: 3.4.1.
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_121;
Gradle: 3.4.1.
The absence of line numbers and class names is experienced only when running with Gradle. I also tried to run checker with Maven and with Javac from command line and it worked perfectly.
To configure Checker Framework with Gradle i followed steps from manual. There are 3 steps:
Download framework;
Unzip it to create a checker-framework directory;
Configure Gradle to include Checker Framework on the classpath.
As i understand, Gradle will do steps 1 and 2 automatically when providing needed Checker Framework's jars through dependency management. Nevertheless i tried both options:
dependency management:
I simply downloaded example project and executed "gradle compileJava" from root
of the GradleJava7Example project.
manually writing paths in gradle build file:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar",
"-Xbootclasspath/p:C:\\checker-framework-2.1.10\\checker\\dist\\jdk8.jar",
'-classpath', 'C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar;C:\\checker-framework-2.1.10\\checker\\dist\\javac.jar'
]
}
}
I've found a workaround. I'll explain it later, but now if somebody has the same problem, add this line to you JavaCompile tasks configuration:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
System.setProperty("line.separator", "\n") // <<<<<< add this line
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "${configurations.checkerFramework.asPath}",
"-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
]
}
}
First of all i must say that problem was not in Checker Framework at all. I managed to reproduce the same behavior as mentioned in question without Checker Framework. I have created a little custom Annotation Processor. Here is the code:
#SupportedSourceVersion(value = SourceVersion.RELEASE_8)
#SupportedAnnotationTypes(value = {"*"})
public class MyProcessor extends AbstractProcessor{
#Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
String sepr = System.getProperty("line.separator");
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "[error code] " + sepr + " catched!!!");
return true;
}
}
As you can see, all it does is printing a message right away from start. Note that i used a line separator provided by java.lang.System class to split message. When i registered this processor and tried to run "gradle compileJava" from gradle project it produced the following output:
:compileJava
catched!!!
1 error
:compileJava FAILED
The property "line.separator" for Windows OS returns CR+LF: "\r\n". I don't know why Messager.printMessage(Diagnostic.Kind kind, CharSequence msg) has this behaviour, because when i type System.err.print("[error code] " + sepr + " catched!!!") instead, everything works fine (note also that this problem occur only when i use Gradle, if i run manually javac with all arguments or use Maven everyting is fine).
I found that if i substitude the provided by system separator with simple "\n" symbol compiler error messages are displayed correctly.
For now i choose this solution as a workaround.

Resources